android-bottomsheet-viewpager

A sample app to demonstrate a solution to put ViewPager on extentable bottom sheet. The core class is BottomSheetViewPager.

Screen record

The scrolling issue of ViewPager on bottom sheet

BottomSheetBehavior supports only single scrollable child view. If there is a ViewPager and it has multiple scrollable views, user can not scroll them as expected.

See following Stack Overflow threads for more details.

My solution

The workaround is implemented by BottomSheetViewPager. Scroll should be performed as expected by using this class instead of standard ViewPager.

BottomSheetBehavior uses findScrollingChild(View) to find scrollable child. BottomSheetViewPager overrites getChildAt(Int) to return displayed page's view if the caller is BottomSheetBehavior.findScrollingChild(View).

Proguard/R8

This solution uses the names of ViewPager.LayoutParams#position and BottomSheetBehavior#findScrollingChild(). You need to protect them from obfuscation and shrink with following ProGuard rules if you enable ProGuard or R8.

-keep class androidx.viewpager.widget.ViewPager$LayoutParams { int position; }
-keep class com.google.android.material.bottomsheet.BottomSheetBehavior { *** findScrollingChild(...); }

Another solution

ViewPagerBottomSheet is another great solution for the problem. It provides a custom ViewPagerBottomSheetBehavior which takes care ViewPager's children.

However, we need to note ViewPagerBottomSheetBehavior is a replacement of Google's BottomSheetBehavior. It means we can't use the latest updates of BottomSheetBehavior until ViewPagerBottomSheetBehavior includes the updates into the library source code.

If you want to use standard BottomSheetBehavior, this app's BottomSheetViewPager might work better for you.