RecyclerView 在 BottomSheetFragment 中隐藏它下面的项目

RecyclerView obscurs items below it in a BottomSheetFragment

我正在使用具有自定义布局的 BottomSheetDialogFragment。我正在尝试进行以下设置:

<TextView> -> pinned to the top of the bottom sheet
<RecyclerView> -> wrap_content 
<Button> -> pinned to the bottom of the bottom sheet

TextViewButton 必须始终可见(粘性),而 RecyclerView 应保持在中间并滚动而不会遮挡其他视图。

这是我目前的布局:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/title"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Title"
        app:layout_constraintBottom_toTopOf="@id/recyclerView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constrainedHeight="true"
        app:layout_constraintBottom_toTopOf="@id/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/title" />

    <Button
        android:id="@+id/button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

这是一个小项目列表的样子,RecyclerView 不需要滚动。

这是包含大量项目的样子。 标题保持固定在顶部,但按钮不会。 该按钮实际上什至不可见,即使我一直向下滚动也是如此。

令我感到奇怪的是,同样的布局适用于常规全屏 activity,但它以某种方式失败并显示 BottomSheetFragment

我已经看过其他帖子,但是 none 有帮助,例如

recycler view 的高度不应该是wrap_content

如果你想要 recyler in-between 你的标题和页脚,更好的方法是设置 height = 0 并将其顶部固定到底部标题,将底部固定到页脚顶部(就像你已经做的那样),它会自动为你拉伸

最终的解决方案是将底部 sheet 的状态设置为展开,例如

val bottomSheetDialog = requireDialog() as BottomSheetDialog
bottomSheetDialog.behavior.state = BottomSheetBehavior.STATE_EXPANDED

我想底部 sheet 从未完全展开,因此布局从未完全可见。我以为底部sheet会根据内容的高度自动展开,但是我错了。布局本身很好,我没有对其进行任何更改。