这几天做软件有用到这个效果,在网上找了好几个Demo,下载下来后发现都有一些小瑕玆,自己花了半天时间,想了好几种方法,最后发现这种方法实现起来的效果是最简单和最好的,倒计时功能可以实现,而且ListView可以复用,并且不会出现Item复用,内容错位的情况。
先上效果图:
依然废话不多说,直接上代码:
1:主页MainActivity函数:
/**
* 倒计时demo
* @author SHI
* 2016年3月16日 19:08:07
*/
public class MainActivity extends Activity {
/**截至时间数据源**/
private List<Date> listData;
/**当前时间**/
private long time_Current;
/**ListView控件**/
private ListView listView;
/**适配器**/
private MyCountAdapter myCountAdapter;
//这里很重要,使用Handler的延时效果,每隔一秒刷新一下适配器,以此产生倒计时效果
private Handler handler_timeCurrent = new Handler(){
@Override
public void handleMessage(Message msg) {
time_Current = time_Current+1000;
myCountAdapter.notifyDataSetChanged();
handler_timeCurrent.sendEmptyMessageDelayed(0,1000);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView);
//模拟活动截至时间数据
listData = new ArrayList<Date>();
listData.add(new Date(2016,3,16,8,20,31));
listData.add(new Date(2016,3,16,8,21,20));
listData.add(new Date(2016,3,16,13,21,22));
listData.add(new Date(2016,3,16,8,21,20));
listData.add(new Date(2016,3,16,8,21,23));
listData.add(new Date(2016,3,16,14,21,20));
listData.add(new Date(2016,3,16,8,21,23));
listData.add(new Date(2016,3,16,8,21,24));
listData.add(new Date(2016,3,16,8,21,20));
listData.add(new Date(2016,3,16,8,22,25));
listData.add(new Date(2016,3,16,8,23,20));
listData.add(new Date(2016,3,16,8,24,26));
listData.add(new Date(2016,3,16,8,25,20));
listData.add(new Date(2016,3,16,8,24,25));
listData.add(new Date(2016,3,16,8,25,20));
listData.add(new Date(2016,3,16,8,24,26));
listData.add(new Date(2016,3,16,11,20,20));
listData.add(new Date(2016,3,16,14,40,20));
listData.add(new Date(2016,3,16,8,44,20));
listData.add(new Date(2016,3,16,10,20,20));
//模拟当前服务器时间数据
Date date = new Date(2016,3,16,8,20,20);
time_Current = date.getTime();
myCountAdapter = new MyCountAdapter();
listView.setAdapter(myCountAdapter);
handler_timeCurrent.sendEmptyMessageDelayed(0,1000);
}
//防止当前Activity结束以后, handler依然继续循环浪费资源
@Override
protected void onDestroy() {
handler_timeCurrent.removeCallbacksAndMessages(null);
super.onDestroy();
}
public class MyCountAdapter extends BaseAdapter{
@Override
public int getCount() {
return listData.size();
}
@Override
public Object getItem(int position) {
return listData.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(MainActivity.this, R.layout.item_adapter_listview, null);
holder = new ViewHolder();
holder.tv_hour = (TextView) convertView.findViewById(R.id.tv_hour);
holder.tv_minute = (TextView) convertView.findViewById(R.id.tv_minute);
holder.tv_second = (TextView) convertView.findViewById(R.id.tv_second);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
Date date_finish = listData.get(position);
updateTextView( date_finish.getTime()-time_Current, holder);
return convertView;
}
/****
* 刷新倒计时控件
*/
public void updateTextView(long times_remain,ViewHolder hoder) {
if (times_remain <= 0) {
hoder.tv_hour.setText("00");
hoder.tv_minute.setText("00");
hoder.tv_second.setText("00");
return;
}
//秒钟
long time_second = (times_remain/1000)%60;
String str_second;
if (time_second < 10) {
str_second = "0" + time_second;
} else {
str_second = "" + time_second;
}
long time_temp = ((times_remain / 1000) - time_second) / 60;
//分钟
long time_minute = time_temp % 60;
String str_minute;
if (time_minute < 10) {
str_minute = "0" + time_minute;
} else {
str_minute = "" + time_minute;
}
time_temp = (time_temp - time_minute) / 60;
//小时
long time_hour = time_temp;
String str_hour;
if (time_hour < 10) {
str_hour = "0" + time_hour;
} else {
str_hour = "" + time_hour;
}
hoder.tv_hour.setText(str_hour);
hoder.tv_minute.setText(str_minute);
hoder.tv_second.setText(str_second);
}
private class ViewHolder{
/** 小时 **/
private TextView tv_hour;
/** 分钟 **/
private TextView tv_minute;
/** 秒 **/
private TextView tv_second;
}
}
}
2:activity_main.xml内容:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:xianglixiangqin="http://schemas.android.com/apk/res/com.example.myviewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</LinearLayout>
3:ListView适配器对应的布局文件item_adapter_listview.xml内容:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="30dp"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="@+id/tv_hour"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:background="#3D4045"
android:gravity="center"
android:inputType="number"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text="00"
android:textColor="#ffffff" />
<TextView
android:layout_width="wrap_content"
android:layout_height="25dp"
android:padding="0dp"
android:text=" : "
android:textSize="20sp" />
<TextView
android:id="@+id/tv_minute"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:background="#3D4045"
android:gravity="center"
android:inputType="number"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text="00"
android:textColor="#ffffff" />
<TextView
android:layout_width="wrap_content"
android:layout_height="25dp"
android:padding="0dp"
android:text=" : "
android:textSize="20sp" />
<TextView
android:id="@+id/tv_second"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:background="#3D4045"
android:gravity="center"
android:inputType="number"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text="00"
android:textColor="#ffffff" />
</LinearLayout>
代码不多,也没有自定义控件,这样就实现了基本的倒计时效果了,至于酷炫方面,大家可以自己再根据自己的需求加工一下吧!另外附上Demo下载:点击打开链接