RecyclerView适配器中的导航组件 [英] Navigation Component in RecyclerView Adapter
本文介绍了RecyclerView适配器中的导航组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在适配器中编写了以下代码.
I wrote the following piece of code in the adapter.
holder.itemView.setOnClickListener {
val action = SearchFragmentDirections.actionSearchFragmentToArtistFragment(artist.id)
Navigation.createNavigateOnClickListener(action)
}
这可能是导航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?
推荐答案
您正在使用lambda本身就是一个点击监听器. 选中此导航文档,它可以正确实现点击侦听器使用导航ID和createNavigationListener.
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)
)
或者,尝试这种方式
holder.itemView.setOnClickListener{ view ->
view.findNavController().navigate(R.id.action_searchFragment_to_artistFragment)
}
这篇关于RecyclerView适配器中的导航组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文