android - 如何禁用 RecyclerView 项目单击

标签 android android-recyclerview floating-action-button

我正在使用 float 操作按钮。当我按下 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);
                    }
                }
            }
        });

最佳答案

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

public boolean isClickable = true;

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

mAdapter.isClickable = true/false;

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

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

关于android - 如何禁用 RecyclerView 项目单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41829485/

相关文章:

java - Dagger 可以在不注入(inject)子 Activity 类的情况下注入(inject)抽象 Activity 类吗?

java - SAX 解析器上的文档意外结束(怀疑输入流多次使用)

java - 使用 FirebaseRecyclerAdapter 将 Firebase 实时数据库中的数据检索到 Recycler View 中

android - 如何正确实现 Snackbar、Coordinator Layout 和 bottom|right FAB?

flutter - 如何在 Flutter 中隐藏 float 操作按钮

android - 始终在后台以 root 身份在 android 上运行服务

android - 无法展开 RemoteViews

android - RecyclerView 适配器不适用于 butterknife

android - RecyclerView 展开/折叠项目

Android 全屏阻止 View ,如 Google Calendar FAB