統一的dialog,4種統一樣式。


class DialogViewHolder {
    private SparseArray<View> mViews;
    private View mConvertView;

    private DialogViewHolder(View view) {
        mConvertView = view;
        mViews = new SparseArray<>();
    }

    public static DialogViewHolder create(View view) {
        return new DialogViewHolder(view);
    }

    /**
     * 獲取view
     *
     * @param viewId
     * @param <T>
     * @return
     */
    public <T extends View> T getView(@IdRes int viewId) {
        View view = mViews.get(viewId);
        if (view == null) {
            view = mConvertView.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (T) view;
    }

    /**
     * 設置文本
     *
     * @param viewId
     * @param text
     */
    public void setText(int viewId, String text) {
        TextView textView = getView(viewId);
        textView.setText(text);
    }

    /**
     * 設置字體顏色
     *
     * @param viewId
     * @param colorId
     */
    public void setTextColor(int viewId, int colorId) {
        TextView textView = getView(viewId);
        textView.setTextColor(colorId);
    }

    /**
     * 設置背景圖片
     *
     * @param viewId
     * @param resId
     */
    public void setBackgroundResource(int viewId, int resId) {
        View view = getView(viewId);
        view.setBackgroundResource(resId);
    }

    /**
     * 設置背景顏色
     *
     * @param viewId
     * @param colorId
     */
    public void setBackgroundColor(int viewId, int colorId) {
        View view = getView(viewId);
        view.setBackgroundColor(colorId);
    }

    /**
     * 設置點擊事件
     *
     * @param viewId
     * @param listener
     */
    public void setOnClickListener(int viewId, View.OnClickListener listener) {
        View view = getView(viewId);
        view.setOnClickListener(listener);
    }

    /**
     * 設置控件顯示隱藏
     * @param viewId
     * @param visibility
     */
    public void setShowView(int viewId, int visibility) {
        View view = getView(viewId);
        view.setVisibility(visibility);
    }
}

public abstract class BaseDialog extends DialogFragment {

    @LayoutRes
    protected int mLayoutResId;
    private float mDimAmount = 0.0f;//背景透明
    private boolean mShowBottomEnable;//是否底部顯示
    private int mMargin = 0;//左右邊距
    public boolean mOutCancel = true;  //點擊外部關閉dialog
    private int mAnimStyle = R.style.sitech_window_anim;//進入退出動畫
    private int mWidth;
    private int mHeight;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.BaseDialog);
        mLayoutResId = setLayoutId();

        setCancelable(true);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(mLayoutResId, container, false);
        convertView(DialogViewHolder.create(view), this);
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        initParams();
    }

    private void initParams() {
        Window window = getDialog().getWindow();
        if (window == null) {
            WindowManager.LayoutParams params = window.getAttributes();
            window.setBackgroundDrawableResource(android.R.color.transparent);
            params.dimAmount = mDimAmount;

            //設置dialog顯示位置
            if (mShowBottomEnable) {
                params.gravity = Gravity.BOTTOM;
            }

            //設置dialog寬度
            if (mWidth == 0) {
                params.width = getScreenWidth(getContext()) - 2 * dp2px(getContext(), mMargin);
            } else {
                params.width = dp2px(getContext(), mWidth);
            }

            //設置dialog高度
            if (mHeight == 0) {
                params.height = WindowManager.LayoutParams.WRAP_CONTENT;
            } else {
                params.height = dp2px(getContext(), mHeight);
            }

            //設置dialog動畫
            if (mAnimStyle != 0) {
                window.setWindowAnimations(mAnimStyle);
            }
            window.setAttributes(params);
        }
    }

    /**
     * 設置背景昏暗度
     *
     * @param dimAmount
     * @return
     */
    public BaseDialog setDimAmout(@FloatRange(from = 0, to = 1) float dimAmount) {
        mDimAmount = dimAmount;
        return this;
    }

    /**
     * 是否顯示底部
     *
     * @param showBottom
     * @return
     */
    public BaseDialog setShowBottom(boolean showBottom) {
        mShowBottomEnable = showBottom;
        return this;
    }

    /**
     * 設置寬高
     *
     * @param width
     * @param height
     * @return
     */
    public BaseDialog setSize(int width, int height) {
        mWidth = width;
        mHeight = height;
        return this;
    }

    /**
     * 設置左右margin
     *
     * @param margin
     * @return
     */
    public BaseDialog setMargin(int margin) {
        mMargin = margin;
        return this;
    }

    /**
     * 設置進入退出動畫
     *
     * @param animStyle
     * @return
     */
    public BaseDialog setAnimStyle(@StyleRes int animStyle) {
        mAnimStyle = animStyle;
        return this;
    }

    /**
     * 設置是否點擊外部取消
     *
     * @param outCancel
     * @return
     */
    public BaseDialog setOutCancel(boolean outCancel) {
        mOutCancel = outCancel;
        return this;
    }

    public BaseDialog show(FragmentManager manager) {
        super.show(manager, String.valueOf(System.currentTimeMillis()));
        return this;
    }

    /**
     * 設置Dialog佈局
     *
     * @return
     */
    public abstract int setLayoutId();

    /**
     * 操作dialog佈局
     *
     * @param holder
     * @param dialog
     */
    public abstract void convertView(DialogViewHolder holder, BaseDialog dialog);


    /**
     * 獲取屏幕寬度
     *
     * @param context
     * @return
     */
    public static int getScreenWidth(Context context) {
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        return displayMetrics.widthPixels;
    }

    public static int dp2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }
}
/***
 * 自定義Dialog
 * @date 2019/4/28
 *
 * 使用步驟如下:
 *   CustomDialog.newInstance().setViewData("標題","內容區域.....",()->{
 *                 Toast.makeText(this,"取消",Toast.LENGTH_SHORT).show();
 *             },()->{
 *                 Toast.makeText(this,"確定",Toast.LENGTH_SHORT).show();
 *             }).setOutCancel(false).show(getSupportFragmentManager());
 */
