使用ViewPager2的BottomSheetBehavior无法通过嵌套的RecyclerView滚动向下滚动

11

我有一个类似于BottomSheetBehavior的视图,这个视图里面有ViewPager2。每个ViewPager2的页面都是一个垂直的RecyclerView。问题在于,当当前的垂直RecyclerView(也就是ViewPager的一页)无法再垂直滚动时,BottomSheet就无法向下滚动。如果只有一个垂直的RecyclerView而不是ViewPager,那么一切正常。

临时解决方案是使用NestedScrollView包装ViewPager,但这对性能非常不利并且自身存在缺陷。

原始布局:

    <?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>

[这就是它的外观(抱歉gif质量不佳)]

在此输入图片描述

2个回答

18

对于这种情况,我找到了一个解决方案,我为内部 RecyclerView 设置了 isNestedScrollingEnabled = false ,这样 BottomSheetBehavior 就可以找到另一个滚动视图。


viewPager.children.find { it is RecyclerView }?.let {
        (it as RecyclerView).isNestedScrollingEnabled = false
}

6
谢谢,它起作用了!不幸的是出现了另一个错误:当打开底部菜单并向下滑动到下一页时,该页面中的RecyclerView变得无法滚动,直到我将底部菜单下移至少几个像素。 - deviant

1

非常感谢您的回复,这对我的研究很有帮助。我已经找到了另一种解决方案:我在ViewPager中的内部RecyclerView中设置了isNestedScrollingEnabled = false,这样BottomSheetBehavior就可以找到其他滚动视图了。 - SimpleAndroid

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接