BottomSheetDialogFragment 如何设置高度和禁止滑动

  • 主要是获取dialog 的BottomSheetBehavior 然后设置 setPeekHeightBottomSheetCallback.

code

public class BottomSheetFragment extends BottomSheetDialogFragment {

    private Button button;
    private View contentView;
    //
    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) {
        }
    };


    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();

        if (dialog != null) {
            View bottomSheet = dialog.findViewById(R.id.design_bottom_sheet);
            bottomSheet.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT;
        }
        final View view = getView();
        view.post(new Runnable() {
            @Override
            public void run() {
                View parent = (View) view.getParent();
                CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) (parent).getLayoutParams();
                CoordinatorLayout.Behavior behavior = params.getBehavior();
                mBottomSheetBehavior = (BottomSheetBehavior) behavior;
                mBottomSheetBehavior.setBottomSheetCallback(mBottomSheetBehaviorCallback);
                Display display = getActivity().getWindowManager().getDefaultDisplay();
                //设置高度
                int height = display.getHeight() / 2;
                mBottomSheetBehavior.setPeekHeight(height);

                parent.setBackgroundColor(Color.TRANSPARENT);
            }
        });
    }


    private void findView() {
        button = contentView.findViewById(R.id.button);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        contentView = inflater.inflate(R.layout.bottom_sheet_dialog_content_view, container,
                false);
        findView();

        return contentView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        button.setText(TAG);
    }
}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 177,775评论 25 709
  • 在园子里散步时,又见到这个卖米酥的女人,她总是风雨无阻地吆喝着,却还未曾见过有人来买。 她眼神里也流露出失望,却又...
    天堂里的鱼阅读 276评论 0 0
  • 您好
    宙斯盾与飞阅读 252评论 0 0
  • 阳春三月,春回大地,万物复苏,春日暖阳刚唤醒沉睡已久的枯萎小草,伸探出两片尖尖的嫩芽绿瓣。吹红了林里粉红白蕊的艳桃...
    蓝色忧郁_816f阅读 582评论 1 9
  • 夕阳裹挟了最后一抹绚烂,沉沉的坠入远山,星与星辰,月色温润,柔柔的穿透了黑暗。日与夜从来没有泾渭分明,却也没有割舍...
    星辰还暖阅读 449评论 0 0

友情链接更多精彩内容