BottomSheetDialogFragment + ViewPager+Fragment+RecyclerView 出現只有第一頁列表可以滑動其他頁不可滑動的情況。

BottomSheetDialogFragment + ViewPager+Fragment+RecyclerView這種樣式出現的不多,所以前一陣做這類需求的時候遇到種種問題。

主要的原因就是因爲BottomSheetBehavior的findScrollingChild方法並沒有有關ViewPager 更新查找子元素view的東西,所以它只能拿到一個頁面去滑動,那麼就需要對BottomSheetBehavior進行修改,這樣的話就需要自己定義BottomSheetDialog。
 

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;
}

修改後的結果:
 

@VisibleForTesting
    View findScrollingChild(View view) {
        if (ViewCompat.isNestedScrollingEnabled(view)) {
            return view;
        }
        if (view instanceof ViewPager) {
            ViewPager viewPager = (ViewPager) view;
            View currentViewPagerChild = viewPager.getChildAt(viewPager.getCurrentItem());
//            View currentViewPagerChild = ViewPagerUtils.getCurrentView(viewPager);
            if (currentViewPagerChild == null) {
                return null;
            }

            View scrollingChild = findScrollingChild(currentViewPagerChild);
            if (scrollingChild != null) {
                return scrollingChild;
            }
        } else 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;
    }

還有注意的一點是這種結合 RecyclerView不要在其外層加那種帶手勢一些事件攔截的ViewGroup,這樣會有一些衝突。比如刷新組件SwipeRefreshLayout 和一些上拉加載的組件。如果想要實現上拉加載那就在RecyclerView本身上實現。

具體demo請看我github:https://github.com/JiangAndroidwork/BottomSheetViewPager

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章