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?
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.
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.
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.
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.
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.
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.
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)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With