项目中刚好用到需要在GridView和ListView中进行倒计时处理,多个倒计时同时进行,毫秒级别的(分:秒:毫秒),整理一下拿出来和大家分享。
效果图:
主要思路很简单:1、获取后台的给的时间与当前时间相减,得到需要倒计时的时间
2、将得到的时间换算成毫秒(根据实际需求换算),开启子线程做递减
3、最后利用handler循环更新界面
直接上代码:
1、Activity
public class MainActivity extends Activity{
public static List<Person> list;
private ListView listView;
private MainListAdapter adapter;
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
if (msg.what ==1) {
adapter.notifyDataSetChanged();
//每隔1毫秒更新一次界面,如果只需要精确到秒的倒计时此处改成1000即可
handler.sendEmptyMessageDelayed(1,1);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list_view);
//测试数据
list = new ArrayList<>();
for (int i = 1; i < 5; i++) {
Person person = new Person();
person.setName("产品"+i);
person.setTime(90000 + i + "");
list.add(person);
}
// 数据拿到开始计时
adapter = new MainListAdapter(this,list);
listView.setAdapter(adapter);
adapter.start();//开始倒计时
handler.sendEmptyMessageDelayed(1,1);
}
}
2、Adapter
public class MainListAdapter extends BaseAdapter {
private List<Person> list;
private LayoutInflater layoutInflater;
public MainListAdapter(Context context, List<Person> list) {
this.list = list;
this.layoutInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = layoutInflater.inflate(R.layout.adapter_main_lv_item, parent, false);
viewHolder.tvTimer = (TextView) convertView.findViewById(R.id.tv_timer);
viewHolder.tvName = (TextView) convertView.findViewById(R.id.tv_name);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.tvName.setText(list.get(position).getName());
if (!list.get(position).getTime().equals("时间到")) {
//将一个int类型的数值转换成时间格式
int tempTime = Integer.parseInt(list.get(position).getTime());
//SSS为毫秒,如果精确到时分秒改成HH:mm:ss即可
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mm:ss:SSS");
Date date = new Date(tempTime);
String time = simpleDateFormat.format(date);
//因为1秒是等于1000毫秒的,正常显示格式为05:40:99,所以此处截取掉最后一位数字
viewHolder.tvTimer.setText(time.substring(0, time.length() - 1));
} else {
viewHolder.tvTimer.setText(list.get(position).getTime());
}
return convertView;
}
public class ViewHolder {
TextView tvTimer;
TextView tvName;
}
int result = 0;
private Thread thread;
public void start() {
thread = new Thread() {
public void run() {
while (true) {
try {
if (list == null || result == list.size()) {
break;
}
sleep(1);
for (Person person : list) {
if (!"时间到".equals(person.getTime())) {
if ("1".equals(person.getTime())) {
person.setTime("时间到");
result++;
} else {
person.setTime((Integer.parseInt(person.getTime()) - 1) + "");
}
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
thread.start();
}
}
csdn资源链接: 点击打开链接