单击项目时禁用RecyclerView [英] Disable RecyclerView when item is clicked

查看:83
本文介绍了单击项目时禁用RecyclerView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个回收站视图,其中有一个简单的复选框,用于适配器中的每个条目.我在RecyclerView下方还有一个完成"按钮,单击该按钮可启动API调用,其中包含用户已检出的所有项目.问题在于,在api调用返回(自然)到活动结束之间存在一些延迟,如果用户足够快的话,允许用户点击任意复选框.

I have a recycler view with a simple Checkbox for each entry in the adapter. I also have a Done button below the RecyclerView that, when clicked, launches an API call with all the items the user has checked off. The issue is that there is a slight delay between the time the api call returns (naturally) and when the activity finishes, allowing the user to tap any of the checkboxes if they are quick enough.

有没有一种方法可以禁用RecyclerView的触摸事件以防止这种情况发生?我尝试了一个回调,当单击完成按钮无效时,它将执行以下操作:

Is there a way to disable the touch events for a RecyclerView to prevent this? I have tried a callback that does the following when the done button is clicked to no effect:

mRecyclerViewList.setClickable(false);
mRecyclerViewList.setEnabled(false);

我不想使用onBindViewHolder来实际禁用每个项目,因为那将意味着重新加载整个RecyclerView以强制执行该方法.

I don't want to use the onBindViewHolder to actually disable each item because that would mean reloading the entire RecyclerView to force that method to execute.

推荐答案

在RecyclervView上方放置一个空白的可单击透明视图以捕获点击.当您的API返回时,将可见性设置为已消失".

Place a blank, clickable transparent view over the RecyclervView to capture clicks. When your API returns, set visibility to "gone".

示例布局

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/frameLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="50dp"
    android:background="#FFFFFFFF"
    android:orientation="horizontal">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        android:paddingBottom="16dp"
        android:paddingTop="16dp" />

    <View
        android:id="@+id/overlay"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true"
        android:visibility="gone" />
</FrameLayout>

代码中的某处

    // Block clicks on RecyclerView. overlay will consume clicks.
    findViewById(R.id.overlay).setVisibility(View.VISIBLE);

    // Re-enable clicks on RecyclerView
    findViewById(R.id.overlay).setVisibility(View.GONE);

这篇关于单击项目时禁用RecyclerView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