一、简述
1、 网上很多关于这个的描述都过时了,先去官网盘一下类映射 Androidx:
1、BottomSheetBehavior | Android Developers
android.support.design.widget.BottomSheetBehavior 变更为-> com.google.android.material.bottomsheet.BottomSheetBehavior
2、BottomSheetDialog 使用详解,设置圆角、固定高度、默认全屏等
- 指定一个layout,设置属性:
app:layout_behavior="@string/bottom_sheet_behavior" - 设置位置
- 指定 behavior_peekHeight(初始显示高度,也是收起时的高度,下限),若过高则无法拖动
- 没有结合RecycleView
- CoordinatorLayout高度是展开上限
- BottomSheetBehavior几种状态:
- STATE_COLLAPSED: 折叠状态
- STATE_EXPANDED: 展开状态
- STATE_DRAGGING : 过渡状态
- STATE_SETTLING: 视图从脱离手指自由滑动到最终停下的这一小段时间
- STATE_HIDDEN : 默认无此状态(可通过app:behavior_hideable 启用此状态),启用后用户将能通过向下滑动完全隐藏 bottom sheet
二、实践
1、简单样例
1、 配置xml
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/standard_bottom_sheet"
style="?attr/bottomSheetStyle"
android:background="#FFB6C1"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
<!-- Bottom sheet contents. -->
<TextView
android:id="@+id/tv_1"
android:layout_width="match_parent<img src=""" alt="" width="50%" />
android:layout_height="100dp"
android:text="111"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tv_2"
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="222"
app:layout_constraintTop_toBottomOf="@id/tv_1" />
<TextView
android:id="@+id/tv_3"
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="333"
app:layout_constraintTop_toBottomOf="@id/tv_2" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
- 得到区域:
- 得到一个可向上拉动的view,初始位置不知道怎么定下来的,向上拉红色区域可以覆盖为编写的match_parent
- 如果修改高度 例如 layout_height="300dp",则会在底部占据300dp且无法上拉,应该使用 app:behavior_peekHeight="300dp"