【发布时间】:2021-01-08 16:17:40
【问题描述】:
我做了一个简单的布局,其中有一个 AppBarLayout(不滚动),中间有一些内容,还有一个 BottomSheet。这个BottomSheet其实是一个带有BottomSheetBehavior的LinearLayout,里面有一个RecyclerView。
这个 BottomSheet 展开后会将 RecyclerView 放在 AppBarLayout 的顶部。问题是,当用户试图滚动这个 RecyclerView 时,下面的 AppBarLayout 会窃取滚动。
我将留下布局代码,但我将整个 example project 上传到 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)中; - 在我看到这个的原始项目中,如果目标 View 是 BottomSheet 的 RecyclerView,我编写了一个自定义 AppBarLayout 行为来忽略,但也没有用;
- 当 BottomSheet 展开时设置整个 AppBarLayout
GONE并且足够有趣,AppBarLayout 的行为就像我设置为INVISIBLE一样(如果以编程方式设置为GONE,它的行为就像不可见,如果在充气之前设置,它的行为就像预计)。
由于项目规范,我避免使用 Fragment 创建此 BottomSheet。
【问题讨论】:
-
线性布局匹配父级,你想在滚动时显示appbar吗?
-
不。 LinearLayout 必须填满整个屏幕,其子项的行为不应反映在后面的 AppBar 中。
标签: android android-recyclerview bottom-sheet android-appbarlayout appbar