BottomSheetDialogFragment 如何设置高度和禁止拖拽

这个很简单,看代码就能懂了,就不做描述了:


code

public class BottomSheetFragment extends BottomSheetDialogFragment {

    private BottomSheetBehavior<View> mBottomSheetBehavior;
    private BottomSheetBehavior.BottomSheetCallback mBottomSheetBehaviorCallback
            = new BottomSheetBehavior.BottomSheetCallback() {

        @Override
        public void onStateChanged(@NonNull View bottomSheet, int newState) {
            //禁止拖拽,
            if (newState == BottomSheetBehavior.STATE_DRAGGING) {
                //设置为收缩状态
                mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
            }
        }

        @Override
        public void onSlide(@NonNull View bottomSheet, float slideOffset) {
        }
    };


    @SuppressLint("RestrictedApi")
    @Override
    public void setupDialog(Dialog dialog, int style) {
        super.setupDialog(dialog, style);
        View contentView = View.inflate(getContext(), R.layout.bottom_sheet_dialog_content_view, null);
        dialog.setContentView(contentView);
        mBottomSheetBehavior = BottomSheetBehavior.from(((View) contentView.getParent()));
        if (mBottomSheetBehavior != null) {
            mBottomSheetBehavior.setBottomSheetCallback(mBottomSheetBehaviorCallback);
            mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
            Display display = getActivity().getWindowManager().getDefaultDisplay();
            //设置高度
            mBottomSheetBehavior.setPeekHeight(display.getHeight() / 2);
            contentView.requestLayout();
        }
    }
}

效果

这里写图片描述

猜你喜欢

转载自blog.csdn.net/a1018875550/article/details/80950053
今日推荐