Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Bottom Sheet 使用詳解:五種狀態、嵌套滑動與 Dialog 封裝

編輯:關於 Android 開發

Support Library 23.2 帶來的 Bottom Sheet

Android Support Library 更新到 23.2 版時,除了既有的 API 調整,還新增了幾個實用控制項,Bottom Sheet 就是其中之一。

這個控制項通常用於「從底部劃出表單」,Material Design 設計規範官網上就有這種設計模式。


怎麼用

用法很直覺:只要為 CoordinatorLayout 的某個子 View 加上 BottomSheetBehavior 來描述行為即可。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/bottom_sheet"
    android:layout_alignParentBottom="true"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    app:behavior_hideable="true"
    app:behavior_peekHeight="@dimen/peek_height"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

    <include layout="@layout/layout_bottom_sheet" />
</RelativeLayout>

layout_bottom_sheet.xml 裡定義的是 sheet 本身的版面,內容完全依你的需求撰寫。

唯一必須注意的是:sheet 的版面必須是 CoordinatorLayout 的子 View。

這一點可以從原始碼看出來:

public static <V extends View> BottomSheetBehavior<V> from(V view) {
    ViewGroup.LayoutParams params = view.getLayoutParams();
    if (!(params instanceof CoordinatorLayout.LayoutParams)) {
        throw new IllegalArgumentException("The view is not a child of CoordinatorLayout");
    }
    CoordinatorLayout.Behavior behavior =
            ((CoordinatorLayout.LayoutParams) params).getBehavior();
    if (!(behavior instanceof BottomSheetBehavior)) {
        throw new IllegalArgumentException(
                "The view is not associated with BottomSheetBehavior");
    }
    return (BottomSheetBehavior<V>) behavior;
}

也就是說,若父容器不是 CoordinatorLayout,就會丟出 IllegalArgumentException。


五種狀態回呼

STATE_COLLAPSED
折疊狀態。可透過 app:behavior_peekHeight 設定預設露出的高度。

STATE_SETTLING
拖拽鬆開後、抵達終點位置(collapsed 或 expanded)之前的過渡狀態。

STATE_EXPANDED
完全展開的狀態。

STATE_HIDDEN
隱藏狀態。預設為 false,可透過 app:behavior_hideable 屬性開啟。

STATE_DRAGGING
正在被拖拽的狀態。

監聽方式如下:

View bottomSheet = findViewById(R.id.bottom_sheet);
BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);

behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
    @Override
    public void onStateChanged(View bottomSheet, int newState) {
        String state = "null";
        switch (newState) {
            case 1: state = "STATE_DRAGGING"; break;
            case 2: state = "STATE_SETTLING"; break;
            case 3: state = "STATE_EXPANDED"; break;
            case 4: state = "STATE_COLLAPSED"; break;
            case 5: state = "STATE_HIDDEN"; break;
        }
        Log.d("MainActivity", "newState:" + state);
    }

    @Override
    public void onSlide(View bottomSheet, float slideOffset) {
        // slideOffset 介於 -1(隱藏)到 1(完全展開)之間
    }
});

嵌套滑動是硬性要求

如果 Bottom Sheet 內部含有需要滑動的視圖,那個視圖必須支援嵌套滑動,否則手勢會互相打架。可用的選項包括 NestedScrollView、RecyclerView,或在 API 21 以上使用 ListView、ScrollView。


BottomSheetDialog 與 BottomSheetDialogFragment

Support Library 23.2 另外提供了這兩個現成類別,用法與一般的 Dialog 幾乎相同,省下自己組 CoordinatorLayout 的功夫。

Button dialogBtn = (Button) findViewById(R.id.bottom_sheet_dialog_btn);
dialogBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        BottomSheetDialog dialog = new BottomSheetDialog(BottomSheetDemo.this);
        dialog.setContentView(R.layout.bottom_sheet_dialog_layout);
        dialog.show();
    }
});

版面檔 bottom_sheet_dialog_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:paddingLeft="15dp"
    android:paddingRight="15dp">

    <TextView
        android:gravity="center"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:minHeight="30dp"
        android:text="選取方式"
        android:textColor="?attr/colorAccent" />

    <Button
        android:gravity="center"
        android:id="@+id/photo"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="48dp"
        android:text="照片"
        android:textColor="@color/gray" />

    <View
        android:background="@color/color_gray"
        android:layout_height="1dp"
        android:layout_width="match_parent" />

    <Button
        android:gravity="center"
        android:id="@+id/camera"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="48dp"
        android:text="相機"
        android:textColor="@color/gray" />

    <View
        android:background="@color/color_gray"
        android:layout_height="1dp"
        android:layout_width="match_parent" />

    <Button
        android:gravity="center"
        android:id="@+id/tv_dialog_cancel"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:text="取消"
        android:textColor="?attr/colorAccent" />
</LinearLayout>

效果圖:


Bottom Sheet 介紹完畢。實務上它很適合承載「分享面板」、「篩選條件」、「更多操作」這類不需要佔滿整個螢幕、又希望使用者能一眼掌握選項的情境。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved