androidandroid-recyclerviewonclicklistenerswiperefreshlayout

Recycler View Item Click not working if its put under Swipe Refresh Layout - Android


The recycler view item clicks not working if I put recycler view inside swipe refresh layout. adding the layout below.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/tv_no_order"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="90dp"
    android:layout_marginEnd="90dp"
    android:gravity="center_horizontal"
    android:lineSpacingExtra="6sp"
    android:text="@string/no_orders_found"
    android:textColor="#616161"
    android:textSize="16sp"
    android:visibility="gone"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<include
    android:id="@+id/progressView"
    layout="@layout/layout_progress"
    android:visibility="gone"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
    android:id="@+id/swipe_refresh_layout"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0"
    android:clickable="false">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rcv_inventory"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true"
        android:padding="@dimen/padding_15dp"
        android:focusable="true" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

The clicks are working when I remove the swipe refresh layout. How to make recycler view clickable without removing the swipe refresh layout?

More Info: This layout is part of a fragment and the fragment is used as a page of view pager 2. I have two view types in Recycler View adapter


Solution

  • try replaceConstraintLayout with LinearLayout

    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <TextView
        android:id="@+id/tv_no_order"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="90dp"
        android:layout_marginEnd="90dp"
        android:gravity="center_horizontal"
        android:lineSpacingExtra="6sp"
        android:text="@string/no_orders_found"
        android:textColor="#616161"
        android:textSize="16sp"
        android:visibility="gone"
      
     />
    
    <include
        android:id="@+id/progressView"
        layout="@layout/layout_progress"
        android:visibility="gone"
       />
    
    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/swipe_refresh_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"   
        >
    
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rcv_inventory"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="@dimen/padding_15dp"
            />
    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
    
    </LinearLayout>