通过RecyclerView实现列表倒计时

2017-08-27  本文已影响0人  JCJIE

最近在做一个项目,需要用到列表倒计时功能,捣鼓半天终于弄了出来,在安卓中实现这个效果需要用到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类时就进行过重写,在里面就可以写我们倒计时展示的具体逻辑了。至此整个流程结束。

git地址:https://github.com/jcj123/Countdowntimer

上一篇下一篇

猜你喜欢

热点阅读