如何禁用 RecyclerView 项目的点击

问题描述 投票:0回答:11

我正在使用浮动操作按钮。我想在按 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);
                    }
                }
            }
        });
android android-recyclerview floating-action-button
11个回答
59
投票

您可以向适配器添加一个简单的布尔值,如下所示:

public boolean isClickable = true;

并将其设置在您的 fab 中 - 单击:

mAdapter.isClickable = true/false;

并且在适配器的 OnClickListener 中,仅在可点击时才起作用:

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

25
投票

要禁用 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.当你想禁用 RecyclerView 时,设置视图可见性 `View.VISIBLE


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

2
投票

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

1
投票

您需要为每个

FloatingActionButton
设置点击监听器。

图书馆

查看此问题

1
投票

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

1
投票

Java

recyclerView.setOnTouchListener((view1, motionEvent) -> true );

科特林

reyclerView.setOnTouchListener { v, event -> true }

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


0
投票

您可以禁用recyclerview的触摸

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

0
投票
  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);
                }
            }
        });
    

这应该有效。


0
投票

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

RecyclerView
的多个项目。

在您的 Activity 中声明一个变量。

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 中找到了这个解决方案。我写这行是为了确认实际答案是由某人发布的(不幸的是,我无法找到该答案来链接他/她的答案。)

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


0
投票
yourRecycelerview.suppresslayout(true)

干净、简单,无需项目触摸侦听器或视图持有者逻辑即可工作。

文档:

/**
 * Tells this RecyclerView to suppress all layout and scroll calls until layout
 * suppression is disabled with a later call to suppressLayout(false).
 * When layout suppression is disabled, a requestLayout() call is sent
 * if requestLayout() was attempted while layout was being suppressed.
 * <p>
 * In addition to the layout suppression {@link #smoothScrollBy(int, int)},
 * {@link #scrollBy(int, int)}, {@link #scrollToPosition(int)} and
 * {@link #smoothScrollToPosition(int)} are dropped; TouchEvents and GenericMotionEvents are
 * dropped; {@link LayoutManager#onFocusSearchFailed(View, int, Recycler, State)} will not be
 * called.
 *
 * <p>
 * <code>suppressLayout(true)</code> does not prevent app from directly calling {@link
 * LayoutManager#scrollToPosition(int)}, {@link LayoutManager#smoothScrollToPosition(
 *RecyclerView, State, int)}.
 * <p>
 * {@link #setAdapter(Adapter)} and {@link #swapAdapter(Adapter, boolean)} will automatically
 * stop suppressing.
 * <p>
 * Note: Running ItemAnimator is not stopped automatically,  it's caller's
 * responsibility to call ItemAnimator.end().
 *
 * @param suppress true to suppress layout and scroll, false to re-enable.
 */
© www.soinside.com 2019 - 2024. All rights reserved.