Bottom Sheet使用教程

来源:互联网 发布:软件升级工具 编辑:程序博客网 时间:2024/06/03 14:31

什么是Bottom Sheet?

Bottom Sheet是Design Support Library23.2 版本引入的一个类似于对话框的控件,可以暂且叫做底部弹出框吧。 Bottom Sheet中的内容默认是隐藏起来的,只显示很小一部分,可以通过在代码中设置其状态或者手势操作将其完全展开,或者完全隐藏,或者部分隐藏
官方地址

引入

dependencies {    //必须要23.2.0 以上    compile "com.android.support:appcompat-v7:25.3.1"    compile "com.android.support:design:25.3.1"}

Persistent bottom sheet的用法

其实Persistent bottom sheet不能算是一个控件,因为它只是一个普通的布局在CoordinatorLayout这个布局之下所表现出来的特殊行为。所以其使用方式跟普通的控件也很不一样,它必须在CoordinatorLayout中,并且是CoordinatorLayout的直接子view。

定义布局

<?xml version="1.0" encoding="utf-8"?><android.support.design.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"    android:fitsSystemWindows="true"    tools:context=".activity.BottomSheetActivity">    <android.support.design.widget.AppBarLayout        android:layout_width="match_parent"        android:layout_height="wrap_content">        <android.support.v7.widget.Toolbar            android:id="@+id/toolbar"            android:layout_width="match_parent"            android:layout_height="?attr/actionBarSize" />    </android.support.design.widget.AppBarLayout>    <LinearLayout        android:layout_width="wrap_content"        android:orientation="vertical"        android:layout_gravity="center_vertical"        android:layout_height="wrap_content">        <Button            android:id="@+id/btn_intro"            android:layout_gravity="center_horizontal"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="介绍"/>        <Button            android:id="@+id/btn_selected"            android:layout_gravity="center_horizontal"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="选择"/>    </LinearLayout>    <android.support.v4.widget.NestedScrollView        android:id="@+id/scroll"        android:layout_width="match_parent"        android:layout_height="wrap_content"        app:behavior_hideable="true"        app:behavior_peekHeight="50dp"        app:layout_behavior="@string/bottom_sheet_behavior">        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="vertical">            <TextView                android:layout_width="match_parent"                android:layout_height="500px"                android:gravity="center"                android:background="@color/default_app_main_color"                android:text="人不会死在绝境,却往往栽在十字路口"                android:textColor="@android:color/white"/>        </LinearLayout>    </android.support.v4.widget.NestedScrollView></android.support.design.widget.CoordinatorLayout>

其中两个重要属性解释
app:behavior_hideable=”true”
app:behavior_peekHeight=”50dp”

app:behavior_hideable="true" 表示你可以让bottom sheet完全隐藏,默认为false
app:behavior_peekHeight="50dp" 表示当为STATE_COLLAPSED(折叠)状态的时候bottom sheet残留的高度,默认为0

Bottom sheet有以下5种状态

  • STATE_COLLAPSED: 默认的折叠状态, bottom sheets只在底部显示一部分布局。显示高度可以通过 app:behavior_peekHeight 设置(默认是0)
  • STATE_DRAGGING : 过渡状态,此时用户正在向上或者向下拖动bottom sheet
  • STATE_SETTLING: 视图从脱离手指自由滑动到最终停下的这一小段时间
  • STATE_EXPANDED: bottom sheet 处于完全展开的状态:当bottom sheet的高度低于CoordinatorLayout容器时,整个bottom sheet都可见;或者CoordinatorLayout容器已经被bottom sheet填满。
  • STATE_HIDDEN : 默认无此状态(可通过app:behavior_hideable 启用此状态),启用后用户将能通过向下滑动完全隐藏 bottom sheet
    -

控制隐藏显示

BottomSheetBehavior behavior = BottomSheetBehavior.from(findViewById(R.id.scroll));if(behavior.getState() == BottomSheetBehavior.STATE_EXPANDED) {    behavior.setState(BottomSheetBehavior.STATE_COLLAPSED);}else {    behavior.setState(BottomSheetBehavior.STATE_EXPANDED);}

监听状态

BottomSheetBehavior behavior = BottomSheetBehavior.from(findViewById(R.id.scroll));behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {    @Override    public void onStateChanged(View bottomSheet, int newState) {        // Check Logs to see how bottom sheets behaves        switch (newState) {            case BottomSheetBehavior.STATE_COLLAPSED:                Log.e("Bottom Sheet Behaviour", "STATE_COLLAPSED");                break;            case BottomSheetBehavior.STATE_DRAGGING:                Log.e("Bottom Sheet Behaviour", "STATE_DRAGGING");                break;            case BottomSheetBehavior.STATE_EXPANDED:                Log.e("Bottom Sheet Behaviour", "STATE_EXPANDED");                break;            case BottomSheetBehavior.STATE_HIDDEN:                Log.e("Bottom Sheet Behaviour", "STATE_HIDDEN");                break;            case BottomSheetBehavior.STATE_SETTLING:                Log.e("Bottom Sheet Behaviour", "STATE_SETTLING");                break;        }    }    @Override    public void onSlide(View bottomSheet, float slideOffset) {    }});

部分博文摘自:
http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2017/0327/7729.html