如何禁用RecyclerView中的条目点击

33

我正在使用浮动操作按钮(Floating Action Button)。当我按下FAB按钮时,我想禁用Recyclerview中的项目点击。我尝试了这种方法但不起作用 setClickable(true);

我的布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:fab="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="#fff"
    tools:context="com.hartwintech.socialchat.activity.IconTabsActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        >

    </android.support.v7.widget.RecyclerView>

    <com.github.clans.fab.FloatingActionMenu
        android:id="@+id/floatmenu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="60dp"
        android:layout_marginRight="16dp"
        fab:fab_showAnimation="@anim/show_from_bottom"
        fab:fab_hideAnimation="@anim/hide_to_bottom"
        fab:menu_labels_style="@style/MenuLabelsStyle"
        fab:menu_shadowColor="#444"
        fab:menu_colorNormal="#FFB805"
        fab:menu_colorPressed="#F2AB00"
        fab:menu_colorRipple="#D99200"/>

</RelativeLayout>

Java类

floatMenu.setOnMenuToggleListener(new FloatingActionMenu.OnMenuToggleListener() {
            @Override
            public void onMenuToggle(boolean opened) {
                if (opened) {
                    final int color = R.color.transp;
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        mrecyclerview.setClickable(false);
                        mrecyclerview.setEnabled(false);
                        mrecyclerview.setForeground(new ColorDrawable(ContextCompat.getColor(getContext(), color)));
                    }
                } else {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        mrecyclerview.setClickable(true);
                        mrecyclerview.setEnabled(true);
                        mrecyclerview.setForeground(null);
                    }
                }
            }
        });

你想禁用还是启用?setClickable(true);将使其可点击。 - N J
我想要禁用。 - Dev Tamil
2
试过了兄弟。它还没有禁用。 - Dev Tamil
你能发布你的布局吗? - N J
除了mrecyclerview.setEnabled(false);之外,您还需要在适配器的监听器的OnClick事件中检查if (mrecyclerview.isEnabled() == false) return; - ZP007
显示剩余2条评论
11个回答

59

你可以像这样向适配器添加一个简单的布尔值:

public boolean isClickable = true;

然后将其设置在您的fab-click中:

mAdapter.isClickable = true/false;

在适配器中的OnClickListener中,只有在它可点击时才执行操作:

public void onClick(View view) {
    if(!isClickable)
        return;
    // do your click stuff
}

25
为了禁用RecyclerView,请按照以下步骤进行操作:

1.将以下视图添加到您的布局文件中,
        <View
            android:id="@+id/viewDisableLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#40000000"
            android:clickable="true"
            android:focusable="true"
            android:visibility="gone"/>

2. 当你想要禁用RecyclerView时,请将View可见性设置为`View.VISIBLE`。


16
干得好,@Rajan!你做了最出色的肮脏手段,但却奏效了。 - Skizo-ozᴉʞS ツ
1
不错!我自己尝试过,但是漏掉了android:clickable="true" android:focusable="true"这部分!谢谢天才! :) - Mohamed Hamdaoui
1
对我来说不起作用。RecyclerView项仍然接收点击事件。 - iCantC
1
用户仍然可以通过移动焦点来访问RecyclerView中的项,因此这不是100%的解决方案。 - Stan

3
您可以简单地使用递归来禁用/启用视图上的点击。
 public static void setClickable(View view, boolean clickable) {
        if (view != null) {
            if (view instanceof ViewGroup) {
                ViewGroup viewGroup = (ViewGroup) view;
                for (int i = 0; i < viewGroup.getChildCount(); i++) {
                    setClickable(viewGroup.getChildAt(i), clickable);
                }
            }
            view.setClickable(clickable);
        }
    }

2

