含RecyclerView页面中Standard/Persistent BottomSheet滚动与可见性异常问题
问题根源
布局层级顺序错误导致异常:ConstraintLayout 中后声明的控件 Z 轴层级更高,你当前将 RecyclerView 声明在承载 BottomSheet 的 CoordinatorLayout 之后,导致 RecyclerView 覆盖在 BottomSheet 上层,所有触摸事件优先被 RecyclerView 消费,因此出现 BottomSheet 无法响应操作、触摸触发后方列表滚动的问题。
修复方案
步骤1:调整 activity_main.xml 控件顺序
将 RecyclerView 移到 CoordinatorLayout 之前,保证 BottomSheet 的层级高于 RecyclerView,修改后代码如下:
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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/layout_container_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="app.standardbottomsheet.ui.MainActivity"> <!-- 先声明RecyclerView,层级更低,放在底层 --> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recycler_list" android:layout_width="match_parent" android:layout_height="match_parent" /> <!-- 后声明CoordinatorLayout,层级更高,盖在RecyclerView上方 --> <androidx.coordinatorlayout.widget.CoordinatorLayout android:id="@+id/layout_container_1" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_constraintBottom_toBottomOf="parent" tools:context="app.standardbottomsheet.ui.MainActivity"> <include android:id="@+id/included_layout_standard_bottom_sheet" layout="@layout/layout_standard_bottom_sheet" /> </androidx.coordinatorlayout.widget.CoordinatorLayout> </androidx.constraintlayout.widget.ConstraintLayout>
步骤2:优化BottomSheet布局属性
在 layout_standard_bottom_sheet.xml 的根布局添加属性,阻止触摸事件向下透传,若你已经设置过纯色不透明背景可以不用重复添加背景属性:
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/layout_container_bottom_sheet" android:layout_width="match_parent" android:layout_height="wrap_content" <!-- 新增以下属性 --> android:background="@color/white" android:clickable="true" android:focusable="true" app:layout_behavior="@string/bottom_sheet_behavior"> <!-- 原有布局内容保持不变 --> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/summary" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent"> <androidx.appcompat.widget.AppCompatImageView android:id="@+id/thumbnail" android:layout_width="?actionBarSize" android:layout_height="?actionBarSize" android:src="@mipmap/ic_launcher" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toTopOf="parent" /> <androidx.appcompat.widget.AppCompatTextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/app_name" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toRightOf="@id/thumbnail" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> <com.google.android.material.slider.Slider android:id="@+id/progress" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@id/summary" /> </androidx.constraintlayout.widget.ConstraintLayout>
修改完成后即可正常使用BottomSheet的折叠/展开功能,触摸BottomSheet不会触发下方RecyclerView滚动。
内容的提问来源于stack exchange,提问作者user6617128
相关产品推荐
相关产品推荐

