SwipeRefreshLayout prevent refresh when not pulling down from the top

718 Views Asked by At

I have a SwipeRefreshLayout embedding a RecyclerView

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
    android:id="@+id/swipeRefreshLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/catalog_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

I'd like it to trigger the refresh only when I start pulling down from the top of the scrollable area.

If I'm scrolling though the top of the content, when I reach the top still pulling down, it's not supposed to chain with the refresh, that's another intention.

How can I prevent the refresh to trigger when I didn't start the pull down from the top of the content ? Is there an option for that or do I need to cancel it from the refresh listener in some way ?

1

There are 1 best solutions below

1
ndriqa On

You can add a listener to the RecyclerView to know when the moving state is DRAGGING or IDLE, and use this to allow the swipeRefresh to happen only if the RV is not DRAGGING by saving the condition in a Boolean (i.e. canRefresh), so you know the list is not scrolling, hence, you can refresh.

You can disable the SwipeRefreshLayout with isEnabled = false, this won't stop the list or its items functionality, just the SwipeRefreshLayout