How to disable RecyclerView Items from clicking

后端 未结 9 825
迷失自我
迷失自我 2020-12-31 04:00

I am using Floating Action Button. I want to disable Recyclerview Items from Clicking when i press FAB button. I tried this method but not working setClickable(true);<

相关标签:
9条回答
  • 2020-12-31 04:27

    You can disable the touch of recyclerview

    recyclerView.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        return true;
                    }
                });
    
    0 讨论(0)
  • 2020-12-31 04:27

    I solved this problem with very simple logic. This will prevent double click on single item and multiple items of RecyclerView as well.

    Declare a Variable in your Activity.

    private long mLastClickTime = 0;
    

    Then use in any 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. 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.:)

    0 讨论(0)
  • 2020-12-31 04:33

    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);
                    }
                }
    
    0 讨论(0)
  • 2020-12-31 04:34

    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

    0 讨论(0)
  • 2020-12-31 04:42

    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)
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-31 04:43

    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
    }
    
    0 讨论(0)
提交回复
热议问题