How to disable RecyclerView Items from clicking

随声附和 提交于 2019-12-03 11:59:41

You can add a simple boolean to your adapter like this:

public boolean isClickable = true;

and set it in your fab-click:

mAdapter.isClickable = true/false;

And within your OnClickListener in the Adapter, only act when it is clickable:

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

To disable RecyclerView, follow below steps:

1. Add following view into your layout file,

        <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

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

You need to set the click listener to every FloatingActionButton.

see this issue on library

Björn Kechel's answer helps me. As he said I just added Boolean. When i click the fab menu the boolean is activated. Then have to write the condition on mrecyclerview.addOnItemTouchListener
Java Class

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

Working solution with 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.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    return true;
                }
            });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!