BottomSheetDialogFragment下拉退出弹框

作者: 奔跑吧李博 | 来源:发表于2019-04-01 21:14 被阅读24次
    效果图:

    代码直通车

    BottomSheetDialogFragment类继承关系:

    BottomSheetDialogFragment -> AppCompatDialogFragment -> DialogFragment -> Fragment

    在实际创建的BottomSheetDialog中看到引用的布局文件,引用的是id为design_bottom_sheet的FrameLayout,可以通过设置它的高度来修改dialog的高度。
        private View wrapInBottomSheet(int layoutResId, View view, LayoutParams params) {
            FrameLayout container = (FrameLayout)View.inflate(this.getContext(), layout.design_bottom_sheet_dialog, (ViewGroup)null);
            CoordinatorLayout coordinator = (CoordinatorLayout)container.findViewById(id.coordinator);
            if (layoutResId != 0 && view == null) {
                view = this.getLayoutInflater().inflate(layoutResId, coordinator, false);
            }
    
            FrameLayout bottomSheet = (FrameLayout)coordinator.findViewById(id.design_bottom_sheet);
            this.behavior = BottomSheetBehavior.from(bottomSheet);
            this.behavior.setBottomSheetCallback(this.bottomSheetCallback);
            this.behavior.setHideable(this.cancelable);
            if (params == null) {
                bottomSheet.addView(view);
            } else {
                bottomSheet.addView(view, params);
            }
    
            coordinator.findViewById(id.touch_outside).setOnClickListener(new OnClickListener() {
                public void onClick(View view) {
                    if (BottomSheetDialog.this.cancelable && BottomSheetDialog.this.isShowing() && BottomSheetDialog.this.shouldWindowCloseOnTouchOutside()) {
                        BottomSheetDialog.this.cancel();
                    }
    
                }
            });
            ViewCompat.setAccessibilityDelegate(bottomSheet, new AccessibilityDelegateCompat() {
                public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfoCompat info) {
                    super.onInitializeAccessibilityNodeInfo(host, info);
                    if (BottomSheetDialog.this.cancelable) {
                        info.addAction(1048576);
                        info.setDismissable(true);
                    } else {
                        info.setDismissable(false);
                    }
    
                }
    
                public boolean performAccessibilityAction(View host, int action, Bundle args) {
                    if (action == 1048576 && BottomSheetDialog.this.cancelable) {
                        BottomSheetDialog.this.cancel();
                        return true;
                    } else {
                        return super.performAccessibilityAction(host, action, args);
                    }
                }
            });
            bottomSheet.setOnTouchListener(new OnTouchListener() {
                public boolean onTouch(View view, MotionEvent event) {
                    return true;
                }
            });
            return container;
        }
    
    创建简单的BottomSheetDialogFragment的基类,设置自定义弹框的高度。重写DialogFragment的getTheme()方法来设置弹框的style。
    public class BaseBottomSheetDialog extends BottomSheetDialogFragment {
        private FrameLayout bottomSheet;
        public BottomSheetBehavior<FrameLayout> behavior;
    
        @Override
        public void onStart() {
            super.onStart();
    
            BottomSheetDialog dialog = (BottomSheetDialog) getDialog();
            bottomSheet = dialog.getDelegate().findViewById(android.support.design.R.id.design_bottom_sheet);
            if (bottomSheet != null) {
                CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) bottomSheet.getLayoutParams();
                layoutParams.height = getHeight();
                bottomSheet.setLayoutParams(layoutParams);
                behavior = BottomSheetBehavior.from(bottomSheet);
                behavior.setPeekHeight(getHeight());
                // 初始为展开状态
                behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
            }
        }
    
        @Override
        public int getTheme() {
            return R.style.basedialog_anim_style;
        }
    
        protected int getHeight() {
            return getResources().getDisplayMetrics().heightPixels;
        }
    
    }
    
    子类继承使用:
    public class MyBottomSheetDialog extends BaseBottomSheetDialog {
    
        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.dialog_comment, container);
            return view;
        }
    
        @Override
        protected int getHeight() {
            return getResources().getDisplayMetrics().heightPixels - 400;
        }
    }
    
    展示方式:
            MyBottomSheetDialog dialog = new MyBottomSheetDialog();
            dialog.show(getSupportFragmentManager(), "");
    

    相关文章

      网友评论

        本文标题:BottomSheetDialogFragment下拉退出弹框

        本文链接:https://www.haomeiwen.com/subject/hwxjbqtx.html