亲宝软件园·资讯

展开

android dialog根据弹窗等级排序显示的代码实例

影魔 人气:0

背景:由于主界面可能弹窗很多弹窗,需求要求某个dialog必须在前面显示,如果再消失监听中,线性判断,每个dialog都去判断工作量巨大,且不易阅读。所以做了一个弹窗排序的项目。这里只是讲解单个activity中的弹窗排序,demo中还有维护多个activity 弹窗排序,详细看最下面github

做到工具组件化,不侵入原项目代码,无需继承,使用方便。

原理:当有一个dialog添加显示时,判断等级是否大于,大于则显示,否则则添加到等待队列

private void show(int level, Dialog dialog, DialogInterface.OnDismissListener onDismissListener){
 if(dialog == null){
  return;
 }
 DialogLevelModel topDialog = getTopShowingDialog();
 DialogLevelModel inModel = new DialogLevelModel(level, dialog, activity);
 new DismissListenerBinder(inModel,this,onDismissListener).bind();
 if(topDialog ==null || level >= topDialog.level){
  realShow(inModel);
 }else {
  waitList.sortAdd(new DialogLevelModel(level, dialog, activity));
 }
}

添加集合中要根据等级排序

public boolean sortAdd(DialogLevelModel inModel){
 Log.e("DLArrayList","add---"+inModel.level);
 int lastSize = size();
 if(lastSize == 0){
  return super.add(inModel);
 }
 int index = 0;
 for (int i = size()-1; i >=0; i--){
  DialogLevelModel temp = get(i);
 
  if(inModel.level >= temp.level){
   index = i+1;
   break;
  }
 }
 super.add(index,inModel);
 return true;
}

显示的时候,判断activity是否存在,整理集合

private void realShow(DialogLevelModel dialogLevelModel){
 waitList.remove(dialogLevelModel);
 showList.add(dialogLevelModel);
 if(dialogLevelModel.activity != null && dialogLevelModel.activity.get() != null && !dialogLevelModel.activity.get().isFinishing()){
  dialogLevelModel.dialog.show();
 }else{
  destory();
 }
}

收到dialog消失监听,整理集合,判断最上层dialog 和等待队列dialog等级对比

@Override
public void onDismiss(DialogLevelModel dialogLevelModel) {
 for(int i = showList.size()-1;i>=0;i--){
  if(showList.get(i).dialog == dialogLevelModel.dialog){
   showList.remove(i);
   break;
  }
 }
 
 boolean flag =true;
 while (flag) {
  if(waitList.size() == 0){
   break;
  }
  DialogLevelModel tempModel = waitList.get(waitList.size() -1);
  if (getTopShowingDialog() == null) {
   realShow(tempModel);
  } else if (tempModel.level >= getTopShowingDialog().level) {
   realShow(tempModel);
  } else {
   break;
  }
 }
}

调用:

for(int i = 0;i<15;i++){
 final int level2 = (int)(1+Math.random()*1000);
 final Dialog dialog2 = showCommonDialog("content:level-"+level2);
 Log.e("dialoglevl","level:"+level2);
 if(i ==13){
  dialogLevelManager.showDelay(level2,2000,dialog2,MainActivity.this);
  continue;
 }
 if(i ==14){
  dialogLevelManager.showDelay(level2,3000,dialog2,MainActivity.this);
  continue;
 }
 dialogLevelManager.show(level2,dialog2,MainActivity.this);
}

结果展示:

这里只是单个activity中添加排列显示dialog的部分重要代码,要想要全部代码,下面github

DialogLevel如果感觉有用,请star哦

加载全部内容

相关教程
猜你喜欢
用户评论