如何创建适合 sheet 大小的 android 底部 sheet 且视图始终与底部对齐?

How to create an android Bottom sheet that fits the size of the sheet with view always aligned to bottom?

我在研究时遇到了一些麻烦,不知道如何用这个词来表达这个,所以我在下面做了一个我想要实现的快速模型。 我的应用程序有一个持久的底部 sheet,我希望 sheet 的内容根据 sheet 是展开还是一半展开来调整大小。

我的 sheet 由一个回收器视图和底部带有一些按钮的视图组成。目前,sheet 的内容总是像 sheet 完全展开一样大小。这意味着除非 sheet 完全展开,否则不会显示按钮,并且大部分回收器视图也隐藏在裁剪后的 sheet 后面。我想要发生的是让按钮始终显示在 sheet 的底部,即使说 sheet 已展开一半,并且让 recyclerview 填充 space 剩下的任何内容.这在持久底部 sheet 中是否可能,如果现在,我应该使用什么方法来实现这一点?将不胜感激。

您可以使按钮布局始终显示在 sheet 的底部,方法是使用 addBottomSheetCallback 跟踪底部 sheet 滑动设置其 y 坐标回调:

val bottomSheet = findViewById<ConstraintLayout>(R.id.bottom_sheet)
val bottomLayout = findViewById<LinearLayout>(R.id.button_layout)

// initial setting the y coordinates of the bottom layout
bottomSheet.post {
    val bottomSheetVisibleHeight = bottomSheet.height - bottomSheet.top
    bottomLayout.y =
        (bottomSheetVisibleHeight - bottomLayout.height).toFloat()
}

BottomSheetBehavior.from(bottomSheet).apply {

    addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
        override fun onSlide(bottomSheet: View, slideOffset: Float) {
        
            // set the y coordinates of the bottom layout on bottom sheet slide
            val bottomSheetVisibleHeight = bottomSheet.height - bottomSheet.top
            bottomLayout.y =
                (bottomSheetVisibleHeight - bottomLayout.height).toFloat()
        }

        override fun onStateChanged(bottomSheet: View, newState: Int) {
        }
    })
}

布局:

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!--    Main Layout -->
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </androidx.constraintlayout.widget.ConstraintLayout>

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/bottom_sheet"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            app:behavior_hideable="false"
            app:layout_behavior="@string/bottom_sheet_behavior">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recyclerview"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:background="#CFE2F3"
                app:layout_constraintBottom_toTopOf="@+id/button_layout"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <LinearLayout
                android:id="@+id/button_layout"
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:background="#FF03DAC5"
                android:orientation="horizontal"
                app:layout_constraintBottom_toBottomOf="parent">

                <Button
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_marginStart="8dp"
                    android:layout_marginEnd="40dp"
                    android:layout_weight="1"
                    android:text="cancel" />

                <Button
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_marginEnd="8dp"
                    android:layout_weight="1"
                    android:text="ok" />

            </LinearLayout>

        </androidx.constraintlayout.widget.ConstraintLayout>

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>