《DialogFragment系列一之源碼分析》
《DialogFragment系列二之Dialog封裝》
《DialogFragment系列三之AlertDialog實現(xiàn)》
《DialogFragment系列四之StatusDialog(Progress、Success虐呻、Error)實現(xiàn)》
《DialogFragment系列五之ItemDialog(eg:BottomDialog)實現(xiàn)》
《DialogFragment系列六之常見問題》
此篇基于Dialog定義一個StatusDialog(ProgressDialog象泵、SuccessDialog、ErrorDialog)來滿足用戶操作過程中的各種狀態(tài)斟叼,筆者將三種狀態(tài)集中在一個StatusDialog中偶惠,通過setType來切換,了解之前幾篇文章后代碼看起來就比較簡單犁柜,還是構(gòu)造者模式洲鸠,還是extend Dialog,
效果圖:
ProgressDialog:
SuccessDialog:
ErrorDialog:
代碼實現(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);
//更換圓形進度條顏色
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ù)此擴展屬性或更換布局皆的。
讀者有問或其他觀點請留言交流覆履,共同進步!