片段Onclick侦听器 [英] Fragment Onclick Listener
问题描述
我在项目中使用了底部导航活动.其中包含以下活动.我正在使用科特林语言.我不确定在底部导航活动中如何从一个片段路由到另一个片段.感谢您的帮助.
I am using Bottom Navigation Activity in my project. Which contains following activities. I am using kotlin Language. I am not sure how to route from one fragment to another fragment in bottom navigation activity. Any help is appreciated.
代码:
https://github.com/joshvapeter/KotlinBottomNavigationNew
活动
1)One Main Activity
2)Two Fragments
3)Two Adapter Class
4)Two Layout files
期望
当我单击一个碎片回收者视图项目"时,它需要自动将其路由到下一个底部标签中的两个碎片回收器视图项目"中.下面是我在项目中使用的代码.
When I click the Fragment One Recycler View Item then It need to automatically route it to Fragment two Recycler View Item in the next bottom tab. Below is the code, I am using in my project.
代码
片段一适配器
itemView.setOnClickListener {
Log.d("Fragment One Clicked","Fragment One Clicked")
//FragmentTwo()
}
MainActivity
fun ShowFragmentOne() {
val transaction = manager.beginTransaction()
val fragment = FragmentOne()
transaction.replace(R.id.one, fragment)
transaction.addToBackStack(null)
transaction.commit()
isFragmentOneLoaded = true
}
fun ShowFragmentTwo() {
val transaction = manager.beginTransaction()
val fragment = FragmentTwo()
transaction.replace(R.id.two, fragment)
transaction.addToBackStack(null)
transaction.commit()
isFragmentOneLoaded = false
}
推荐答案
我没有足够的声誉来添加评论.因此,根据我的理解,我正在编写此答案.如果我的理解是错误的,请发表评论.
I don't have enough reputation to add a comment. So on the basis of my understanding, i am writing this answer. If my understanding is wrong, please comment.
据我了解,当您在FragmentOne的RecyclerView中单击某个项目时,您想移至FragmentTwo.您可以通过以下方式实现它:
From what i can understand you want to move to FragmentTwo when an item is clicked in RecyclerView in FragmentOne. You can achieve it in following way:
FragmentOne :
fun onItemSelected(item:MyModel){
(activity as MainActivity).showFragmentTwo(item)
}
FragmentOneAdapter :
class FragmentOneAdapter(val fragment:FragmentOne,val myList:ArrayList<MyModel>):RecyclerView.Adapter<MyViewHolder>(){
override fun onCreateViewHolder(p0: ViewGroup, p1: Int): MyViewHolder {
//your code to create view holder
}
override fun onBindViewHolder(p0: MyViewHolder, p1: Int) {
p0.bindItem(myList[p1], fragment)
}
override fun getItemCount(): Int {
return myList.size
}
class MyViewHolder(view:View):RecyclerView.ViewHolder(view){
fun bindItem(item:MyModel,frag:FragmentOne)=with(itemView){
setOnClickListener{
frag.onItemSelected(item)
}
}
}
}
这篇关于片段Onclick侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!