Android 用 RecyclerView 实现倒计时列表功能
标签(空格分隔): Android RecyclerView CountDownTime
在很多时候我们会用到一个场景,在 RecyclerView 的 item 上显示不同的倒计时,如下图所示。
需要注意的地方也没多少:
1.ViewHolder 的重复使用与倒计时显示冲突。
2.退出应用时释放所有 CountDownTimer 资源。
关于使用 Android 用 ListView 实现列表倒计时。
Step00.核心代码 MyAdapter.java
//适配器
public static class MyAdapter extends RecyclerView.Adapter {
private List mDatas;
//用于退出activity,避免countdown,造成资源浪费。
private SparseArray countDownMap;
public MyAdapter(Context context, List 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
当需要复用时,通用 TextView 的 hashCode 取出对应的 CountDownTimer。重新启动并显示。
当退出此页面时,调用 cancelAllTimers() 方法,遍历 countDownCounters 关闭所有的 CountDownTimer 类。避免造成内存泄漏。
Step01.数据填充
mAdapter = new MyAdapter(mContext,TimerItemUtil.getTimerItemList());
mListView.setAdapter(mAdapter);
Step02.Entity 类
public class TimerItem {
//其他属性
public String name;
//倒计时长,单位毫秒
public long expirationTime;
public TimerItem(String name, long expirationTime) {
this.name = name;
this.expirationTime = expirationTime;
}
}
Step03.获取倒计时集合(自定义时间)。
public static List getTimerItemList() {
List 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;
}
Step04.list_item_common.xml
Step05.当退出 Activity 时释放资源.
@Override
protected void onDestroy() {
super.onDestroy();
if (mAdapter != null) {
mAdapter.cancelAllTimers();
}
}