The effect is as follows:



MainActivityThe code is as follows:

package com.abner.countdownforlistview; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.widget.ListView; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private ListView lv_goods; private GoodsAdapter adapter; private int result = -1; private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); if (msg.what == 1) { result = adapter.countDownTime(); if (result == 0) { handler.removeMessages(1); return; } else { adapter.notifyDataSetChanged(); handler.sendEmptyMessageDelayed(1, 1000); }}}}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv_goods = (ListView) findViewById(R.id.lv_goods); initData(); } private void initData() { List goodsList = new ArrayList<>(); for (int i = 0; i < 15; i++) { Goods goods = new Goods(); Goods.setgoodsname (" This is the product name: "+ I); goods.setPrice(i * 26); goods.setTime(i % 3 == 0 ? i * 8 + 62 : 0); goodsList.add(goods); } adapter = new GoodsAdapter(this, goodsList); lv_goods.setAdapter(adapter); handler.sendEmptyMessageDelayed(1, 1000); }}Copy the code

In thisMainActivityIn, you can see throughHandlerRealized the reciprocal function, in theListViewSet theAdapterAfter throughhandlerthesendEmptyMessageDelayedMethod sends a message, and thenhandlerthehandleMessageMethod by calling theadapterIn a statementcountDownTimeMethod traverses the corresponding object collection and sets the corresponding value, and finally passes the returned objectresultCheck whether the countdown has ended. If so, remove the message; otherwise, continue sending the message for the countdown.

ListViewtheGoodsAdapterThe code is as follows:

package com.abner.countdownforlistview; import android.content.Context; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import java.util.List; import static com.abner.countdownforlistview.R.id.tv_price; /** * Created by abner on 2016/5/6. */ public class GoodsAdapter extends BaseAdapter { private Context context; private List goodsList; public GoodsAdapter(Context context, List goodsList) { this.context = context; this.goodsList = goodsList; } @Override public int getCount() { return goodsList == null ? 0 : goodsList.size(); } @Override public Object getItem(int position) { return goodsList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { final ViewHolder holder; if (convertView == null) { holder = new ViewHolder(); convertView = LayoutInflater.from(context).inflate(R.layout.lv_goods_item, null); holder.tv_goodsName = (TextView) convertView.findViewById(R.id.tv_goodsName); holder.tv_price = (TextView) convertView.findViewById(tv_price); holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } final Goods goods = goodsList.get(position); holder.tv_goodsName.setText(goods.getGoodsName()); Holder.tv_price. SetText ("¥" + goods.getprice ()); final int time = goods.getTime(); If (time > 0) {holder.tv_time.settext (getTimeStr(time) + "start "); } else {holder.tv_time.setText(" In progress...") ); } return convertView; } class ViewHolder { TextView tv_goodsName; TextView tv_price; TextView tv_time; } private int result = -1; /** ** * * @return */ public int countDownTime() {new Thread(new Runnable() {@override public void run() { try { Thread.sleep(1000); // If goodsList is null or result equals goodsList. Size +1, the countdown is complete. The need to stop bottom if (goodsList = = null | | result = = goodsList. The size () + 1) {result = 0; return; } result = 1; for (Goods goods : goodsList) { if (goods.getTime() == 0) { goods.setTime(0); result++; } else {goods.settime (goods.gettime () -1);} else {goods.settime (goods.gettime () -1); } } } catch (InterruptedException e) { e.printStackTrace(); } } }).start(); return result; } /** * Format the time display, the format is: Public static String getTimeStr(int min) {int min = timeDiff / 60; int sec = timeDiff - min * 60; String result = ""; If (min == 0) {result = SEC + "SEC "; } else {result = min + SEC + SEC; } return result; }}Copy the code

At this point, theandroidIn the implementationListVieworGridViewIn theitemThe reciprocal function is done.

Download the source code