I have a nav_graph, where Fragment 1 and Fragment 2 are defined. Fragment1 has view pager with 3 tabs and each tab has recyclerview.
How can i navigate to Fragment 2 on item click of recyclerview ?
Fragment->ViewPager->Recyclerview->ClickAction.
I have a nav_graph, where Fragment 1 and Fragment 2 are defined. Fragment1 has view pager with 3 tabs and each tab has recyclerview.
How can i navigate to Fragment 2 on item click of recyclerview ?
Fragment->ViewPager->Recyclerview->ClickAction.
In recyclerview's fragment, simply call requireParentFragment().findNavController().navigate(/* destination */)
to navigate to Fragment2.
Besides, you should pass a lambda into your recycler view adapter and then pass it into your view holder for using it (i recommend this way).
You can read more about this in the sample code below.
class YourRecyclerViewAdapter(..., private val onItemClick: () -> Unit) : RecyclerView.Adapter<YourViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup) = YourViewHolder(onItemClick)
override fun onBindViewHolder(holder: YourViewHolder, position: Int) {
holder.bind(...)
}
}
class YourViewHolder(..., private val onItemClick: () -> Unit) : RecyclerView.ViewHolder(...) {
fun bind(...) {
// Use onItemClick here...
}
}
class RecyclerViewFragment : Fragment() {
override fun onViewCreated(view: View, saveInstanceState: Bundle?) {
val adapter = YourRecyclerViewAdapter(...) {
requireParentFragment().findNavController().navigate(...)
}
yourRecyclerView.adapter = adapter
}
}