Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use navigation component with clicks coming from RecyclerView elements?

I have a feed fragment that its main element is a RecyclerView of posts.

I am managing the recycler with Lisa Wray's Groupie library https://github.com/lisawray/groupie

Now in my navigation graph I have an action connecting the feed fragment to the another fragment, and I try to call this action with an on click listener I create in the bind function of the adapter.

val action=FeedFragmentDirections.actionDestinationFeedToDestinationAddToBucket(image.id)
findNavController().navigate(action)

I keep getting an error saying navigation destination XXXXX is unknown to this NavController

I've tried adding a few things before the findNavController(), like viewHolder.itemView or viewHolder.root but nothing works.

When I use viewHolder.itemView.rootView I get this error instead View DecorView@a190adf[MainActivity] does not have a NavController set I don't know what DecorView is.

Any ideas how I can implement my oncick listeners properly?

like image 455
Tsabary Avatar asked Mar 26 '19 22:03

Tsabary


1 Answers

Using itemView seems to work too as that's what I have on my code:

class RecyclerSettingsItem(
private val activity: MainActivity,
private val settingItem: String
) : Item<GroupieViewHolder>() {

override fun bind(viewHolder: GroupieViewHolder, position: Int) {

    var navController: NavController? = null

    viewHolder.apply {

        with(viewHolder.itemView) {

            tvTitle.text = settingItem

            itemView.setOnClickListener {

                navController = Navigation.findNavController(itemView)
                navController!!.navigate(R.id.action_settingsFragment_to_contactTracingFragment)

            }

        }

    }
}


override fun getLayout() = R.layout.rv_settings_item_row

}
like image 169
Francislainy Campos Avatar answered Sep 28 '22 09:09

Francislainy Campos