目前我们的app启动时候有多个跳转流程,有免责声明,有激活流程,有续航提醒,有消息提醒,电量提醒等等,目前项目有两处需要优化
1 这些个操作有的项目是用的动态添加view的方式,有的是 dialog,有的项目是通过fragmentdialog来做的,比较多样。
2 这些流程分散个各个地方,难以管理,但他们之间有联系,有顺序,也有依赖关系。
这里通过固定下通用的操作 show,close,retry等,然后是将这些view统一管理,方便维护 类其实也是很简单的,
1 看下管理类:
public class ViewManager implements LifecycleObserver , View.OnClickListener {
public static final String TAG = "ViewManager";
public static final int TYPE_DEFAUT = 9999;
public static final int TYPE_ACTIVE = 0;
public static final int TYPE_AGREE = 1;
public static final int TYPE_MESSAGE = 2;
public static final int TYPE_RENAVI = 3;
//优先级:高优先级覆盖低优先级
private int currentType = TYPE_DEFAUT;
private void resetStatus(){
currentType = TYPE_DEFAUT;
}
//之所以转换以下onclick,主要是需要resetStatus 恢复默认优先级
@Override
public void onClick(View v) {
mView.onClick(v);
resetStatus();
}
public interface DialogView{
boolean bootshow();
boolean bootclose();
void onClick(View view);
}
DialogView mView;
public void showView(int viewType){
if(viewType > currentType){
Log.e(TAG,"优先级低,不显示"+currentType+":"+viewType);
return;
}else if(currentType != TYPE_DEFAUT){
closeView(currentType);
}
if(viewType == TYPE_ACTIVE) {
//mView = new ActiveDialogView();
}else if(viewType == TYPE_AGREE){
mView = new AgreementDialog(ContextHodler.instance(),this);
}
currentType = viewType;
mView.bootshow();
}
public void closeView(int viewType){
mView.bootclose();
currentType = TYPE_DEFAUT;
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
public void onStop() {
mView.bootclose();
currentType = TYPE_DEFAUT;
}
}
2 看下view的基类,基于dialog(view,fragmentdialog省略):
public abstract class BaseDialog extends AlertDialog implements ViewManager.DialogView{
protected String TAG = getClass().getSimpleName();
View.OnClickListener dialogAction ;
View sucessView,failView;
public BaseDialog(Context context, View.OnClickListener action) {
super(context);
dialogAction = action;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setCanceledOnTouchOutside(false);
if (null != getWindow()) {
getWindow().setBackgroundDrawable(null);
}
initView();
sucessView = getSucessView();
failView = getFailView();
sucessView.setOnClickListener(dialogAction);
failView.setOnClickListener(dialogAction);
}
//初始化view
abstract void initView();
//获取成功按钮
abstract View getSucessView();
//获取失败按钮
abstract View getFailView();
//点击成功按钮响应事件
abstract void onActionSucess();
//点击失败按钮事件
abstract void onActionFail();
@SuppressLint("NonConstantResourceId")
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_agree: {
onActionSucess();
break;
}
case R.id.iv_not_show: {
onActionFail();
break;
}
}
}
@Override
public boolean bootshow() {
this.show();
return true;
}
@Override
public boolean bootclose() {
this.cancel();
return true;
}
}
3 看下具体实现类:
public class AgreementDialog extends BaseDialog implements ViewManager.DialogView{
protected String TAG = getClass().getSimpleName();
private TextView mTitle, mTips, mBtnAgree, mTxtRemind;
private ImageView mNotShow;
private volatile boolean isCheck;
private static ObservableData