简体   繁体   English

嵌套的 RecyclerView 滚动无法向下滚动 ViewPager2 的 BottomSheetBehavior

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

I have a view that acts like BottomSheetBehavior and this view has ViewPager2 inside.我有一个类似于BottomSheetBehavior的视图,并且该视图内部有ViewPager2 Each ViewPager2's page is a vertical RecyclerView.每个 ViewPager2 的页面都是一个垂直的 RecyclerView。 The issue is that BottomSheet doesn't scroll down when current vertical RecyclerView (which is a page of ViewPager) can't scroll vertically anymore.问题是当当前垂直 RecyclerView(它是 ViewPager 的页面)不能再垂直滚动时,BottomSheet 不会向下滚动。 Everything works file when instead of ViePager I have only one vertical RecyclerView.当我只有一个垂直 RecyclerView 而不是 ViePager 时,一切工作文件。

The temporary solution is to wrap ViewPager with NestedScrollView but it's horrible for performance and has it's own bugs.临时解决方案是用 NestedScrollView 包装 ViewPager ,但它的性能很糟糕并且有它自己的错误。

The original layout:原始布局:

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

[Here's how it looks (sorry for the gif quality)] [这是它的外观(对于 gif 质量抱歉)]

在此处输入图像描述

I've found a solution for this case, I set isNestedScrollingEnabled = false for inner RecyclerView so that BottomSheetBehavior finds another scrolling view我为这种情况找到了解决方案,我为内部 RecyclerView 设置isNestedScrollingEnabled = false以便 BottomSheetBehavior 找到另一个滚动视图

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

BottomSheetBehaviour only detects the first scrollable view. BottomSheetBehaviour 仅检测第一个可滚动视图。 So it is always recommended to use only one scrollable view inside of it.因此,始终建议在其中仅使用一个可滚动视图。

For More information check this answer bottomsheetbehavior-with-two-recyclerview有关更多信息,请查看此答案bottomsheetbehavior-with-two-recyclerview

And this one also Scroll not working for multiple RecyclerView in BottomSheet而这一个也Scroll 不适用于 BottomSheet 中的多个 RecyclerView

If you really want to have the two scrollable views I recommend you to take a look at this library also AndroidSlidingUpPanel如果你真的想要两个可滚动的视图,我建议你看看这个库还有AndroidSlidingUpPanel

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM