FIFO队列管理多个Dialog显示

项目中我们有时会遇到这样一种场景,首页中加载数据要弹出一个加载对话框,之后会弹出一个定位城市的选择确认框,或者个人喜好设置,或者感兴趣的栏目订阅选择对话框。为了便于管理dialog,取消前一个对话框后再显示下一个对话框,我们可以用FIFO队列对dialog进行排队。
   
   
private Queque < Dialog > dialogQueue = new LinkedList <>();
private Dialog currentDialog = null ; // 当前显示的对话框
private void showDialog ( Dialog dialog ) {
if ( dialog != null ) {
dialogQueue . offer ( dialog );
}
if ( currentDialog == null ) {
currentDialog = dialogQueue . poll ();
if ( currentDialog != null ) {
currentDialog . show ();
currentDialog . setOnDismissListener ( new DialogInterface . OnDismissListener () {
@Override
public void onDismiss ( DialogInterface dialog ) {
currentDialog = null ;
showDialog ( null );
}
});
}
}
}
OK,这样只需要在我们需要显示对话框的时候调用showDialog()方法就可以了,是不是很方便呢~~

Java中,队列的使用率很高,多数生产消费模型的首选数据结构就是队列。Queue接口与List、Set接口同一级别,都是继承了Collection接口,LinkedList实现了Queue接口。队列有两个基本操作:在队列尾部加入一个元素,和从队列头部移动一个元素。也就是说,队列以一种先进先出的方式管理数据,如果你试图向一个已经满了的阻塞队列中添加一个元素或者从一个空的阻塞队列中移除一个元素,将导致线程阻塞。
Queue使用时要尽量避免Collection接口的add()和remove()方法。最好入队使用 offer() ,出队使用poll() ,不会抛出异常。

add                增加一个元索                                              如果队列已满,则抛出一个IIIegaISlabEepeplian异常
remove   移除并返回队列头部的元素        如果队列为空,则抛出一个NoSuchElementException异常
offer      添加一个元素并返回true                如果队列已满,则返回false
poll                  移除并返问队列头部的元素         如果队列为空,则返回null
   
   
/**
* Inserts the specified element into this queue if it is possible to do
* so immediately without violating capacity restrictions.
* When using a capacity-restricted queue, this method is generally
* preferable to {@link #add}, which can fail to insert an element only
* by throwing an exception.
*
* @param e the element to add
* @return <tt>true</tt> if the element was added to this queue, else
* <tt>false</tt>
* @throws ClassCastException if the class of the specified element
* prevents it from being added to this queue
* @throws NullPointerException if the specified element is null and
* this queue does not permit null elements
* @throws IllegalArgumentException if some property of this element
* prevents it from being added to this queue
*/
boolean offer(E e);
   
   
/**
* Retrieves and removes the head of this queue,
* or returns <tt>null</tt> if this queue is empty.
*
* @return the head of this queue, or <tt>null</tt> if this queue is empty
*/
E poll();
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一个通用的Android端弹窗管理框架,内部维护弹窗优先级队列 具备弹窗管理扩展功能 整合Dialog,PoupoWindow,悬浮Widget,透明Webview,Toast,SnackBar,无需再为繁琐的业务弹窗逻辑所困扰如何添加依赖只需要两行代码轻松接入//add this to your repositories  maven { url 'https://www.jitpack.io' } //add this to your dependencies implementation 'com.github.MrCodeSniper:PopLayer:2.0.0'具体如何使用1.根据策略创建对应的弹窗view//Dialog形式 PopLayerView  mLayerView = new PopLayerView(this,R.layout.common_dialog_upgrade_app); //透明Webview形式 PopLayerView mLayerView = new PopLayerView(this,LayerConfig.redPocketScheme);2.开始装配弹窗配置Popi mUpgradePopi1 = new Popi.Builder()                 .setmPopId(4)//弹窗的唯一标识 当id发生改变 视为新的弹窗                 .setmPriority(2)//优先级这里不具体划分对应的范围 值越小优先级越高                 .setmCancelType(TRIGGER_CANCEL)//弹窗消失的类型分为 TRIGGER_CANCEL(触摸消失) COUNTDOWN_CANCEL (延时消失)                 .setMaxShowTimeLength(5)//最长显示时间(S)                 .setMaxShowCount(5)//最大显示次数                 .setmBeginDate(1548858028)//开始时间 2019-01-30 22:20:28                 .setmEndDate(1548944428)//结束时间 2019-01-31 22:20:28                 .setLayerView(mLayerView)//弹窗View                 .build();3.纳入弹窗管理//纳入弹窗管理 PopManager.getInstance().pushToQueue(mUpgradePopi); //开始显示弹窗 PopManager.getInstance().showNextPopi();效果预览未来的计划逐步统一 其他类型的弹窗 希望能提供给大家一个较为全面的应对业务需求的弹窗管理框架版本记录V1方案版本号LOG进度更新V1.0.0项目开源,完成弹窗管理Dialog形式扩展Dialog策略扩展完成V1.0.1修复Dialog策略无法获取dialog实体bugDialog策略优化V1.0.2修复activity摧毁造成的弹窗异常 bugDialog策略优化V1.0.3优化了弹窗的使用更加方便快捷框架使用优化V2方案版本号LOG进度更新V2.0.0正式加入透明Webview弹窗策略扩展透明Webview策略扩展完成作者介绍Hello 我叫lalala,如果您喜欢这个项目 请给个star 能follow我那真是太好了!!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值