RecyclerView中怎么实现列表倒计时
创始人
2024-12-22 18:01:20
0次

首先看下实现的最终效果

如何显示列表我相信大家都会,这里我只附上和倒计时功能实现的adapter类。

public class ClockAdapter extends RecyclerView.Adapter { private SparseArray countDownMap = new SparseArray<>(); @Override public ClockViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {  View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv, parent, false);  return new ClockViewHolder(view); } /**  * 清空资源  */ public void cancelAllTimers() {  if (countDownMap == null) {   return;  }  for (int i = 0,length = countDownMap.size(); i < length; i++) {   CountDownTimer cdt = countDownMap.get(countDownMap.keyAt(i));   if (cdt != null) {    cdt.cancel();   }  } } @Override public void onBindViewHolder(final ClockViewHolder holder, int position) {  long betweenDate;  if (position == 0) {   betweenDate= DateUtil.getLeftTime("2017-8-8 12:10:10");  } else {   betweenDate= DateUtil.getLeftTime("2017-8-9 15:10:10");  }  if (holder.countDownTimer != null) {   holder.countDownTimer.cancel();  }  if (betweenDate > 0) {   holder.countDownTimer = new CountDownTimer(betweenDate, 1000) {    public void onTick(long millisUntilFinished) {     millisUntilFinished = millisUntilFinished / 1000;     int hours = (int) (millisUntilFinished / (60 * 60));     int leftSeconds = (int) (millisUntilFinished % (60 * 60));     int minutes = leftSeconds / 60;     int seconds = leftSeconds % 60;     final StringBuffer sBuffer = new StringBuffer();     sBuffer.append(addZeroPrefix(hours));     sBuffer.append(":");     sBuffer.append(addZeroPrefix(minutes));     sBuffer.append(":");     sBuffer.append(addZeroPrefix(seconds));     holder.clock.setText(sBuffer.toString());    }    public void onFinish() {//     时间结束后进行相应逻辑处理    }   }.start();   countDownMap.put(holder.clock.hashCode(), holder.countDownTimer);  } else {//   时间结束 进行相应逻辑处理  } } @Override public int getItemCount() {  return 25; } class ClockViewHolder extends RecyclerView.ViewHolder {  TextView clock;  CountDownTimer countDownTimer;  public ClockViewHolder(View itemView) {   super(itemView);   clock = (TextView) itemView.findViewById(R.id.clock);  } }}

其中cancelAllTimer()这个方法解决了内存的问题,通过这行代码,将item的hashcode作为key设入SparseArray中,这样在cancelAllTimer方法中可以一个一个取出来进行倒计时取消操作。

countDownMap.put(holder.clock.hashCode(),holder.countDownTimer);

接着通过下面这行代码新建一个CountDownTimer类

holder.countDownTimer = new CountDownTimer(betweenDate, 1000) { public void onTick(long millisUntilFinished) { millisUntilFinished = millisUntilFinished / 1000; int hours = (int) (millisUntilFinished / (60 * 60)); int leftSeconds = (int) (millisUntilFinished % (60 * 60)); int minutes = leftSeconds / 60; int seconds = leftSeconds % 60; final StringBuffer sBuffer = new StringBuffer(); sBuffer.append(addZeroPrefix(hours)); sBuffer.append(":")   sBuffer.append(addZeroPrefix(minutes));     sBuffer.append(":");     sBuffer.append(addZeroPrefix(seconds));     holder.clock.setText(sBuffer.toString());}public void onFinish() {// 时间结束后进行相应逻辑处理}}.start();

分析它的源码

public CountDownTimer(long millisInFuture, long countDownInterval) {  mMillisInFuture = millisInFuture;  mCountdownInterval = countDownInterval; }

从中可以很清楚的看出,设置了两个值,第一个是倒计时结束时间,第二个是刷新时间的间隔时间。 然后通过start方法进行启动,接着看下start方法中进行的处理

public synchronized final CountDownTimer start() {  mCancelled = false;  if (mMillisInFuture <= 0) {   onFinish();   return this;  }  mStopTimeInFuture = SystemClock.elapsedRealtime() + mMillisInFuture;  mHandler.sendMessage(mHandler.obtainMessage(MSG));  return this; }

源码中,当倒计时截止时间小于等0时也就是倒计时结束时,调用了onFinish方法,若时间还未结束,则通过handler的异步消息机制,将消息进行发出,通过一整个流程,最终方法会走到handler的handleMessage方法中,如果有不熟悉这个异步流程的伙伴,可以去看我以前写的一篇异步消息机制的文章 android异步消息机制,源码层面彻底解析。好了,接下来就来看看handler的handleMessage方法。

private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) {  synchronized (CountDownTimer.this) {  if (mCancelled) {   return;  }  final long millisLeft = mStopTimeInFuture - SystemClock.elapsedRealtime();  if (millisLeft <= 0) {   onFinish();  } else if (millisLeft < mCountdownInterval) {  // no tick, just delay until done  sendMessageDelayed(obtainMessage(MSG), millisLeft);  } else {long lastTickStart=SystemClock.elapsedRealtime();   onTick(millisLeft); // take into account user's onTick taking time to execute long delay = lastTickStart + mCountdownInterval - SystemClock.elapsedRealtime();// special case: user's onTick took more than interval to// complete, skip to next interval while (delay < 0) delay += mCountdownInterval;  sendMessageDelayed(obtainMessage(MSG), delay);    }   }  } };

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...