我有两个片段-家庭片段和片段2。我在Home Fragment(id:imageView 3),当我点击图像时,我应该被带到Frag 2。我使用以下代码行使图像可点击:
android:id="@+id/imageView3"
android:layout_width="383dp"
android:layout_height="211dp"
android:clickable="true"
android:focusable="true"
我正在添加下面的Home Fragment和Frag 2代码,我已经尝试在Home Fragment中设置On Click,但我不确定如何将相应的代码添加到Frag 2。我已经添加了从Home Fragent到Frag 2的导航,但我认为我还应该添加一些代码到MainActivity?我遇到的所有在线参考都非常令人困惑,我是一个初学者,试图自己学习,所以请帮助我了解Home Fragment,Frag 2和MainActivity中的代码。非常感谢你的帮助!!
HomeFragment.kt
package com.example.landingpages.ui.home
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.findNavController
import com.example.landingpages.Frag2
import com.example.landingpages.R
import com.example.landingpages.databinding.FragmentHomeBinding
class HomeFragment : Fragment() {
private var _binding: FragmentHomeBinding? = null
// This property is only valid between onCreateView and
// onDestroyView.
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
val homeViewModel =
ViewModelProvider(this).get(HomeViewModel::class.java)
_binding = FragmentHomeBinding.inflate(inflater, container, false)
val root: View = binding.root
val imageView5 = findNavController(R.id.imageView5) as ImageView
imageView5.setOnClickListener {
val intent = Intent(this, Frag2::class.java)
startActivity(intent)
}
val textView: TextView = binding.textView
homeViewModel.text.observe(viewLifecycleOwner) {
}
return root
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
Frag2.kt
package com.example.landingpages
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.example.landingpages.databinding.FragmentIplAuctionBinding
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"
class Frag2 : Fragment() {
private var param1: String? = null
private var param2: String? = null
private lateinit var binding: FragmentIplAuctionBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = FragmentIplAuctionBinding.inflate(layoutInflater)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_ipl_auction, container, false)
}
}
}
MainActivity.kt
package com.example.landingpages
import android.os.Bundle
import com.google.android.material.bottomnavigation.BottomNavigationView
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import com.example.landingpages.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
val navView: BottomNavigationView = binding.navView
val navController = findNavController(R.id.nav_host_fragment_activity_main)
val appBarConfiguration = AppBarConfiguration(
setOf(
R.id.navigation_home, R.id.navigation_matches, R.id.navigation_profile
)
)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
}
}
我已经将setOnClickListener添加到home片段中。我想我必须在Frag 2中添加return(),但我不确定。我也不确定要在主活动中添加Frag 2,因为我使用的其他片段都是导航栏。要求很简单:如果用户单击imageview 3,则应将其带到Frag 2
1条答案
按热度按时间w8rqjzmb1#
the documentation的简单答案是在onClick期间导航到视图:
我建议学习this course来完全理解片段导航。