androidandroid-viewpagerandroid-viewpager2swiperefreshlayout

Stop ViewPager scroll when SwipeRefreshLayout child is triggered


I'm developing a view with ViewPager2 (horizontal scroll) who handle pages with SwipeRefreshLayout.

My problem is : When I scroll horizontally everything is ok but when I scroll vertically, the SwipeRefreshLayout start to appear and the ViewPager can still scroll horizontally. So that make the usage weird.

I'm looking for disabled the swipe on ViewPager2 when SwipeToRefresh consume the gesture.

Hierarchy of activity with ViewPager

<ConstraintLayout >

    <AppBarLayout>
        <MaterialToolbar/>
    <AppBarLayout>

    <ViewPager2
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@id/app_bar_layout"/>

</ConstraintLayout>

Hierarchy of a page

<SwipeRefreshLayout>

    <ConstraintLayout>

        <TextView/>
        <LinearLayout/>
        <LinearLayout/>

    </ConstraintLayout>

</SwipeRefreshLayout>

I tried to implement MultiSwipeRefreshLayout whithout success...

Thank's for your help.


Solution

  • I found a solution

    It's to wrap the child (in my case ConstraintLayout) of the SwipeRefreshLayout in a NestedScrollView with the parameter android:fillViewport at true.

    <SwipeRefreshLayout
     android:layout_width="match_parent"
     android:layout_height="match_parent">
    
        <NestedScrollView
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:fillViewport="true">
    
            <ConstraintLayout>
    
                <TextView/>
                <LinearLayout/>
                <LinearLayout/>
    
            </ConstraintLayout>
    
        <NestedScrollView/>
    
    </SwipeRefreshLayout>
    

    Dont know if it's a bad solution but for the moment it work like a charm...