如何禁用 RecyclerView 单击侦听器但滚动工作

how can disable RecyclerView click listener but scroll working

我已经嵌套了 RecyclerView 并用这种方式禁用了我的 child RecyclerView 的点击,并使用这个 class 作为我的 child RecyclerView.

public class MyDisabledRecyclerView extends RecyclerView {
    public MyDisabledRecyclerView(Context context) {
        super(context);
    }

    public MyDisabledRecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MyDisabledRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean onTouchEvent(MotionEvent e) {
        return false;
    }

我的 child RecyclerView 在另一个 RecyclerView 项目中,我想在单击 child RecyclerView 时转到 parent RecyclerView 项目点击,所以我禁用 child RecyclerView 的触摸事件但是用这种方式 child RecyclerView 不能再滚动了,我尝试把 child RecyclerViewNestedScrollViewrecyclerView.setNestedScrollingEnabled(false); 内 但还是不行。

试试这个:

childRecyclerView.setNestedScrollingEnabled(false);

将大部分 parent RecyclerView 包裹在 NestedScrollView.

<NestedScrollView
 ...>

   <RecyclerView
   .../>

</NestedScrollView>

并在所有 RecyclerView 上添加 recyclerView.setNestedScrollingEnabled(false);

这样你就不会在 Child RecyclerView 中滚动时发生冲突。

只是满足您的要求

用另一个不可点击的 View 包裹 Child RecyclerView。喜欢

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:nestedScrollingEnabled="false"
        android:layout_height="wrap_content" />

    <View
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="false"/>
</FrameLayout>

注意.

  • Parent FrameLayout 应该是 wrap_content.
  • Child RecyclerView 应该是 wrap_contentnestedScrollingEnabled="false".
  • ParentRecyclerView应该是nestedScrollingEnabled="false"
  • 在 child RecyclerView 上叠加 View 应该是 match_parent

了解 这里我们将 nestedScrolling false 设置为 child RecyclerView。因此它的滚动将被禁用,并且它将在 parent RecyclerView 中占用 wrap_content 高度。现在因为我们在上面添加了一个 View 和 non-clickable 所以 child RecyclerView 将不再是可触摸的。

经过一些研究和思考,我这样做了: 将我在父 RecyclerView 中需要的所有内容传递给具有如下位置的子 RecyclerView 适配器构造函数:

   @Override
    public void onBindViewHolder(final ChatList_Adapter.ViewHolder holder, final int position) {

        holder.name.setText(chanelsList.get(i).getUser().getFirstname()+" "+chanelsList.get(i).getUser().getLastname());

        holder.city_recycler_hosts.setHasFixedSize(true);
        LinearLayoutManager layoutManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, true);
        holder.city_recycler_hosts.setLayoutManager(layoutManager);
        holder.city_recycler_hosts.addItemDecoration(new VerticalDividerItemDecoration.Builder(context).color(ContextCompat.getColor(context,R.color.lightergray)).margin(0,20).
                build());

        CityHosts_Adapter adapter=new CityHosts_Adapter(chanelsList.get(i).getUser().getCities(),chanelsList,file,position,pusher
        ,auth,message_ist);
        holder.city_recycler_hosts.setAdapter(adapter);
}

然后在子 RecyclerView 中执行我们在父 RecyclerView click 中所做的操作,但是我们将父 RecyclerView 的位置传递给子 RecyclerView 的构造函数适配器,因此在子 RecyclerView 中使用 getAdapterPosition 我们使用在构造函数中获得的位置 getAdapterPosition =12=]

要禁用 RecyclerView 项目,请单击:

将以下视图添加到您的布局文件中以屏蔽视图层次结构的顶部。

 <View
     android:id="@+id/recyclerViewDisable"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:background="@color/transparent"
     android:clickable="true"
     android:focusable="true"
     android:visibility="gone"/>

设置此视图可见性 View.VISISBLE 当您想要禁用 recyclerview 项目时 click.If 您想要启用项目点击然后设置可见性 View.GONE

单击禁用 Recyclerview 项目时可滚动:

recyclerViewDisable.setOnTouchListener((view, motionEvent) -> {
            recyclerview.onTouchEvent(motionEvent);
            return true;
        });

这很完美。如果发现可行,请投票选出正确答案。