Android UI篇——DialogFragment系列四之StatusDialog(Progress、Success、Error)實(shí)現(xiàn)

《DialogFragment系列一之源碼分析》
《DialogFragment系列二之Dialog封裝》
《DialogFragment系列三之AlertDialog實(shí)現(xiàn)》
《DialogFragment系列四之StatusDialog(Progress、Success、Error)實(shí)現(xiàn)》
《DialogFragment系列五之ItemDialog(eg:BottomDialog)實(shí)現(xiàn)》
《DialogFragment系列六之常見問題》

此篇基于Dialog定義一個StatusDialog(ProgressDialog、SuccessDialog、ErrorDialog)來滿足用戶操作過程中的各種狀態(tài),筆者將三種狀態(tài)集中在一個StatusDialog中,通過setType來切換,了解之前幾篇文章后代碼看起來就比較簡單,還是構(gòu)造者模式,還是extend Dialog,

效果圖:

ProgressDialog:
ProgressDialog.png
SuccessDialog:
SuccessDialog.png
ErrorDialog:
ErrorDialog.png

代碼實(shí)現(xiàn):

public class StatusDialog extends Dialog {

    private Handler mMainHandler = new Handler();
    /**
     * 成功或錯誤2s后dismiss
     */
    public static final int DELAY_TIME = 2000;
    private DialogParams mDialogParams;
    private Timer mDelayTimer;

    private StatusDialog() {
        mDialogParams = new DialogParams();
    }

    public static Builder with(AppCompatActivity activity) {
        return new Builder(activity);
    }

    @Override
    protected int setLayoutRes() {
        return R.layout.dialog_status;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        TextView tvDialogStatusContent = view.findViewById(R.id.tv_dialog_status_prompt);
        ImageView imgDialogStatusShow = view.findViewById(R.id.img_dialog_status_show);
        ProgressBar pbDialogStatusShow = view.findViewById(R.id.pb_dialog_status_show);

        //更換圓形進(jìn)度條顏色
        pbDialogStatusShow.getIndeterminateDrawable()
                .setColorFilter(getResources().getColor(R.color.colorPrimaryDark), PorterDuff.Mode.SRC_ATOP);

        //填充數(shù)據(jù)
        if (isNonEmpty(mDialogParams.prompt)) {
            tvDialogStatusContent.setText(mDialogParams.prompt);
        }

        switch (mDialogParams.type) {
            case Type.ERROR:
                pbDialogStatusShow.setVisibility(View.GONE);
                imgDialogStatusShow.setVisibility(View.VISIBLE);
                imgDialogStatusShow.setImageResource(R.mipmap.icon_dialog_error);
                break;
            case Type.SUCCESS:
                pbDialogStatusShow.setVisibility(View.GONE);
                imgDialogStatusShow.setVisibility(View.VISIBLE);
                imgDialogStatusShow.setImageResource(R.mipmap.icon_dialog_success);
                break;
            case Type.PROGRESS:
                imgDialogStatusShow.setVisibility(View.GONE);
                pbDialogStatusShow.setVisibility(View.VISIBLE);
                break;
        }
    }

    @Override
    protected boolean setCancelable() {
        return mDialogParams.isCancelable;
    }

    @Override
    public void onStart() {
        super.onStart();
        /**
         * 顯示定時間后自動dismiss
         */
        if (mDialogParams.type == Type.SUCCESS || mDialogParams.type == Type.ERROR) {
            cancelTimer();
            mDelayTimer = new Timer();
            mDelayTimer.schedule(new TimerTask() {
                @Override
                public void run() {
                    mMainHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            dismiss();
                        }
                    });
                }
            }, DELAY_TIME);
        }
    }

    private void cancelTimer() {
        if (mDelayTimer != null) {
            mDelayTimer.cancel();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        cancelTimer();
    }

    public class DialogParams {
        String prompt;
        boolean isCancelable;
        int type = -1;
    }

    public static class Builder {
        AppCompatActivity activity;
        DialogParams P;
        StatusDialog progressDialog;

        public Builder(AppCompatActivity activity) {
            progressDialog = new StatusDialog();
            this.P = progressDialog.mDialogParams;
            this.activity = activity;
        }

        public Builder setPrompt(String val) {
            P.prompt = val;
            return this;
        }

        public Builder setCancelable(boolean val) {
            P.isCancelable = val;
            return this;
        }

        public Builder setType(int val) {
            P.type = val;
            return this;
        }

        public StatusDialog show() {
            if (P.type == -1) {
                throw new IllegalArgumentException("Please set type");
            }
            progressDialog.show(activity);
            return progressDialog;
        }
    }

    public interface Type {

        int PROGRESS = 0x000000000211;

        int ERROR = 0x000000000985;

        int SUCCESS = 0x00000000011;

    }
}

SuccesDialog和ErrorDialog show后果幾秒自動dismiss,ProgressDialog可通過網(wǎng)絡(luò)請求集中封裝來show和dismiss,例如在網(wǎng)絡(luò)請求onStart()中調(diào)用show,onComplete()中調(diào)用dismiss,提供外部使用接口with(),讀者據(jù)此擴(kuò)展屬性或更換布局。

讀者有問或其他觀點(diǎn)請留言交流,共同進(jìn)步!

源碼Github傳送門,覺得不錯就給個star吧,謝謝!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容