最近项目有个需求,就是每个商品列表都有一个倒计时功能,于是乎,用万能的recyclerview撸了一把,为了方便备忘,在此做个笔记,闲话不多说,直接上demo:
![`Z(PQQHK{0HKRN8Q29%~CI.png
需要注意以下2点:
1.ViewHolder 的重复使用与倒计时显示冲突。
2.退出应用时释放所有 CountDownTimer 资源。
Step1.
//适配器
public static class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private List<TimerItem> mDatas;
//用于退出activity,避免countdown,造成资源浪费。
private SparseArray<CountDownTimer> countDownMap;
public MyAdapter(Context context, List<TimerItem> datas) {
mDatas = datas;
countDownMap = new SparseArray<>();
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_common, parent, false);
return new ViewHolder(view);
}
/**
* 清空资源
*/
public void cancelAllTimers() {
if (countDownMap == null) {
return;
}
Log.e("TAG", "size : " + countDownMap.size());
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 ViewHolder holder, int position) {
final TimerItem data = mDatas.get(position);
holder.statusTv.setText(data.name);
long time = data.expirationTime;
time = time - System.currentTimeMillis();
//将前一个缓存清除
if (holder.countDownTimer != null) {
holder.countDownTimer.cancel();
}
if (time > 0) {
holder.countDownTimer = new CountDownTimer(time, 1000) {
public void onTick(long millisUntilFinished) {
holder.timeTv.setText(TimeTools.getCountTimeByLong(millisUntilFinished));
Log.e("TAG", data.name + " : " + millisUntilFinished);
}
public void onFinish() {
holder.timeTv.setText("00:00:00");
holder.statusTv.setText(data.name + ":结束");
}
}.start();
countDownMap.put(holder.timeTv.hashCode(), holder.countDownTimer);
} else {
holder.timeTv.setText("00:00:00");
holder.statusTv.setText(data.name + ":结束");
}
}
@Override
public int getItemCount() {
if (mDatas != null && !mDatas.isEmpty()) {
return mDatas.size();
}
return 0;
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView statusTv;
public TextView timeTv;
public CountDownTimer countDownTimer;
public ViewHolder(View itemView) {
super(itemView);
statusTv = (TextView) itemView.findViewById(R.id.tv_status);
timeTv = (TextView) itemView.findViewById(R.id.tv_time);
}
}
}
主要通过 SparseArray<CountDownTimer> 类保存每个 Item 上的 CountDownTimer 类。
当需要复用时,通用 TextView 的 hashCode 取出对应的 CountDownTimer。重新启动并显示。
当退出此页面时,调用 cancelAllTimers() 方法,遍历 countDownCounters 关闭所有的 CountDownTimer 类。避免造成内存泄漏。
Step2.数据填充
mAdapter = new MyAdapter(mContext,TimerItemUtil.getTimerItemList());
mListView.setAdapter(mAdapter);
Step3.TimerItem 类
public class TimerItem {
//其他属性
public String name;
//倒计时长,单位毫秒
public long expirationTime;
public TimerItem(String name, long expirationTime) {
this.name = name;
this.expirationTime = expirationTime;
}
}
Step4.获取数据
public static List<TimerItem> getTimerItemList() {
List<TimerItem> lstTimerItems = new ArrayList<>();
lstTimerItems.add(new TimerItem("A", System.currentTimeMillis() + 11 * 1000));
lstTimerItems.add(new TimerItem("B", System.currentTimeMillis() + 22 * 1000));
lstTimerItems.add(new TimerItem("C", System.currentTimeMillis() + 26 * 1000));
lstTimerItems.add(new TimerItem("D", System.currentTimeMillis() + 33 * 1000));
lstTimerItems.add(new TimerItem("E", System.currentTimeMillis() + 24 * 1000));
lstTimerItems.add(new TimerItem("F", System.currentTimeMillis() + 98 * 1000));
lstTimerItems.add(new TimerItem("G", System.currentTimeMillis() + 14 * 1000));
lstTimerItems.add(new TimerItem("H", System.currentTimeMillis() + 36 * 1000));
lstTimerItems.add(new TimerItem("I", System.currentTimeMillis() + 58 * 1000));
lstTimerItems.add(new TimerItem("J", System.currentTimeMillis() + 47 * 1000));
lstTimerItems.add(new TimerItem("K", System.currentTimeMillis() + 66 * 1000));
lstTimerItems.add(new TimerItem("L", System.currentTimeMillis() + 55 * 1000));
lstTimerItems.add(new TimerItem("M", System.currentTimeMillis() + 62 * 1000));
lstTimerItems.add(new TimerItem("N", System.currentTimeMillis() + 45 * 1000));
lstTimerItems.add(new TimerItem("O", System.currentTimeMillis() + 14 * 1000));
return lstTimerItems;
}
Step5.当退出 Activity 时释放资源
@Override
protected void onDestroy() {
super.onDestroy();
if (mAdapter != null) {
mAdapter.cancelAllTimers();
}
}
Android技术交流群:633600411(欢迎入群交流)