Björn Kechel的回答对我很有帮助。就像他说的那样,我只需添加Boolean。当我点击fab菜单时,布尔值被激活。然后必须在mrecyclerview.addOnItemTouchListener上编写条件。
Java类

    public Boolean fabClick = false;

    floatMenu.setOnMenuToggleListener(new FloatingActionMenu.OnMenuToggleListener() {
                @Override
                public void onMenuToggle(boolean opened) {
                    if (opened) {
                        final int color = R.color.transp;
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                            fabClick = true;
                            mrecyclerview.setClickable(false);
                            mrecyclerview.setEnabled(false);
                            mrecyclerview.setForeground(new ColorDrawable(ContextCompat.getColor(getContext(), color)));
                        }
                    } else {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {                               
                            fabClick = false;
                            mrecyclerview.setClickable(true);
                            mrecyclerview.setEnabled(true);
                            mrecyclerview.setForeground(null);
                        }
                    }
                }
            });


            mrecyclerview.addOnItemTouchListener(new RecyclerTouchListener(getActivity(), mrecyclerview, new RecyclerTouchListener.ClickListener() {
            @Override
            public void onClick(View view, int position) {
                if(!fabClick) {
                    android.support.v4.app.FragmentTransaction ft = getFragmentManager().beginTransaction();
                    ft.setCustomAnimations(R.anim.fragment_anim_start, R.anim.fragment_anim_stop);
                    Intent i = new Intent(getActivity(), Group_Chat_Screen.class);
                    startActivity(i);
                }
            }

1

您需要为每个FloatingActionButton设置点击监听器。

请查看此问题的library


问题与FloatingActionButton有关。但我想在单击FloatingActionMenu时禁用点击recyclerview。 - Dev Tamil
1
然后,您可以将视图放在“RecyclerVIew”上,将其设置为“消失”,并不要忘记在重叠的视图上添加“setClickable =“true””。 - N J
1
我已经发布了答案,兄弟。谢谢你的支持。 - Dev Tamil

1

Java

recyclerView.setOnTouchListener((view1, motionEvent) -> true );

Kotlin

reyclerView.setOnTouchListener { v, event -> true }

这个解决方案禁用了所有的触摸事件。


1

使用RecyclerView.OnItemTouchListener的有效解决方案:

@SuppressLint("ClickableViewAccessibility")
@BindingAdapter("itemsClickable")
fun setRecyclerViewClickable(view: RecyclerView, clickable: Boolean) {
    view.isEnabled = clickable
    if (!clickable) {
        val itemTouchListener = object : RecyclerView.OnItemTouchListener {
            override fun onTouchEvent(rv: RecyclerView?, e: MotionEvent?) {

            }

            override fun onInterceptTouchEvent(rv: RecyclerView?, e: MotionEvent?): Boolean {
                return rv?.isEnabled == false
            }

            override fun onRequestDisallowInterceptTouchEvent(disallowIntercept: Boolean) {

            }

        }
        view.addOnItemTouchListener(itemTouchListener)
        view.tag = itemTouchListener
    } else {
        (view.tag as? RecyclerView.OnItemTouchListener)?.let {
            view.requestDisallowInterceptTouchEvent(true)
            view.removeOnItemTouchListener(it)
        }
    }
}

0
  1. 在xml文件中,将的layout_width和layout_height设置为match_parent,并将clickable设置为false:

        android:layout_width="match_parent "
        android:layout_height="match_parent "
        android:clickable="false"
    
  2. 在您的Java类中,

    floatMenu.setOnMenuToggleListener(new FloatingActionMenu.OnMenuToggleListener() {
            @Override
            public void onMenuToggle(boolean opened) {
                if (opened) {
                   floatMenu.setClickable(true);
                } else {
                     floatMenu.setClickable(false);
                }
            }
        });
    

这应该可以工作。


0

我用非常简单的逻辑解决了这个问题。这将防止在单个条目和多个RecyclerView上双击。

在您的Activity中声明一个变量。

private long mLastClickTime = 0;

然后在任何OnClickListener中使用。

@Override
public void onClick(View v) {
    if(SystemClock.elapsedRealtime() - mLastClickTime < 1000){//You can reclick after 1 second
        return;//Before 1 seconds from first click this onclick will return from here
    }
    mLastClickTime = SystemClock.elapsedRealtime();
    
    //Do stuff here

}

实际上,当我在寻找如何防止按钮双击时,我在Stack Overflow上找到了这个解决方案。我写这行文字是为了承认真正的答案是由某人发布的(不幸的是,我无法找到那个答案来在此处链接他/她的答案)。

希望这能解决你的问题。:)


0

您可以禁用RecyclerView的触摸功能

recyclerView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    return true;
                }
            });

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接