RecyClerview适配器中的导航组件[英] Navigation Component in RecyclerView Adapter

本文是小编为大家收集整理的关于RecyClerview适配器中的导航组件的处理方法,想解了RecyClerview适配器中的导航组件的问题怎么解决?RecyClerview适配器中的导航组件问题的解决办法?RecyClerview适配器中的导航组件问题的解决方案?那么可以参考本文帮助大家快速定位并解决问题,译文如有不准确的地方,大家可以切到English参考源文内容。

问题描述

我在适配器中写下了以下代码.

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>

但它不起作用.有人帮忙吗?

推荐答案

您正在使用lambda,它本身就是单击侦听器. 选中此导航文档,它有正确实现单击侦听器使用导航ID和CreateNavigationListener.

用于您的案例以下代码.

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

或,尝试以这种方式

holder.itemView.setOnClickListener{ view ->
 view.findNavController().navigate(R.id.action_searchFragment_to_artistFragment)
}

其他推荐答案

我认为最好将此添加到已接受的答案:

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

其他推荐答案

在recyclerview适配器中OnBindViewHolder方法您可以以这种方式拥有它:

        holder.bookBtn.setOnClickListener(view -> {
            Bundle bundle = new Bundle();
            String salonJsonString = 
            HelperMethods.getGsonParser().toJson(salonModel);bundle.putString("SALON_DETAILS", salonJsonString);
            Navigation.findNavController(view).navigate(R.id.salonDetailFragment,bundle);     
        });
在上面,目的地是SalonDetailFragment,其ID是salonDetailFragment并将数据传递给目的地.

我的导航配置

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mobile_navigation"
    android:label="Salon"
    app:startDestination="@+id/nav_home">
    <fragment
        android:id="@+id/nearbySalonsFragment"
        android:name="my.package.NearbySalonsFragment"
        android:label="Nearby Salons"
        tools:layout="@layout/fragment_nearby_salons" >
        <action
            android:id="@+id/action_nearbySalonsFragment_to_salonDetailFragment"
            app:destination="@id/salonDetailFragment" />
    </fragment>
    <fragment
        android:id="@+id/salonDetailFragment"
        android:name="my.package.SalonDetailFragment"
        android:label="Salon Details"
        tools:layout="@layout/fragment_salon_detail" />
</navigation>

本文地址:https://www.itbaoku.cn/post/1533893.html

问题描述

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?

推荐答案

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)
}

其他推荐答案

I think its better to add this to the accepted answer:

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

其他推荐答案

In your RecyclerView Adapter OnBindViewHolder method you can have it this way:

        holder.bookBtn.setOnClickListener(view -> {
            Bundle bundle = new Bundle();
            String salonJsonString = 
            HelperMethods.getGsonParser().toJson(salonModel);bundle.putString("SALON_DETAILS", salonJsonString);
            Navigation.findNavController(view).navigate(R.id.salonDetailFragment,bundle);     
        });

In the above, the destination is SalonDetailFragment whose id is salonDetailFragment and passing data as a Bundle to the destination.

My Navigation configuration

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mobile_navigation"
    android:label="Salon"
    app:startDestination="@+id/nav_home">
    <fragment
        android:id="@+id/nearbySalonsFragment"
        android:name="my.package.NearbySalonsFragment"
        android:label="Nearby Salons"
        tools:layout="@layout/fragment_nearby_salons" >
        <action
            android:id="@+id/action_nearbySalonsFragment_to_salonDetailFragment"
            app:destination="@id/salonDetailFragment" />
    </fragment>
    <fragment
        android:id="@+id/salonDetailFragment"
        android:name="my.package.SalonDetailFragment"
        android:label="Salon Details"
        tools:layout="@layout/fragment_salon_detail" />
</navigation>
查看更多