listView的倒计时工具

来源:互联网 发布:域名投资人张尧 编辑:程序博客网 时间:2024/06/05 15:09
 @SuppressLint("HandlerLeak") private Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { switch (msg.what) { case 1: boolean isNeedCountTime = false; if (listRemove != null && !listRemove.isEmpty()) { listRemove.clear(); } // ①:其实在这块需要精确计算当前时间 for (int index = 0; index < list.size(); index++) { Map<String, String> mapinfo = list.get(index); if ("Y".equals(mapinfo.get("attentionStatus"))) { continue; } else { String miaoStr = mapinfo.get("addTime").toString(); if (miaoStr != null && miaoStr.length() > 0) { long start = System.currentTimeMillis() / 1000; long end = Long.parseLong(mapinfo.get("endTime") .toString()); long miao = end - start; Date date = new Date(start * 1000); Date date2 = new Date(end * 1000); long temp = date2.getTime() - date.getTime(); // 相差毫秒数 long hours = temp / 1000 / 3600; // 相差小时数 long temp2 = temp % (1000 * 3600); long mins = temp2 / 1000 / 60; // 相差分钟数 long second1 = (temp / 1000 - hours * 60 * 60 - mins * 60); String time = hours + ":" + mins + ":" + second1; if (miao > 1000) {// 判断是否还有条目能够倒计时,如果能够倒计时的话,延迟一秒,让它接着倒计时 isNeedCountTime = true; mapinfo.put("addTime", time + ""); } else { listRemove.add(mapinfo); mapinfo.put("addTime", "0"); listRemove.add(mapinfo); } } else { listRemove.add(mapinfo); mapinfo.put("addTime", "0"); } } } if (listRemove != null && !listRemove.isEmpty()) { for (Map<String, String> mapremove : listRemove) { list.remove(mapremove); } } // ②:for循环执行的时间 notifyDataSetChanged(); if (isNeedCountTime) { // 然后用1000,就赢延迟的时间 handler.sendEmptyMessageDelayed(1, 1000); } break; } } };
在整理的时候发现还有这样的一块代码
0 0
原创粉丝点击