1. 程式人生 > >ListView中每個item中定時器

ListView中每個item中定時器

直接上原始碼:

package com.example.myviewpager.daojishi;


import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;


import com.example.myviewpager.R;


public class Test1Activity extends Activity {


/**截至時間資料來源**/
private ArrayList<HashMap<String, String>> infoMapList = new ArrayList<HashMap<String, String>>();
private HashMap<String, String> infoMap = null;
    /**當前時間**/
    private int time_Current = 1;
/**ListView控制元件**/
private ListView listView;
/**介面卡**/
private MyCountAdapter myCountAdapter;
 
private int minute = 0;
private int second = 0;
 
 

//這裡很重要,使用Handler的延時效果,每隔一秒重新整理一下介面卡,以此產生倒計時效果
    private Handler handler_timeCurrent = new Handler(){
        @Override
        public void handleMessage(Message msg) {
        time_Current = time_Current+1;
myCountAdapter.notifyDataSetChanged();
            handler_timeCurrent.sendEmptyMessageDelayed(0,1000);
        }
    };

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_one);

listView = (ListView) findViewById(R.id.listView);

}

//防止當前Activity結束以後,   handler依然繼續迴圈浪費資源
@Override
protected void onDestroy() {

System.out.println("_______________我毀滅了______________________");
super.onDestroy();
}



@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
handler_timeCurrent.removeCallbacksAndMessages(null);
System.out.println("_______________我暫停了______________________");

}


@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();

time_Current = 1;
minute = 0;
second = 0;

//模擬活動截至時間資料
infoMapList = new ArrayList<HashMap<String, String>>();
for(int i=0;i<3;i++){
infoMap = new HashMap<String, String>();
           infoMap.put("allTime", i+"");
           infoMap.put("address", "地址"+i+"");
           infoMapList.add(infoMap);
}
myCountAdapter = new MyCountAdapter();
listView.setAdapter(myCountAdapter);

       handler_timeCurrent.sendEmptyMessageDelayed(0,1000);
System.out.println("_______________我重新喚起了______________________");
}






public class MyCountAdapter extends BaseAdapter{


@Override
public int getCount() {
return infoMapList.size();
}


@Override
public Object getItem(int position) {
return infoMapList.get(position);
}


@Override
public long getItemId(int position) {
return position;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView == null){
convertView = View.inflate(Test1Activity.this, R.layout.activity_test_one_item, null);
holder = new ViewHolder();
holder.tv_second = (TextView) convertView.findViewById(R.id.tv_second);
holder.tv_minute = (TextView) convertView.findViewById(R.id.tv_minute);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
String date_finish = infoMapList.get(position).get("allTime");
holder.tv_minute.setText(infoMapList.get(position).get("address"));
int finishTime = Integer.parseInt(date_finish);
finishTime = finishTime * 60 +30;
updateTextView( finishTime-time_Current, holder);
return convertView;
}
/****
* 重新整理倒計時控制元件
*/
public void updateTextView(int times_remain,ViewHolder hoder) {
System.out.println("________times_remain__________"+times_remain);
if(times_remain<=0){
hoder.tv_second.setText("已完成");
}else{
minute = times_remain/60;
if(minute!=0){
hoder.tv_second.setText("約" + minute+"分鐘");
       }else{
           second = times_remain%60;
           if(second!=0){
            hoder.tv_second.setText("約" + minute+"分鐘");
           }else{
            hoder.tv_second.setText("已完成");
            //handler_timeCurrent.removeCallbacksAndMessages(null);
           }
       }
}

}

private class ViewHolder{

/** 秒 **/
private TextView tv_second;
/** 秒 **/
private TextView tv_minute;

}
}





}