Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

viewpager2 interaction with SwipeRefreshLayout

I have viewpager2 with 4 fragments. 3 of them have SwipeRefreshLayout to refresh async task data in particular fragments.

When using SwipeRefreshLayout and viewpager2 the gestures are somehow conflicting. ie. swype down to refresh makes screen so sensitive, that a little move to left or right also makes page screen change and refresh icon is freezing or the processis unfinished.

my goal is to make gestures independent, so for example when i start to swype down SwipeRefreshLayout, then vp2 is disabled so it it is not interfere with SRL.

This was not happening when using standard viewpager with SwipeRefreshLayout, gestures were not conflicting, but I need to use "setUserInputEnabled" in VP2. any idea how to mitigate this behaviour and should i mitigate it at SwipeRefreshLayout level or within viepager2 code?

like image 905
B.Ondrej Avatar asked Aug 03 '19 07:08

B.Ondrej


People also ask

What is SwipeRefreshLayout?

Android SwipeRefreshLayout is a ViewGroup that can hold only one scrollable child. It can be either a ScrollView, ListView or RecyclerView. The basic need for a SwipeRefreshLayout is to allow the users to refresh the screen manually.

How to stop SwipeRefreshLayout in Android?

If an activity wishes to show just the progress animation, it should call setRefreshing(true). To disable the gesture and progress animation, call setEnabled(false) on the view.

How do you use SwipeRefreshLayout?

Add the SwipeRefreshLayout Widget To add the swipe to refresh widget to an existing app, add SwipeRefreshLayout as the parent of a single ListView or GridView . Remember that SwipeRefreshLayout only supports a single ListView or GridView child. You can also use the SwipeRefreshLayout widget with a ListFragment .


2 Answers

EDIT: Update as 1.1.0 has been released on July 22, 2020

The problem was due to a bug in SwipeRefreshLayout, which has been resolved by Version 1.1.0. To use it, just upgrade to that version by adding the following line in the dependencies of your Gradle file:

implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'

See issueTracker for the bug history: [ViewPager2] SwipeRefreshLayout should not ignore requestDisallowInterceptTouchEvent. Note that there's also a workaround described there (extending SwipeRefreshLayout and overriding "requestDisallowInterceptTouchEvent").

like image 166
FlorianT Avatar answered Oct 15 '22 06:10

FlorianT


It looks problem is resolved when I added to my scrollview:

android:nestedScrollingEnabled="true"

Final code of the fragment layout then looks like this:

<?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/some_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/activity_background"
tools:context="some_fragment_name">

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true"
    android:nestedScrollingEnabled="true" <<<<------ HERE the change
    android:id="@+id/some_id">

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/sensors_relative_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context="some_package_name">

    <TextView
        android:id="@+id/some_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:background="@android:color/white"
        android:gravity="center"
        android:textColor="@android:color/black"
        android:textStyle="bold"
        android:layout_marginTop="5sp"
        android:layout_marginLeft="5sp"
        android:layout_marginRight="5sp"
        android:textSize="20sp" />

...

like image 22
B.Ondrej Avatar answered Oct 15 '22 04:10

B.Ondrej