public class CustomDialog extends BaseDialog {
    public enum DIALOG_TYPE {
        NULL,
        ONE_LINE,
        MORE_LINE,
        ONE_BUTTON,
        TWO_BUTTON
}

    private DIALOG_TYPE mType;
    private String mTitle;
    private String mContent;
    private OnOkClickListener mOnOkClickListener;
    private OnCancelClickListener mOnCancelClickListener;

    public static CustomDialog newInstance() {
        return new CustomDialog();
    }

    public CustomDialog setViewData(String title) {
        mType = DIALOG_TYPE.ONE_LINE;
        this.mTitle = title;
        return this;
    }

    public CustomDialog setViewData(String title, String content) {
        mType = DIALOG_TYPE.MORE_LINE;
        this.mTitle = title;
        this.mContent = content;
        return this;
    }

    public CustomDialog setViewData(String title, String content, OnCancelClickListener onCancelClickListener) {
        mType = DIALOG_TYPE.ONE_BUTTON;
        this.mTitle = title;
        this.mContent = content;
        this.mOnCancelClickListener = onCancelClickListener;
        return this;
    }

    public CustomDialog setViewData(String title, String content, OnCancelClickListener onCancelClickListener, OnOkClickListener onOkClickListener) {
        mType = DIALOG_TYPE.TWO_BUTTON;
        this.mTitle = title;
        this.mContent = content;
        this.mOnOkClickListener = onOkClickListener;
        this.mOnCancelClickListener = onCancelClickListener;
        return this;
    }

    @Override
    public int setLayoutId() {
        return R.layout.dialog_confirm;
    }

    @Override
    public void convertView(DialogViewHolder holder, final BaseDialog dialog) {
        switch (mType) {
            case ONE_LINE:
                showDialogView(holder, R.id.rl_1);
                break;
            case MORE_LINE:
                showDialogView(holder, R.id.rl_2);
                break;
            case ONE_BUTTON:
                showDialogView(holder, R.id.rl_3);
                break;
            case TWO_BUTTON:
                showDialogView(holder, R.id.rl_4);
                break;
            default:
                dialog.dismiss();
                break;
        }
        setData(holder, dialog);
    }

    private void setData(DialogViewHolder holder, BaseDialog dialog) {
        if (TextUtils.isEmpty(mTitle)) {
            new RuntimeException("y must call setViewData() first...");
        }

        //單行
        if (!TextUtils.isEmpty(mTitle) && TextUtils.isEmpty(mContent)) {
            holder.setText(R.id.tv_1, mTitle);
        }

        //多行
        if (!TextUtils.isEmpty(mTitle) && !TextUtils.isEmpty(mContent) && null == mOnCancelClickListener) {
            holder.setText(R.id.tv_title_2, mTitle);
            holder.setText(R.id.tv_content_2, mContent);
        }

        //單個按鈕
        if (!TextUtils.isEmpty(mTitle) && !TextUtils.isEmpty(mContent) && null != mOnCancelClickListener && null == mOnOkClickListener) {
            holder.setText(R.id.tv_title_3, mTitle);
            holder.setText(R.id.tv_content_3, mContent);
            holder.setOnClickListener(R.id.tv_cancel_3, v -> {
                dialog.dismiss();
                mOnCancelClickListener.onCancelClick();
                clearData();
            });
        }

        //兩個按鈕
        if (!TextUtils.isEmpty(mTitle) && !TextUtils.isEmpty(mContent) && null != mOnCancelClickListener && null != mOnOkClickListener) {
            holder.setText(R.id.tv_title_4, mTitle);
            holder.setText(R.id.tv_content_4, mContent);
            holder.setOnClickListener(R.id.tv_cancel_4, v -> {
                dialog.dismiss();
                mOnCancelClickListener.onCancelClick();
                clearData();
            });
            holder.setOnClickListener(R.id.tv_ok_4, v -> {
                dialog.dismiss();
                mOnOkClickListener.onOKClick();
                clearData();
            });
        }

        if (mOutCancel) holder.setOnClickListener(R.id.cl,v->dialog.dismiss());
    }

    private void clearData() {
        mType = DIALOG_TYPE.NULL;
        this.mTitle = "";
        this.mContent = "";
        this.mOnOkClickListener = null;
        this.mOnCancelClickListener = null;
    }

