Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigation Component in RecyclerView Adapter

I wrote the following piece of code in the adapter.

holder.itemView.setOnClickListener {
    val action = SearchFragmentDirections.actionSearchFragmentToArtistFragment(artist.id)
    Navigation.createNavigateOnClickListener(action)
}

And this is may navigation xml:

 ...
    <fragment android:id="@+id/searchFragment"
          android:name="com.salmanseifian.spotiny.ui.search.SearchFragment"
          android:label="SearchFragment">

    <action android:id="@+id/action_searchFragment_to_artistFragment"
            app:destination="@id/artistFragment"
            app:popUpTo="@+id/searchFragment"/>
</fragment>

but it won't work. Can anybody help?

like image 367
salmanseifian Avatar asked Jul 19 '19 18:07

salmanseifian


People also ask

What is navigation component in android?

Android Jetpack's Navigation component helps you implement navigation, from simple button clicks to more complex patterns, such as app bars and the navigation drawer. The Navigation component also ensures a consistent and predictable user experience by adhering to an established set of principles.

Can we use navigation component for activities?

To take full advantage of the Navigation component, your app should use multiple fragments in a single activity. However, activities can still benefit from the Navigation component. Note, however, that your app's UI must be visually broken up across several navigation graphs.

What are the methods of recyclerview adapter in Android?

The RecyclerView.Adapter has three primary methods: onCreateViewHolder(), onBindViewHolder(), and getItemCount(). onCreateViewHolder() inflates an XML layout and returns a ViewHolder. onBindViewHolder() sets the various information in the list item View through the ViewHolder.

How to get data from the model to the recyclerview?

We do that with the adapter and ViewHolder objects. If you remember from the last post, the adapter object is in charge to get data from the model, process it and send it to the recyclerview using ViewHolder objects as a way to bind (display) the data to the UI since they are representations from the kotlin/java side of each row’s element.

How to add a view to the Recycler view of mainactivity?

Create a new class RvAdapter.kt this will act as an Adapter class for the recycler view. Using View binding we use the generated class of the layout single_item.xml ie SingleItemBinding to add data and view in the recycler view of MainActivity.kt in our case.

How to create a horizontal recyclerview in Java?

Start working on the Adapter class for the horizontal RecyclerView: So, right-click on the just created folder (RV1) -> New -> Java Class. Give the name of the class. I named it RVOneAdapter. So with the adapter, we also need a ViewHolder class. The use of these classes and every other method are already clearly described in this GFG link.


1 Answers

You are using lambda which is itself a click listener. Check this Navigation Docs which has proper implementation of click listener using navigation id and createNavigationListener.

Use below code for your case.

holder.itemView.setOnClickListener(
Navigation.createNavigateOnClickListener(R.id.action_searchFragment_to_artistFragment)
)

OR, try this way

holder.itemView.setOnClickListener{ view ->
 view.findNavController().navigate(R.id.action_searchFragment_to_artistFragment)
}
like image 58
Rajnish suryavanshi Avatar answered Sep 20 '22 16:09

Rajnish suryavanshi