DialogFragment

一、简介

DialogFragment 是在 Android 3.0 时引入的,是一种特殊的 Fragment ,用于在 Acticity 上展示一个静态的对话框,如:提示框,输入框,等等

二、DialogFragment 的好处

在 DialogFragment 之前,我们一般使用 Dialog 或者 AlertDialog 来创建对话框,包括现在还是很多人以这种方式来创建,但是 Google 并不推荐直接使用 Dialog 创建对话框,而是推荐尽量使用 DialogFragment 来创建,这是因为 DialogFragment 有 Dialog 所没有的一些很好的特性

  • DialogFragment 是 Fragment 的直接子类,和 Fragment 有着基本一致的声明周期,当屏幕旋转或者按后退键的时候,能更好的管理其生命周期,例如,旋转屏幕时,会导致对话框重新创建,使用 DialogFragment 的话,会由 FragmentManager 自己来重新创建对话框,而 Dialog 则没有这样的功能,必须手动创建。

三、使用 DialogFragment

3.1 使用 DialogFragment 至少需要实现 onCreateView() 或者 onCreateDialog() 方法,如下所示
public class MyDialogFrament extends DialogFragment {

    /**
     * 使用定义的xml布局文件展示Dialog。
     *
     * @param inflater
     * @param container
     * @param savedInstanceState
     * @return
     */

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    /**
     * 使用 Dialog 或者 AlertDialog 创建的来展示
     *
     * @param savedInstanceState
     * @return
     */
    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
            return super.onCreateDialog(savedInstanceState);

    }
3.2 具体的使用参考:http://blog.csdn.net/lmj623565791/article/details/37815413

其实,使用起来还是很简单的,但是仅仅这样使用,并不能显示我们程序员的专业性 0.0 , 我们要考虑复用性和封装,不想偷懒的程序员不是好程序员!!

四、封装成通用的 DialogFragment

  • 这有一个哥们写的很不错,github 地址:https://github.com/developerHaoz/DialogFragmentDemos

  • 本人也是分析了大神的思路,觉得很不错,就总结了下来,大概说下封装思路:主要是以 onCreateDialog 的方式来创建对话框,通过回调的形式,从外部传来 AlertDialog 来创建不同类型的提示框。

public class MyDialogFrament extends DialogFragment {

    /**
     * 使用定义的xml布局文件展示Dialog。
     *
     * @param inflater
     * @param container
     * @param savedInstanceState
     * @return
     */

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    /**
     * 使用 Dialog 或者 AlertDialog 创建的来展示
     * 设计为回调接口形式,让调用该接口的人决定显示哪种类型的 Dialog
     *
     * @param savedInstanceState
     * @return
     */
    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        if (mOnCallDialog == null) {
            return super.onCreateDialog(savedInstanceState);

        }
        return mOnCallDialog.getDialog(getActivity());

    }

    private OnDialogCancelListener mOnDialogCancelListener;

    /**
     * 监听弹窗是否被取消
     */
    public interface OnDialogCancelListener {
        void onCancel();
    }

    private OnCallDialog mOnCallDialog;

    /**
     * 需要显示哪种类型的 DialogFragment
     */
    public interface OnCallDialog {
        Dialog getDialog(Context context);
    }

    /**
     * 创建 MyDialogFrament 对象
     *
     * @param callDialog     Dialog 取消的监听对象
     * @param cancelable     Dialog 是否可以撤销
     * @param cancelListener 需要展示哪种 Dialog 的回调对象
     * @return MyDialogFrament 对象
     */
    public static MyDialogFrament newInstance(OnCallDialog callDialog, boolean cancelable, OnDialogCancelListener cancelListener) {
        MyDialogFrament instance = new MyDialogFrament();
        //控制显示的 Dialog 是否可取消
        instance.setCancelable(cancelable);
        instance.mOnDialogCancelListener = cancelListener;
        instance.mOnCallDialog = callDialog;
        return instance;
    }

    /**
     * 创建 MyDialogFrament 对象
     *
     * @param callDialog Dialog 取消的监听对象
     * @param cancelable Dialog 是否可以撤销
     * @return
     */
    public static MyDialogFrament newInstance(OnCallDialog callDialog, boolean cancelable) {
        return newInstance(callDialog, cancelable, null);
    }
    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {

            // 在 5.0 以下的版本会出现白色背景边框,若在 5.0 以上设置则会造成文字部分的背景也变成透明
            if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
                // 目前只有这两个 dialog 会出现边框
                if (dialog instanceof ProgressDialog || dialog instanceof DatePickerDialog) {
                    getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                }
            }

            Window window = getDialog().getWindow();
            WindowManager.LayoutParams windowParams = window.getAttributes();
            windowParams.dimAmount = 0.0f;
            window.setAttributes(windowParams);
        }
    }
    @Override
    public void onCancel(DialogInterface dialog) {
        super.onCancel(dialog);
        if (mOnDialogCancelListener != null) {
            mOnDialogCancelListener.onCancel();
        }
    }

}

  • 另一个设计点是监听对话框中的所做的操作,可能是选了某一个选项,又或者是点击了确定或取消等,也是以接口回调的形式来做的,可以更加自由的选择在什么地方来对这些操作处理,以其中的一个举例:

接口为:

public interface IDialogResultListener {
    void onDataResult(T result);
}

以一个列表选项的对话框为例:

    public static DialogFragment showListDialog(FragmentManager fragmentManager, final String title, final String[] items
            , final IDialogResultListener resultListener, boolean cancelable ){
        MyDialogFrament dialogFragment = MyDialogFrament.newInstance(new MyDialogFrament.OnCallDialog() {
            @Override
            public Dialog getDialog(Context context) {
                AlertDialog.Builder builder = new AlertDialog.Builder(context, LIST_THEME);
                builder.setTitle(title);
                builder.setItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if(resultListener != null){
                            resultListener.onDataResult(which);
                        }
                    }
                });
                return builder.create();
            }
        }, cancelable, null);
        dialogFragment.show(fragmentManager, LIST_TAG);
        return null;
    }

其中,列表的点击事件激活此回调接口,使用者,可以在需要的时候实现该接口,并且经过这一层的封装,代码看起来也更简洁,可读性更强!

你可能感兴趣的:(DialogFragment)