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

Viewed 1137

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

3 Answers

SwipeRefreshLayout should not be the blocker for RecyclerView item to receive click event, can you also post your itemClickListener implementation?

Also, consider using FrameLayout for your above layout requirements.

Use latest stable version of Constraint Layout. I have same problem because I am using old version.

dependencies {
    implementation "androidx.constraintlayout:constraintlayout:2.0.4"
}

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