Android Design - 使用 BottomSheetBehavior

Flutter 下使用 BottomSheetBehavior ↗️传送门

效果图

Android Design - 使用 BottomSheetBehavior_第1张图片
demo

使用

  1. 引入依赖:
compile 'com.android.support:design:24.2.0'
  1. 编写底部弹出菜单的显示内容,文件名item_bottom_sheet_behavior



    

        

        
    

    

        
    

    

  1. 布局页面UI,文件名test_activity



    

        

        
    

    

        
    

    

  1. 测试页面:
import android.support.annotation.NonNull;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.BottomSheetDialog;
import android.view.View;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

@EActivity(R.layout.test_activity)
public class TestActivity extends AppBaseActivity {

    @ViewById(R.id.fl_bottomSheet)
    View fl_bottomSheet;

    private BottomSheetBehavior mBottomSheetBehavior;

    @AfterViews
    void onPageStart() {
        initBottomSheetBehavior();
    }

    /**
     * 初始化 BottomSheetBehavior
     */
    private void initBottomSheetBehavior() {

        mBottomSheetBehavior = BottomSheetBehavior.from(fl_bottomSheet);

        // 如果开始就显示一部分内容,可以添加一下两行代码
        mBottomSheetBehavior.setPeekHeight(120); //  app:behavior_peekHeight="60dp"
        mBottomSheetBehavior.setHideable(false); // app:behavior_hideable="false"

        mBottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override
            public void onStateChanged(@NonNull View bottomSheet, int newState) {
                //newState 有5个状态 :
                //拖动 BottomSheetBehavior.STATE_DRAGGING 1
                //下滑 BottomSheetBehavior.STATE_SETTLING 2
                //展开 BottomSheetBehavior.STATE_EXPANDED 3
                //收起 BottomSheetBehavior.STATE_COLLAPSED 4
                // BottomSheetBehavior.STATE_HIDDEN 5
            }

            @Override
            public void onSlide(@NonNull View bottomSheet, float slideOffset) {
                //这里是拖拽中的回调,slideOffset为0-1 完全收起为0 完全展开为1
            }
        });
    }

    /**
     * 点击弹出 BottomSheetBehavior
     */
    @Click(R.id.btn_behavior)
    void btnClick() {
        if (mBottomSheetBehavior.getState() == BottomSheetBehavior.STATE_EXPANDED) {
            mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
        } else {
            mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
        }
    }

    /**
     * 点击弹出 BottomSheetDialog
     */
    @Click(R.id.btn_dialog)
    void btnDialogClick() {
        BottomSheetDialog bsDialog = new BottomSheetDialog(this);
        bsDialog.setContentView(R.layout.item_bottom_sheet_behavior);
        bsDialog.show();
    }
}

你可能感兴趣的:(Android Design - 使用 BottomSheetBehavior)