Android 用 ListView 實(shí)現(xiàn)倒計時列表功能
在很多時候我們會用到一個場景,在 list 的 item 上顯示不同的倒計時廉沮,如下圖所示。
需要注意的地方也沒多少:
1.ViewHolder 的重復(fù)使用與倒計時顯示沖突绍移。
2.退出應(yīng)用時釋放所有 CountDownTimer 資源环凿。
關(guān)于使用 Android 用 RecyclerView 實(shí)現(xiàn)倒計時列表功能。
Step00.核心代碼 MyAdapter.java
public static class MyAdapter extends BaseAdapter {
private List<TimerItem> mDatas;
private Context mContext;
//用于退出 Activity,避免 Countdown萝挤,造成資源浪費(fèi)御毅。
private SparseArray<CountDownTimer> countDownCounters;
public MyAdapter(Context mContext, List<TimerItem> mDatas) {
this.mContext = mContext;
this.mDatas = mDatas;
this.countDownCounters = new SparseArray<>();
}
/**
* 清空當(dāng)前 CountTimeDown 資源
*/
public void cancelAllTimers() {
if (countDownCounters == null) {
return;
}
Log.e("TAG", "size : " + countDownCounters.size());
for (int i = 0, length = countDownCounters.size(); i < length; i++) {
CountDownTimer cdt = countDownCounters.get(countDownCounters.keyAt(i));
if (cdt != null) {
cdt.cancel();
}
}
}
@Override
public int getCount() {
if (mDatas != null && !mDatas.isEmpty()) {
return mDatas.size();
}
return 0;
}
@Override
public Object getItem(int position) {
if (mDatas != null && !mDatas.isEmpty()) {
return mDatas.get(position);
}
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item_common, parent, false);
viewHolder = new ViewHolder();
viewHolder.statusTv = (TextView) convertView.findViewById(R.id.tv_status);
viewHolder.timeTv = (TextView) convertView.findViewById(R.id.tv_time);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
final TimerItem data = mDatas.get(position);
viewHolder.statusTv.setText(data.name);
CountDownTimer countDownTimer = countDownCounters.get(viewHolder.timeTv.hashCode());
if (countDownTimer != null) {
//將復(fù)用的倒計時清除
countDownTimer.cancel();
}
long timer = data.expirationTime;
timer = timer - System.currentTimeMillis();
//expirationTime 與系統(tǒng)時間做比較,timer 小于零怜珍,則此時倒計時已經(jīng)結(jié)束端蛆。
if (timer > 0) {
countDownTimer = new CountDownTimer(timer, 1000) {
public void onTick(long millisUntilFinished) {
viewHolder.timeTv.setText(TimeTools.getCountTimeByLong(millisUntilFinished));
Log.e("TAG", data.name + " : " + millisUntilFinished);
}
public void onFinish() {
viewHolder.timeTv.setText("00:00:00");
viewHolder.statusTv.setText(data.name + ":結(jié)束");
}
}.start();
//將此 countDownTimer 放入list.
countDownCounters.put(viewHolder.timeTv.hashCode(), countDownTimer);
} else {
viewHolder.timeTv.setText("00:00:00");
viewHolder.statusTv.setText(data.name + ":結(jié)束");
}
return convertView;
}
public class ViewHolder {
public TextView statusTv;
public TextView timeTv;
}
}
主要通過 SparseArray<CountDownTimer> 類保存每個 Item 上的 CountDownTimer 類。
當(dāng)需要復(fù)用時酥泛,通用 TextView 的 hashCode 取出對應(yīng)的 CountDownTimer今豆。重新啟動并顯示。
當(dāng)退出此頁面時柔袁,調(diào)用 cancelAllTimers() 方法呆躲,遍歷 countDownCounters 關(guān)閉所有的 CountDownTimer 類遏匆。避免造成內(nèi)存泄漏西饵。
Step01.數(shù)據(jù)填充
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<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;
}
Step04.list_item_common.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="64dp"
android:orientation="horizontal"
android:padding="10dp">
<TextView
android:id="@+id/tv_status"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
tools:text="status"
android:layout_gravity="center"
android:gravity="center"
/>
<TextView
android:id="@+id/tv_time"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="15sp"
android:layout_gravity="center_vertical"
tools:text="00:01:54"/>
</LinearLayout>
Step05.當(dāng)退出 Activity 時釋放資源.
@Override
protected void onDestroy() {
super.onDestroy();
if (mAdapter != null) {
mAdapter.cancelAllTimers();
}
}