    /**
     * 顯示對應樣式的控件
     *
     * @param holder
     */
    private void showDialogView(DialogViewHolder holder, int viewId) {
        holder.setShowView(R.id.rl_1, R.id.rl_1 == viewId ? View.VISIBLE : View.GONE);
        holder.setShowView(R.id.rl_2, R.id.rl_2 == viewId ? View.VISIBLE : View.GONE);
        holder.setShowView(R.id.rl_3, R.id.rl_3 == viewId ? View.VISIBLE : View.GONE);
        holder.setShowView(R.id.rl_4, R.id.rl_4 == viewId ? View.VISIBLE : View.GONE);
    }


    public interface OnOkClickListener {
        void onOKClick();
    }

    public interface OnCancelClickListener {
        void onCancelClick();
    }
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/cl"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <!--一行文字-->
    <RelativeLayout
        android:id="@+id/rl_1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:background="@drawable/shape_custom_dialog_bg"
        android:visibility="gone"
        android:clickable="true"
        android:layout_width="700dp"
        android:layout_height="228dp">

        <TextView
            android:id="@+id/tv_1"
            android:textColor="#fff"
            android:text="無標題一行文字"
            android:textSize="30dp"
            android:layout_centerInParent="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RelativeLayout>

    <!--兩至多行文字-->
    <RelativeLayout
        android:id="@+id/rl_2"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:background="@drawable/shape_custom_dialog_bg"
        android:visibility="gone"
        android:clickable="true"
        android:layout_width="700dp"
        android:layout_height="248dp">

        <TextView
            android:id="@+id/tv_title_2"
            android:text="標題文字"
            android:textSize="30dp"
            android:textColor="#fff"
            android:layout_marginTop="32dp"
            android:layout_centerHorizontal="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/tv_content_2"
            android:layout_marginTop="105dp"
            android:layout_marginLeft="57dp"
            android:layout_marginRight="57dp"
            android:textColor="#fff"
            android:text="內容》》qqqqqqqqqqqq》"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </RelativeLayout>

    <!--包含一個按鈕-->
    <RelativeLayout
        android:id="@+id/rl_3"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:background="@drawable/shape_custom_dialog_bg"
        android:visibility="gone"
        android:clickable="true"
        android:layout_width="700dp"
        android:layout_height="329dp">

        <TextView
            android:id="@+id/tv_title_3"
            android:text="標題文字"
            android:textSize="30dp"
            android:textColor="#fff"
            android:layout_marginTop="32dp"
            android:layout_centerHorizontal="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/tv_content_3"
            android:layout_marginTop="105dp"
            android:layout_marginLeft="57dp"
            android:layout_marginRight="57dp"
            android:textColor="#fff"
            android:text="內容》》qqqqqqqqqqqq》"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <View
            android:background="#fff"
            android:layout_above="@id/tv_cancel_3"
            android:layout_width="match_parent"
            android:layout_height="2dp"/>

        <TextView
            android:id="@+id/tv_cancel_3"
            android:layout_alignParentBottom="true"
            android:textColor="#fff"
            android:text="知道了"
            android:textSize="32dp"
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="75dp" />

    </RelativeLayout>

    <!--包含兩個按鈕-->
    <RelativeLayout
        android:id="@+id/rl_4"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:background="@drawable/shape_custom_dialog_bg"
        android:clickable="true"
        android:layout_width="700dp"
        android:layout_height="329dp">

        <TextView
            android:id="@+id/tv_title_4"
            android:text="標題文字"
            android:textSize="30dp"
            android:textColor="#fff"
            android:layout_marginTop="32dp"
            android:layout_centerHorizontal="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/tv_content_4"
            android:layout_marginTop="105dp"
            android:layout_marginLeft="57dp"
            android:layout_marginRight="57dp"
            android:textColor="#fff"
            android:text="內容》》qqqqqqqqqqqq》"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <View
            android:background="#fff"
            android:layout_above="@id/ll_btn_4"
            android:layout_width="match_parent"
            android:layout_height="2dp"/>

       <LinearLayout
           android:id="@+id/ll_btn_4"
           android:layout_alignParentBottom="true"
           android:layout_width="match_parent"
           android:layout_height="75dp">

           <TextView
               android:id="@+id/tv_cancel_4"
               android:textColor="#fff"
               android:text="取消"
               android:textSize="32dp"
               android:gravity="center"
               android:layout_weight="1"
               android:layout_width="0dp"
               android:layout_height="match_parent" />

           <View
               android:layout_width="2dp"
               android:layout_height="match_parent"
               android:background="#fff"/>

           <TextView
               android:id="@+id/tv_ok_4"
               android:textColor="#fff"
               android:text="確認"
               android:textSize="32dp"
               android:layout_weight="1"
               android:gravity="center"
               android:layout_width="0dp"
               android:layout_height="match_parent" />
       </LinearLayout>

    </RelativeLayout>

</android.support.constraint.ConstraintLayout>

同事在用的時候,遇到點問題,但在測試的時候沒遇到問題。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章