本文重点在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>();
@Override
protected 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{
@Override
public int getCount() {
return goodss.size();
}
@Override
public Object getItem(int position) {
return goodss.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public 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延迟操作,来进行倒计时的操作。这样倒计时就完成了,上下滑动就不会出现紊乱的问题。
图片效果: