標籤:listview條目倒計時 listview android handler
本文重點在ListView中使用倒計時
在Android的開發中,我們經常遇見倒計時的操作,通常使用Timer和Handler共同操作來完成。當然也可以使用Android系統控制項CountDownTimer,這裡簡單操作一下這個控制項:
new CountDownTimer(30000, 1000) { public void onTick(long millisUntilFinished) { timeTV.setText(": " + millisUntilFinished / 1000); } public void onFinish() { timeTV.setText("done!"); } }.start();
這個操作起來比Timer要爽。
當然本文不是要展示這種簡單的效果,在項目中遇見這樣的問題,在ListView中添加倒計時,有的條目有倒計時的功能,有的條目沒有倒計時的功能,這種方式該怎麼實現呢?難道在Adapter裡面,每個條目都添加如上的代碼。如下所示:
public View getView(int position, View convertView, ViewGroup parent) {Goods goods = goodss.get(position);if(convertView == null){convertView = View.inflate(ListViewCountTimeDemo.this, R.layout.item_counttime, null);TextView nameTV = (TextView)convertView.findViewById(R.id.nameTV);final TextView timeTV = (TextView)convertView.findViewById(R.id.timeTV);nameTV.setText(goods.getName());timeTV.setText(goods.getTime()+"");new CountDownTimer(30000, 1000) { public void onTick(long millisUntilFinished) { timeTV.setText(": " + millisUntilFinished / 1000); } public void onFinish() { timeTV.setText("done!"); } }.start(); }else{}return convertView;}
這種方式肯定有問題,每次擷取一個新的條目,都會建立一個計時對象,這樣在條目比較多的時候,來來回回上下滑動之後,就會出現計時紊亂。壓根兒不是1秒1秒的走動。而且可能建立過多的CountDownTimer對象,導致記憶體消耗過大。
個人推薦使用下面這種方式:
package com.example.androidtest;import java.util.ArrayList;import android.app.Activity;import android.os.Bundle;import android.os.CountDownTimer;import android.os.Handler;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ListView;import android.widget.TextView;public class CountdownActivity extends Activity {private ListView lv;private ArrayList<Goods> goodss = new ArrayList<Goods>();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_countdown);lv = (ListView) findViewById(R.id.lv);for(int i=0;i<20;i++){goodss.add(new Goods(30000+i*1000, "zhangfei"+i));}adapter = new CountTimeAdapter();lv.setAdapter(adapter);handler.sendEmptyMessage(1);}private class CountTimeAdapter extends BaseAdapter{@Overridepublic int getCount() {return goodss.size();}@Overridepublic Object getItem(int position) {return goodss.get(position);}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {Goods goods = goodss.get(position);if(convertView == null){convertView = View.inflate(CountdownActivity.this, R.layout.item_counttime, null);}TextView nameTV = (TextView)convertView.findViewById(R.id.nameTV);final TextView timeTV = (TextView)convertView.findViewById(R.id.timeTV);nameTV.setText(goods.getName());long seconds = goods.getTime()/1000;timeTV.setText(seconds/(60)+"分"+seconds%60+"秒");return convertView;}}private Handler handler = new Handler(){public void handleMessage(android.os.Message msg) {switch (msg.what) {case 1:boolean isNeedCountTime = false;//①:其實在這塊需要精確計算目前時間for(int index =0;index<goodss.size();index++){Goods goods = goodss.get(index);long time = goods.getTime();if(time>1000){//判斷是否還有條目能夠倒計時,如果能夠倒計時的話,延遲一秒,讓它接著倒計時isNeedCountTime = true;goods.setTime(time-1000);}else{goods.setTime(0);}}//②:for迴圈執行的時間adapter.notifyDataSetChanged();<span style="color:#009900;">if(isNeedCountTime){//TODO 然後用1000-(②-①),就贏延遲的時間handler.sendEmptyMessageDelayed(1, 1000);}break;</span>}}};private CountTimeAdapter adapter;}本文的重點,就是使用Handler延遲操作,來進行倒計時的操作。這樣倒計時就完成了,上下滑動就不會出現紊亂的問題。
圖片效果:
Android開發-----02-ListView中給某些條目進行倒計時