11

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

Mick
  • 811
  • 14
  • 31
SimpleAndroid
  • 281
  • 2
  • 8

2 Answers2

17

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
}
SimpleAndroid
  • 281
  • 2
  • 8
  • 6
    thanks, it works! unfortunately there is other bug appears: when open bottom sheet and swipe to the next page, the recyclerview in that page becomes unscrollable until i move the bottom sheet down at least for few pixels – deviant Jul 27 '20 at 10:38
1

BottomSheetBehaviour only detects the first scrollable view. So it is always recommended to use only one scrollable view inside of it.

For More information check this answer bottomsheetbehavior-with-two-recyclerview

And this one also Scroll not working for multiple RecyclerView in BottomSheet

If you really want to have the two scrollable views I recommend you to take a look at this library also AndroidSlidingUpPanel

AgentP
  • 6,261
  • 2
  • 31
  • 52
  • thank you very much for your reply, this was helpful for me research. I've found another workaround for my case: I set `isNestedScrollingEnabled = false` for inner RecyclerView in ViewPager so that BottomSheetBehavior finds another scrolling views. – SimpleAndroid May 17 '20 at 19:11