首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >AppBarLayout防止RecyclerView在BottomSheet中滚动

AppBarLayout防止RecyclerView在BottomSheet中滚动
EN

Stack Overflow用户
提问于 2020-09-22 14:01:56
回答 1查看 435关注 0票数 3

我做了一个简单的布局,其中有一个AppBarLayout (不滚动),中间有一些内容,还有一个BottomSheet。这个BottomSheet实际上是一个带有BottomSheetBehavior的LinearLayout,内部有一个RecyclerView。

这个BottomSheet在展开时将RecyclerView放置在AppBarLayout的顶部。问题是,当用户试图滚动这个RecyclerView时,下面的AppBarLayout会窃取这个滚动。

我留下了布局代码,但我将整个示例项目上传到GitHub,并以GIF作为说明。

布局

代码语言:javascript
复制
<?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,但也没有工作;
  • 设置整个AppBarLayout GONE,当BottomSheet展开并且足够有趣时,AppBarLayout的行为就像我设置为INVISIBLE一样(如果以编程方式设置为GONE,它的行为就像不可见的,如果在充气之前设置,它的行为就像预期的那样)。

由于项目规范,我避免使用片段来创建这个BottomSheet。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64011422

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档