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

时间:2018-07-18 05:09:25

标签: android android-recyclerview android-adapter

我嵌套了RecyclerView,并以此方式禁用了对孩子RecyclerView的点击,并将此类用作孩子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;
    }

我的孩子RecyclerView位于另一个RecyclerView项目中,我想在点击孩子RecyclerView时转到父项RecyclerView项目单击,因此我禁用了孩子{ {1}},但通过这种方式,子RecyclerView无法再滚动,我尝试将子RecyclerView放在RecyclerViewNestedScrollView中 但仍然无法正常工作。

4 个答案:

答案 0 :(得分:2)

要禁用 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 当您要禁用回收站视图项单击时。如果要启用项单击然后设置可见性 View.GONE

要在 Recyclerview 项目单击禁用时滚动:

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

这很完美。如果发现可行,则投票给正确答案。

答案 1 :(得分:0)

尝试一下:

childRecyclerView.setNestedScrollingEnabled(false);

答案 2 :(得分:0)

将大多数父项RecyclerView包装在NestedScrollView中。

<NestedScrollView
 ...>

   <RecyclerView
   .../>

</NestedScrollView>

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

这样,您在Child RecyclerView的滚动中不会有冲突。

只是您需要的技巧

用另一个不可点击的RecyclerView包裹孩子View。像

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

请注意。

  • 父母FrameLayout应该是wrap_content
  • 孩子RecyclerView应该和wrap_content一起nestedScrollingEnabled="false"
  • 父母RecyclerView应该是nestedScrollingEnabled="false"
  • 在孩子View上覆盖RecyclerView应该是match_parent

了解 在这里,我们将nestedScrolling false设置为子RecyclerView。因此,其滚动将被禁用,并且将在父级RecyclerView中使用wrap_content高度。现在,由于我们在其上添加了一个View且不可点击,因此子级RecyclerView将不再可触摸。

答案 3 :(得分:0)

经过一些研究和思考,我做到了: 将我在父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的构造函数适配器,因此,在子级中使用getAdapterPosition则使用在构造函数中获得的位置