Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BottomSheetBehavior with ViewPager2 can't be scrolled down by nested RecyclerView scroll

I have a view that acts like BottomSheetBehavior and this view has ViewPager2 inside. Each ViewPager2's page is a vertical RecyclerView. The issue is that BottomSheet doesn't scroll down when current vertical RecyclerView (which is a page of ViewPager) can't scroll vertically anymore. Everything works file when instead of ViePager I have only one vertical RecyclerView.

The temporary solution is to wrap ViewPager with NestedScrollView but it's horrible for performance and has it's own bugs.

The original layout:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.coordinatorlayout.widget.CoordinatorLayout  
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/core"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#C7C7C7"
        tools:context=".MainActivity">

        <LinearLayout
            android:id="@+id/bottom_sheet"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#FFFFFF"
            android:elevation="8dp"
            android:orientation="vertical"
            app:behavior_hideable="true"
            app:behavior_peekHeight="300dp"
            app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">

            <com.google.android.material.tabs.TabLayout
                android:id="@+id/tab_layout"
                android:layout_width="wrap_content"
                android:layout_height="24dp"
                android:layout_gravity="center_horizontal"
                app:tabGravity="center"
                app:tabMode="scrollable" />

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

        </LinearLayout>

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

[Here's how it looks (sorry for the gif quality)]

enter image description here

like image 792
SimpleAndroid Avatar asked Apr 27 '20 17:04

SimpleAndroid


1 Answers

I've found a solution for this case, I set isNestedScrollingEnabled = false for inner RecyclerView so that BottomSheetBehavior finds another scrolling view

viewPager.children.find { it is RecyclerView }?.let {
        (it as RecyclerView).isNestedScrollingEnabled = false
}
like image 117
SimpleAndroid Avatar answered Nov 04 '22 21:11

SimpleAndroid