material design 之 bottomsheet的用法

参考:
BottomSheet的用法
Material Design系列,Behavior之BottomSheetBehavior与BottomSheetDialog

GitHub demo

方式一 :BottomSheetBehavior:

核心:

app:layout_behavior="@string/bottom_sheet_behavior"

// <string name="bottom_sheet_behavior" translatable="false">
//      android.support.design.widget.BottomSheetBehavior
// </string>

使用:

在布局中添加behavior:

    <RelativeLayout
        android:id="@+id/bottom_sheet"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffff"
        android:minHeight="100dp"
        app:behavior_hideable="false"
        app:behavior_peekHeight="56dp"
        app:layout_behavior="@string/bottom_sheet_behavior">

        //.......
    </RelativeLayout>

注意:布局最外层必须是CoordinatorLayout
Behavior作为CoordinatorLayout的子View的LayoutParams,所以CoordinatorLayout是万万不能少的

初始化状态

View bottomSheet = findViewById(R.id.bottom_sheet);//设置了bottom_sheet_behavior的view
if (bottomSheet != null) {
    BottomSheetBehavior<View> behavior = BottomSheetBehavior.from(bottomSheet);
    behavior.setState(BottomSheetBehavior.STATE_COLLAPSED);//设置初始状态
}

切换状态

int state = behavior.getState();
if (state == BottomSheetBehavior.STATE_EXPANDED) {//张开
    behavior.setState(BottomSheetBehavior.STATE_COLLAPSED);//收缩
} else if(state == BottomSheetBehavior.STATE_COLLAPSED){
    behavior.setState(BottomSheetBehavior.STATE_HIDDEN);//隐藏
}else if(state == BottomSheetBehavior.STATE_HIDDEN){
    behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}

设置监听

behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
    @Override
    public void onStateChanged(@NonNull View bottomSheet, int newState) {
        //这里是bottomSheet 状态的改变回调
        //newState 有以下状态 :
        //展开 BottomSheetBehavior.STATE_EXPANDED
        //收起 BottomSheetBehavior.STATE_COLLAPSED
        //拖动 BottomSheetBehavior.STATE_DRAGGING
        //下滑 BottomSheetBehavior.STATE_SETTLING
        //隐藏 BottomSheetBehavior.STATE_HIDDEN
    }

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

方式二 : BottomSheetDialog

public class BottomSheetDialog extends AppCompatDialog {

public class AppCompatDialog extends Dialog implements AppCompatCallback {

public class Dialog implements DialogInterface, Window.Callback,
        KeyEvent.Callback, OnCreateContextMenuListener, Window.OnWindowDismissedCallback {

显示:

final BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(this);
View inflate = View.inflate(this, R.layout.bottomsheetdalig_layout, null);
View qq = inflate.findViewById(R.id.share_qq);
View wx = inflate.findViewById(R.id.share_wx);
View sina = inflate.findViewById(R.id.share_sina);
qq.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Toast.makeText(BottomSheetDialogActivity.this, "分享到qq", Toast.LENGTH_SHORT).show();
        bottomSheetDialog.dismiss();
    }
});
wx.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Toast.makeText(BottomSheetDialogActivity.this, "分享到wx", Toast.LENGTH_SHORT).show();
        bottomSheetDialog.dismiss();
    }
});
sina.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Toast.makeText(BottomSheetDialogActivity.this, "分享到sina", Toast.LENGTH_SHORT).show();
        bottomSheetDialog.dismiss();
    }
});
bottomSheetDialog.setContentView(inflate);
bottomSheetDialog.show();

隐藏:

bottomSheetDialog.dismiss();

坑一:

使用BottomSheetDialog弹出时,状态栏变黑

解决:

/**
 * 解决状态栏变黑的问题
 */
public class MyBottomSheetDialog extends BottomSheetDialog {
    private final Activity mActivity;

    public MyBottomSheetDialog(@NonNull Context context) {
        super(context);
        mActivity = (Activity) context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        int screenHeight = getScreenHeight(mActivity);
        int statusBarHeight = getStatusBarHeight(getContext());
        int dialogHeight = screenHeight - statusBarHeight;
        Window window = getWindow();
        if (window != null) {
            window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, dialogHeight == 0 ? ViewGroup.LayoutParams.MATCH_PARENT : dialogHeight);
        }
    }

    private static int getScreenHeight(Activity activity) {
        DisplayMetrics displaymetrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        return displaymetrics.heightPixels;
    }

    private static int getStatusBarHeight(Context context) {
        int statusBarHeight = 0;
        Resources res = context.getResources();
        int resourceId = res.getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            statusBarHeight = res.getDimensionPixelSize(resourceId);
        }
        return statusBarHeight;
    }

}

使用:

MyBottomSheetDialog mBottomSheetDialog = new MyBottomSheetDialog(this);
View view = getLayoutInflater().inflate(R.layout.dialog_bottom_sheet, null);
mBottomSheetDialog.setContentView(view);
mBottomSheetDialog.setCancelable(true);
mBottomSheetDialog.setCanceledOnTouchOutside(true);
mBottomSheetDialog.show();
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值