美文网首页
使用DialogFragment实现对话框

使用DialogFragment实现对话框

作者: flycode | 来源:发表于2018-05-26 15:02 被阅读0次

    Talk is Cheap.

    public abstract class BaseDialog extends DialogFragment {
        protected View rootView;
    
        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setDialogStyle();
        }
    
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            // 设置圆角需要
            Window window = getDialog().getWindow();
            if (window != null) {
                window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            }
            getDialog().setCanceledOnTouchOutside(getCanceledOnTouchOutside());
            setCancelable(getCancelable());
    
            rootView = inflater.inflate(getLayoutId(), container, false);
            initView();
            setListener();
            return rootView;
        }
    
        @Override
        public void onStart() {
            super.onStart();
            // 重置宽高
            Window window = getDialog().getWindow();
            if (window != null) {
                window.setLayout(getWidth(), getHeight());
            }
        }
    
        /**
         * 显示Dialog
         */
        public void show(FragmentManager fragmentManager) {
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            Fragment pre = fragmentManager.findFragmentByTag(getFragmentTag());
            if (pre != null) {
                transaction.remove(pre);
            }
    
            // onSaveInstance后commit无法提交
            try {
                show(transaction, getFragmentTag());
            } catch (IllegalStateException e) {
                //  容错处理,不做操作
            }
        }
    
        /**
         * 显示Dialog
         */
        public void show(@NonNull FragmentActivity activity) {
            show(activity.getSupportFragmentManager());
        }
    
        /**
         * 避免莫名其妙的空指针
         * onSaveInstance后commit无法提交
         */
        @Override
        public void dismiss() {
            if (getFragmentManager() == null) {
                return;
            }
            dismissAllowingStateLoss();
        }
    
        protected abstract int getLayoutId();
    
        protected abstract void initView();
    
        protected abstract void setListener();
    
        protected abstract String getFragmentTag();
    
        /**
         * override
         * 配置对话框风格
         */
        protected void setDialogStyle() {
            setStyle(DialogFragment.STYLE_NO_TITLE, 0);
        }
    
        /**
         * override
         * 配置点击对话框外部是否消失
         */
        protected boolean getCanceledOnTouchOutside() {
            return false;
        }
    
        /**
         * override
         * 配置返回键是否可以关闭对话框
         */
        protected boolean getCancelable() {
            return true;
        }
    
        /**
         * override
         * 配置宽度
         */
        protected int getWidth() {
            return ViewGroup.LayoutParams.WRAP_CONTENT;
        }
    
        /**
         * override
         * 配置高度
         */
        protected int getHeight() {
            return ViewGroup.LayoutParams.WRAP_CONTENT;
        }
    
    }
    

    相关文章

      网友评论

          本文标题:使用DialogFragment实现对话框

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