《DialogFragment系列一之源码分析》
《DialogFragment系列二之Dialog封装》
《DialogFragment系列三之AlertDialog实现》
《DialogFragment系列四之StatusDialog(Progress、Success、Error)实现》
《DialogFragment系列五之ItemDialog(eg:BottomDialog)实现》
《DialogFragment系列六之常见问题》
此篇基于Dialog定义一个StatusDialog(ProgressDialog、SuccessDialog、ErrorDialog)来满足用户操作过程中的各种状态,笔者将三种状态集中在一个StatusDialog中,通过setType来切换,了解之前几篇文章后代码看起来就比较简单,还是构造者模式,还是extend Dialog,
效果图:
ProgressDialog:
SuccessDialog:
ErrorDialog:
代码实现:
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);
//填充数据
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可通过网络请求集中封装来show和dismiss,例如在网络请求onStart()中调用show,onComplete()中调用dismiss,提供外部使用接口with(),读者据此扩展属性或更换布局。
读者有问或其他观点请留言交流,共同进步!