我制作了一个简单的布局,其中包含一个AppBarLayout(不滚动)、一些内容在中间以及一个BottomSheet。这个BottomSheet实际上是一个带有BottomSheetBehavior的LinearLayout,并且里面有一个RecyclerView。
当这个BottomSheet被展开时,RecyclerView会被放置在AppBarLayout顶部。问题在于,当用户尝试滚动这个RecyclerView时,底下的AppBarLayout会抢占滚动。
我会留下布局代码,但我已经将整个示例项目上传到GitHub上,并配有GIF进行说明。 布局
我已尝试:
当这个BottomSheet被展开时,RecyclerView会被放置在AppBarLayout顶部。问题在于,当用户尝试滚动这个RecyclerView时,底下的AppBarLayout会抢占滚动。
我会留下布局代码,但我已经将整个示例项目上传到GitHub上,并配有GIF进行说明。 布局
<?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/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:liftOnScroll="false">
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:title="@string/app_name" />
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="?colorSurface"
android:gravity="center_vertical"
android:padding="16dp"
android:text="Subtitle"
android:textAppearance="?textAppearanceSubtitle1" />
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="?colorSurface"
android:gravity="center_vertical"
android:padding="16dp"
android:text="Subsubtitle"
android:textAppearance="?textAppearanceBody2" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="56dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="@string/lorem_ipsum"
android:textAppearance="?textAppearanceBody1" />
</androidx.core.widget.NestedScrollView>
<LinearLayout
android:id="@+id/bottom_sheet_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:background="#FFF"
android:elevation="5dp"
android:orientation="vertical"
app:behavior_hideable="false"
app:behavior_peekHeight="64dp"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/bottom_sheet_header"
android:layout_width="match_parent"
android:layout_height="64dp"
android:gravity="center_vertical"
android:padding="16dp"
android:text="Fruits"
android:textAppearance="?textAppearanceHeadline6" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
</LinearLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
我已尝试:
- 将
nestedScrollingEnabled
属性放置在不同的位置,但无效,并且此行为发生在旧的API中(如JellyBean); - 在我看到此问题的原始项目中,我编写了一个自定义的AppBarLayout行为,以忽略目标视图是否为BottomSheet的RecyclerView,但也没有起作用;
- 当BottomSheet展开时,将整个AppBarLayout设置为
GONE
,有趣的是,AppBarLayout的行为就像我设置为INVISIBLE
一样(如果以编程方式设置为GONE
,它的行为就像不可见,如果在充气之前设置,则会按预期行事)。
由于项目规范,我避免使用Fragment来创建此BottomSheet。