简体   繁体   English

如何禁用 RecyclerView 项目单击

[英]How to disable RecyclerView Items from clicking

I am using Floating Action Button.我正在使用浮动操作按钮。 I want to disable Recyclerview Items from Clicking when i press FAB button.当我按下 FAB 按钮时,我想禁用 Recyclerview 项目的点击。 I tried this method but not working setClickable(true);我尝试了这种方法,但不起作用setClickable(true);

My Layout我的布局

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

You can add a simple boolean to your adapter like this:您可以像这样向适配器添加一个简单的布尔值:

public boolean isClickable = true;

and set it in your fab-click:并将其设置在您的 fab-click 中:

mAdapter.isClickable = true/false;

And within your OnClickListener in the Adapter, only act when it is clickable:在适配器中的 OnClickListener 中,仅在可单击时执行:

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

To disable RecyclerView, follow below steps:要禁用 RecyclerView,请按照以下步骤操作:

1. Add following view into your layout file, 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. Set View Visibility `View.VISIBLE when you want to disable RecyclerView else 2.设置View Visibility `View.VISIBLE when you want to disable RecyclerView else

You can simply use recursion to disable/enable clicks on view您可以简单地使用递归来禁用/启用视图点击

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

Björn Kechel's answer helps me. Björn Kechel 的回答对我有帮助。 As he said I just added Boolean.正如他所说,我刚刚添加了布尔值。 When i click the fab menu the boolean is activated.当我单击 fab 菜单时,布尔值被激活。 Then have to write the condition on mrecyclerview.addOnItemTouchListener然后必须在mrecyclerview.addOnItemTouchListener上写条件
Java Class 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);
                }
            }

You need to set the click listener to every FloatingActionButton .您需要将点击侦听器设置为每个FloatingActionButton

see this issue onlibrary图书馆看到这个问题

Working solution with RecyclerView.OnItemTouchListener:使用 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)
        }
    }
}

You can disable the touch of recyclerview您可以禁用recyclerview的触摸

recyclerView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    return true;
                }
            });
  1. In the xml file, set the layout_width and layout_height for FloatingActionMenu as match_parent and set clickable as false :在 xml 文件中,将FloatingActionMenu的 layout_width 和 layout_height 设置为 match_parent 并将 clickable 设置为 false :

     android:layout_width="match_parent " android:layout_height="match_parent " android:clickable="false"
  2. In your java class,在你的java类中,

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

This should work.这应该有效。

I solved this problem with very simple logic.我用非常简单的逻辑解决了这个问题。 This will prevent double click on single item and multiple items of RecyclerView as well.这将防止双击RecyclerView的单个项目和多个项目。

Declare a Variable in your Activity.在您的活动中声明一个变量。

private long mLastClickTime = 0;

Then use in any OnClickListener .然后在任何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

}

Actually I found this solution in stackover flow when I was searching for preventing double click on Button.实际上,当我搜索防止双击按钮时,我在 stackover flow 中找到了这个解决方案。 I'm writing this line to acknowledge that actual answer is posted by someone ( Unfortunatly I'm unable to find that answer to link his/her answer here.)我写这行是为了承认实际答案是由某人发布的(不幸的是,我无法找到该答案来链接他/她的答案。)

Hope this will solve your problem.:)希望这能解决你的问题。:)

reyclerView.setOnTouchListener { v, event -> true }

此解决方案禁用所有触摸事件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM