RecyclerView实现列表倒计时

RecyclerView实现列表倒计时,第1张

概述RecyclerView实现列表倒计时 最近在做一个项目,需要用到列表倒计时功能,捣鼓半天终于弄了出来,在安卓中实现这个效果需要用到Countdowntimer,通过这个类的使用,不仅可以实现倒计时的效果,还可以完美解决在实现倒计时过程中的两个bug. 1.内存问题 2.由于recyclerview的item复用导致不同条目的时间错乱 首先看下实现的最终效果 如何显示列表我相信大家都会,这里我只附上和倒计时功能实现的adapter类. public class ClockAdapter extends RecyclerView.Ada

最近在做一个项目,需要用到列表倒计时功能,捣鼓半天终于弄了出来,在安卓中实现这个效果需要用到Countdowntimer,通过这个类的使用,不仅可以实现倒计时的效果,还可以完美解决在实现倒计时过程中的两个BUG。

1.内存问题
2.由于recyclervIEw的item复用导致不同条目的时间错乱

首先看下实现的最终效果

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

public class ClockAdapter extends RecyclerVIEw.Adapter<ClockAdapter.ClockVIEwHolder> { private SparseArray<CountDownTimer> 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);    }   }  } };

相信这段源码还是很通熟易懂,首先计算出剩余时间,如果剩余时间小于刷新时间,就发送一条延时消息直到时间结束,如果剩余时间大于刷新时间就调用onTick(millisleft)方法,这个方法在我们创建CountDownTimer类时就进行过重写,在里面就可以写我们倒计时展示的具体逻辑了。至此整个流程结束。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

总结

以上是内存溢出为你收集整理的RecyclerView实现列表倒计时全部内容,希望文章能够帮你解决RecyclerView实现列表倒计时所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1145754.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存