带有 ViewPager2 的 BottomSheetBehavior 无法通过嵌套的 RecyclerView 滚动向下滚动

BottomSheetBehavior with ViewPager2 can't be scrolled down by nested RecyclerView scroll

我有一个视图在里面表现得像 BottomSheetBehavior and this view has ViewPager2。每个 ViewPager2 的页面都是一个垂直的 RecyclerView。问题是当当前垂直 RecyclerView(这是 ViewPager 的页面)不能再垂直滚动时,BottomSheet 不会向下滚动。当我只有一个垂直 RecyclerView 而不是 ViePager 时,一切正常文件。

临时解决方案是用 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 质量)]

BottomSheetBehaviour 仅检测第一个可滚动视图。因此始终建议在其中只使用一个可滚动视图。

有关更多信息,请查看此答案bottomsheetbehavior-with-two-recyclerview

还有这个

如果你真的想要两个可滚动的视图,我建议你也看看这个库AndroidSlidingUpPanel

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

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