Cannot scroll scrollable content inside ViewPager as BottomSheet of CoordinatorLayout?

If you had multiple scrollable contents inside ViewPager as BottomSheet of CoordinatorLayout.
You might notice that your scrollable contents unable to scroll except the first one.

After doing some research I found that this is a default behaviour of BottomSheetBehavior by design. Though I disagree with this behavior on the point.

From BottomSheetBehavior source code of design library, I found the condition of picking NestedScrollView inside BottomSheet incredibly simple. It just pick the first NestedScrollView which is enabled, no matter how many views inside the viewgroup.

Code for glance (source code link):

View findScrollingChild(View view) {  
    if (ViewCompat.isNestedScrollingEnabled(view)) {
        return view;
    }
    if (view instanceof ViewGroup) {
        ViewGroup group = (ViewGroup) view;
        for (int i = 0, count = group.getChildCount(); i < count; i++) {
            View scrollingChild = findScrollingChild(group.getChildAt(i));
            if (scrollingChild != null) {
                return scrollingChild;
            }
        }
    }
    return null;
}
My Solution

The tag of NestedScrollView is the position of view pager that you have to set on ViewPager's view instantiating.

Since BottomSheet only pick the first encountered NestedScrollView which is enabled in ViewGroup, the trick is only enable the current scroll view and disable all others.

comments powered by Disqus