RecyclerView中怎么实现列表倒计时

这篇文章将为大家详细讲解有关RecyclerView中怎么实现列表倒计时,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

五华ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18980820575(备注:SSL证书合作)期待与您的合作!

首先看下实现的最终效果

如何显示列表我相信大家都会,这里我只附上和倒计时功能实现的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);    }   }  } };

关于RecyclerView中怎么实现列表倒计时就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


网站名称:RecyclerView中怎么实现列表倒计时
文章来源:http://myzitong.com/article/pgphsp.html