问题
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.
Code:
https://github.com/joshvapeter/KotlinBottomNavigationNew
Activies
1)One Main Activity
2)Two Fragments
3)Two Adapter Class
4)Two Layout files
Expectation
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.
Code
Fragment One Adapter
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
}
回答1:
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.
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)
}
}
}
}
来源:https://stackoverflow.com/questions/53982139/fragment-onclick-listener