Bottomsheet with Viewpager2 on Android

Viewed 632

I am developing an app that uses a vertical bottom sheet. To be more specific, it is a LinearLayout with bottomSheetBehavior. This bottom sheet comprises two elements: a view pager that displays images and can be scrolled horizontally, and, below the view pager, a Recycler View whose elements can be scrolled vertically. The good layout file looks like this:

<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="?android:attr/colorBackground"
    android:orientation="vertical">

    <!--        works with and without frame layout-->
    <FrameLayout
        android:id="@+id/play_queue_square_frame"
        android:layout_width="match_parent"
        android:layout_height="350dp">
        <androidx.viewpager.widget.ViewPager
            android:id="@+id/play_queue_view_pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </FrameLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/play_queue_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="0dp"
        android:layout_marginRight="0dp"
        app:layoutManager="LinearLayoutManager"
        android:background="?android:attr/colorBackground"
        tools:listitem="@layout/item_view_song_debug" />


</LinearLayout>
</androidx.core.widget.NestedScrollView>

With the above layout everything is working as expected. In particular, the view pager scrolls in horizontal direction, the recycler view scrolls in vertical direction, and dragging either the view pager or the recycler view in horizontal direction closes the bottom sheet.

However, when replacing the view pager by androidx.viewpager2.widget.ViewPager2 yields odd behaviour. Scrolling the view pager in horizontal direction is still possible, but vertical scrolling does no longer work. Any swipes downward simply close the bottom sheet. Using a NestedScrollView yields better results but still has erroneous behaviour.

<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="?android:attr/colorBackground"
    android:orientation="vertical">

    <FrameLayout
        android:id="@+id/play_queue_square_frame"
        android:layout_width="match_parent"
        android:layout_height="300dp">

        <androidx.viewpager2.widget.ViewPager2
            android:id="@+id/play_queue_view_pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </FrameLayout>


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/play_queue_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="0dp"
        android:layout_marginRight="0dp"
        app:layoutManager="LinearLayoutManager"
        android:background="?android:attr/colorBackground"
        tools:listitem="@layout/item_view_song_debug" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>

Scrolling in vertical direction is still possible, and also the horizontal scrolling for the view pager. However, dragging the view pager down does no longer close the bottom sheet, whereas a dragging the recycler view down does. If I add the following code

viewPager.setUserInputEnabled(false)

the view pager does no longer allow horizontal scrolling (as expected). So my question is simply what I have to do in order to allow horizontal scrolling and make sure that a vertical swipe downwards (on the view pager) closes the bottom sheet.

I know that a somewhat similar question has already been asked (in this post)

but as there is no answer so far I take the liberty to ask this question. What confuses me most is that it is working with the old viewpager but not with viewpager2.

Any help is greatly appreciated!

0 Answers
Related