Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable RecyclerView when item is clicked

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.

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

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.

like image 701
John Baum Avatar asked Jan 04 '23 09:01

John Baum


1 Answers

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

Sample layout

<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>

Somewhere in the code

    // 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);
like image 71
Cheticamp Avatar answered Jan 18 '23 17:01

Cheticamp