BottomSheet 的實(shí)現(xiàn) 一 使用BottomSheetBehavior

依賴于CoordinatorLayout和BottomSheetBehavior,需要將底部菜單布局作為CoordinatorLayout的子View,實(shí)現(xiàn)簡(jiǎn)單但不夠靈活,適用于底部菜單布局穩(wěn)定的情況。
布局文件

<!--父布局必須是 CoordinatorLayout-->
<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">

    <!--頁(yè)面的主布局-->
    <include layout="@layout/content_main"/>

    <!--當(dāng)作bottomSheet的布局-->
    <include layout="@layout/content_bottom_sheet" />

</android.support.design.widget.CoordinatorLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.AppCompatButton
        android:id="@+id/btn_expand"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Expand_BottomSheet"/>

    <android.support.v7.widget.AppCompatButton
        android:id="@+id/btn_collapsed"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Collapsed_BottomSheet"/>

    <android.support.v7.widget.AppCompatButton
        android:id="@+id/btn_hide"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hide_BottomSheet"/>

    <android.support.v7.widget.AppCompatButton
        android:id="@+id/btn_bottomsheet_dialog"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="BottomSheetDialog"/>

    <android.support.v7.widget.AppCompatButton
        android:id="@+id/btn_bottomsheet_dialog_fragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="BottomSheetDialogFragment"/>
</LinearLayout>

content_bottom_sheet.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/ll_content_bottom_sheet"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:behavior_hideable="true"
    app:behavior_peekHeight="50dp"http://控制bottomSheet 初始顯示的高度
    app:layout_behavior="@string/bottom_sheet_behavior"
    android:background="@color/colorPrimary">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:text="人生若只如初見(jiàn),何事秋風(fēng)悲畫扇。"
        android:textSize="20sp"
        android:gravity="center"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:text="等閑變卻故人心,卻道故人心易變。"
        android:textSize="20sp"
        android:gravity="center"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:text="驪山語(yǔ)罷清宵半,淚雨霖鈴終不怨。"
        android:textSize="20sp"
        android:gravity="center"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:text="何如薄幸錦衣郎,比翼連枝當(dāng)日愿。"
        android:textSize="20sp"
        android:gravity="center"/>

</LinearLayout>

activity中使用

public class BottomSheetActivity extends AppCompatActivity implements View.OnClickListener {

    private AppCompatButton btnExpand;
    private AppCompatButton btnCollapsed;
    private AppCompatButton btnHide;
    private AppCompatButton btnBottomsheetDialog;
    private AppCompatButton btnBottomsheetDialogFragment;
    private LinearLayout llContentBottomSheet;

    private BottomSheetBehavior bottomSheetBehavior;
    private BottomSheetDialog dialog;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bottom_sheet);
        initViews();
    }

    private void initViews() {
        btnExpand = (AppCompatButton) findViewById(R.id.btn_expand);
        btnCollapsed = (AppCompatButton) findViewById(R.id.btn_collapsed);
        btnHide = (AppCompatButton) findViewById(R.id.btn_hide);
        btnBottomsheetDialog = (AppCompatButton) findViewById(R.id.btn_bottomsheet_dialog);
        btnBottomsheetDialogFragment = (AppCompatButton) findViewById(R.id.btn_bottomsheet_dialog_fragment);
        llContentBottomSheet = (LinearLayout) findViewById(R.id.ll_content_bottom_sheet);
        btnExpand.setOnClickListener(this);
        btnCollapsed.setOnClickListener(this);
        btnHide.setOnClickListener(this);
        btnBottomsheetDialogFragment.setOnClickListener(this);
        btnBottomsheetDialog.setOnClickListener(this);
        /*獲取behavior 控制bottomsheet的 顯示 與隱藏*/
        bottomSheetBehavior = BottomSheetBehavior.from(llContentBottomSheet);
        /*bottomSheet 的 狀態(tài)改變 根據(jù)不同的狀態(tài) 做不同的事情*/
        bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override
            public void onStateChanged(@NonNull View bottomSheet, int newState) {

                switch (newState) {
                    case BottomSheetBehavior.STATE_COLLAPSED:
                        Log.e("ccccccccc", "STATE_COLLAPSED");
                        break;
                    case BottomSheetBehavior.STATE_DRAGGING:
                        Log.e("ccccccccc", "STATE_DRAGGING");
                        break;
                    case BottomSheetBehavior.STATE_EXPANDED:
                        Log.e("ccccccccc", "STATE_EXPANDED");
                        break;
                    case BottomSheetBehavior.STATE_HIDDEN:
                        Log.e("ccccccccc", "STATE_HIDDEN");
                        break;
                    case BottomSheetBehavior.STATE_SETTLING:
                        Log.e("ccccccccc", "STATE_SETTLING");
                        break;
                }
            }

            @Override
            public void onSlide(@NonNull View bottomSheet, float slideOffset) {
                 /*slideOffset bottomSheet 的 移動(dòng)距離*/
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_expand://展開(kāi)
                bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
                break;
            case R.id.btn_collapsed://折疊 是顯示剛開(kāi)始預(yù)設(shè)的高度 也就是app:behavior_peekHeight這個(gè)實(shí)行的值
                bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
                break;
            case R.id.btn_hide://隱藏 就是完全隱藏
                bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
                break;
        }
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容