Android ViewPager with RecyclerView works incorrectly inside BottomSheet

后端 未结 7 1060
故里飘歌
故里飘歌 2020-12-13 02:51

When I try to scroll list, sometimes this works incorrect - BottomSheet intercepts the scroll event and hides.

How to reproduce this:

  1. Open Bottom Sheet
7条回答
  •  盖世英雄少女心
    2020-12-13 03:16

    I came across the same limitation but were able to solve it.

    The reason for the effect you described is that BottomSheetBehavior (as of v24.2.0) only supports one scrolling child which is identified during layout in the following way:

    private View findScrollingChild(View view) {
        if (view instanceof NestedScrollingChild) {
            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;
    }
    

    You can see that it essentially finds the first scrolling child using DFS.

    I slightly enhanced this implementation and assembled a small library as well as an example app. You can find it here: https://github.com/laenger/ViewPagerBottomSheet

    Simply add the maven repo url to your build.gradle:

    repositories {
        maven { url "https://raw.github.com/laenger/maven-releases/master/releases" }
    }
    

    Add the library to the dependencies:

    dependencies {
        compile "biz.laenger.android:vpbs:0.0.2"
    }
    

    Use ViewPagerBottomSheetBehavior for your bottom sheet view:

    app:layout_behavior="@string/view_pager_bottom_sheet_behavior"
    

    Setup any nested ViewPager inside the bottom sheet:

    BottomSheetUtils.setupViewPager(bottomSheetViewPager)
    

    (This also works when the ViewPager is the bottom sheet view and for further nested ViewPagers)

提交回复
热议问题