【问题标题】:How to disable RecyclerView Items from clicking如何禁用 RecyclerView 项目单击
【发布时间】:2017-06-09 07:53:01
【问题描述】:

我正在使用浮动操作按钮。当我按下 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); 将使其可点击
  • 我想禁用
  • 试试mrecyclerview.setEnabled(false);
  • 试过了兄弟。它没有禁用
  • 你能发布你的布局吗

标签: android android-recyclerview floating-action-button


【解决方案1】:

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

public boolean isClickable = true;

并在您的 fab-click 中设置它:

mAdapter.isClickable = true/false;

并且在 Adapter 中的 OnClickListener 中,仅在可点击时执行:

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

【讨论】:

    【解决方案2】:

    要禁用 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。设置 View Visibility `View.VISIBLE 当你想禁用 RecyclerView else

    【讨论】:

    • 打得很好@Rajan,你做了有史以来最肮脏的事情,但它奏效了。
    • 不错!我自己尝试过,但缺少 ``` android:clickable="true" android:focusable="true" ``` 部分!谢谢天才! :)
    • 对我不起作用。 RecyclerView 项目仍在接收点击事件。
    • 用户仍然可以到达 RecyclerView 项目移动焦点,所以它不是 100% 的解决方案
    【解决方案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);
            }
        }
    

    【讨论】:

      【解决方案4】:

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

      library 上查看此问题

      【讨论】:

      • 该问题显示与 FloatingActionButton 相关。但我想禁用点击 FloatingActionMenu 上的 recyclerview 点击
      • 然后您可以将视图放在RecyclerVIew 上使其消失,并且不要忘记在该重叠视图上添加setClickable="true"
      • 我已经发布了答案兄弟。感谢您的支持。
      【解决方案5】:

      Björn Kechel 的回答对我有帮助。正如他所说,我刚刚添加了布尔值。当我单击 fab 菜单时,布尔值被激活。然后必须在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);
                      }
                  }
      

      【讨论】:

        【解决方案6】:

        使用 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)
                }
            }
        }
        

        【讨论】:

          【解决方案7】:

          你可以禁用recyclerview的触摸

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

          【讨论】:

            【解决方案8】:
            1. 在xml文件中,将FloatingActionMenu的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);
                          }
                      }
                  });
              

            这应该可行。

            【讨论】:

              【解决方案9】:

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

              在你的活动中声明一个变量。

              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
              
              }
              

              实际上,当我在搜索防止双击按钮时,我在 stackover flow 中找到了这个解决方案。我写这行是为了承认实际答案是由某人发布的(不幸的是,我无法找到该答案来链接他/她的答案。)

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

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2023-03-13
                • 2019-12-01
                • 2013-03-30
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多