Dialog的简单封装

许久没来,突发奇想,想要把自己用到的一些项目分享出去,那就从最简单的一个开启:dialog的简单封装使用。

先上图,看下大致的用法:

new WrapperDialog(this) {
    @Override
    public int getLayoutRes() {
        return R.layout.dialog_normal;
    }

    @Override
    protected void setDialogParams(Dialog dialog) {
        setDialogParams(dialog, Gravity.CENTER);
    }

    @Override
    public void help(ViewHelper helper) {
        helper.setOnClickListener(R.id.tv_confirm, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }
}.show();

如上,只需要实现抽象方法即可。之前dialog里经常有圆角的问题,里面有解决方法。

封装的思想基于RecyclerView使用ViewHolder,复用View的控件,但dialog可能用不到fu'y复用这一思想,但我们可以用用其来提供控件的简单获取。那就简单封装下,取名为ViewHelper吧,如下

public class ViewHelper {
    private SparseArray views;
    public View itemView;

    public ViewHelper(final View itemView) {
        this.itemView = itemView;
        this.views = new SparseArray<>();
    }


    public View getItemView() {
        return itemView;
    }

    public  T getView(int viewId) {
        View view = views.get(viewId);
        if (view == null) {
            view = itemView.findViewById(viewId);
            views.put(viewId, view);
        }

        return (T) view;
    }
 public ViewHelper setText(int viewId, CharSequence value) {
     TextView view = getView(viewId);
     view.setText(value);
     return this;
 }
}

另外,Dialog就基于ViewHelper做点简单的封装,真的简单,你看下面:

public abstract class WrapperDialog implements OnViewHelper {

    protected Dialog dialog;
    protected Context context;
    protected ViewHelper helper;

    public WrapperDialog(Context context) {
        this(context, R.style.AlertTheme);
    }

    public WrapperDialog(Context context, int themeResId) {
        this.context = context;
        this.dialog = new Dialog(context, themeResId);
        dialog.setContentView(getHelperView(null, getLayoutRes(), this));
        setDialogParams(dialog);
    }


    /**
     * 实例化对应layoutId的view同时生成ViewHelper
     *
     * @param group    可为null
     * @param layoutId
     * @param listener
     * @return
     */
    protected View getHelperView(ViewGroup group, int layoutId, OnViewHelper listener) {
        helper = new ViewHelper(LayoutInflater.from(context).inflate(layoutId, group, false));
        if (listener != null) {
            listener.help(helper);
        }
        return helper.getItemView();
    }


    public WrapperDialog show() {
        if (dialog != null && !isShowing()) {
            dialog.show();
        }
        return this;
    }

    public WrapperDialog dismiss() {
        if (isShowing()) {
            dialog.dismiss();
        }
        return this;
    }

    public boolean isShowing() {
        if (dialog != null && dialog.isShowing()) {
            return true;
        }
        return false;
    }


    public Dialog getDialog() {
        return dialog;
    }

    /**
     * 设置参数的参考实现
     *
     * @param dialog
     * @param width
     * @param height
     * @param gravity
     */
    protected void setDialogParams(Dialog dialog, int width, int height, int gravity) {
        Window window = dialog.getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = width;
        params.height = height;
        window.setGravity(gravity);
        window.setAttributes(params);
    }


    /**
     * 设置参数的参考实现
     *
     * @param gravity
     */
    public void setDialogParams(Dialog dialog, int gravity) {
        Window window = dialog.getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.WRAP_CONTENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setGravity(gravity);
        window.setAttributes(params);
    }

    public abstract int getLayoutRes();

    /**
     * 设置dialog的参数
     *
     * @param dialog
     */
    protected abstract void setDialogParams(Dialog dialog);

    @Override
    public abstract void help(ViewHelper helper);


    public ViewHelper getViewHelper() {
        return helper;
    }

    public WrapperDialog addHelperCallback(HelperCallback callback) {
        callback.help(dialog, helper);
        return this;
    }

    public WrapperDialog addHelperAbsCallback(HelperAbsCallback callback) {
        callback.help(this, dialog, helper);
        return this;
    }

    public interface HelperCallback {
        /**
         * 默认帮助方式
         *
         * @param dialog 用来设置dialog的一些默认参数
         * @param helper 用于布局修改与实现
         */
        void help(Dialog dialog, ViewHelper helper);
    }


    public interface HelperAbsCallback {
        void help(WrapperDialog wrapper, Dialog dialog, ViewHelper helper);
    }

}

附图几张,以此实现的效果:

Dialog的简单封装_第1张图片

Dialog的简单封装_第2张图片

Dialog的简单封装_第3张图片

Dialog的简单封装_第4张图片

源码地址:点我

 

你可能感兴趣的:(Android)