Android ListView显示倒计时的流畅实现
有人在群里问他实现的倒计时在ListView中很卡,有没有好的方案,我就闲来没事,实现了一下,供参考
package com.microchange; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.microchange.view.TimeView; import com.microchange.view.application.MyApplication; import android.app.ListActivity; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; public class MainActivity extends ListActivity { public static List<Map<String, String>> list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Map<String, String> map = null; list = new ArrayList<Map<String,String>>(); for (int i = 1; i < 50; i++) { map = new HashMap<String, String>(); map.put("name","秒杀产品"+i); map.put("time",String.valueOf(10*i)); list.add(map); } MyApplication.set(list); // 数据拿到开始计时 setListAdapter(new MyAdapter()); start(); } class MyAdapter extends BaseAdapter{ @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){ convertView = View.inflate(getApplicationContext(), R.layout.fragment_main, null); viewHolder = new ViewHolder(); viewHolder.name = (TextView) convertView.findViewById(R.id.name); viewHolder.time = (TimeView) convertView.findViewById(R.id.time); convertView.setTag(viewHolder); }else{ viewHolder = (ViewHolder) convertView.getTag(); } viewHolder.name.setText(list.get(position).get("name"));; viewHolder.time.setText(list.get(position).get("time"));; viewHolder.time.setPosition(position); return convertView; } } class ViewHolder{ TextView name; TimeView time; } 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(1000); for(Map<String, String> map :list){ if(!"售罄".equals(map.get("time"))){ if("1".equals(map.get("time"))){ map.put("time", "售罄"); result++; }else{ map.put("time", ""+(Integer.parseInt(map.get("time"))-1)); } } } } catch (InterruptedException e) { e.printStackTrace(); } } }; }; thread.start(); } @Override protected void onDestroy() { MyApplication.removeAll(); list = null; super.onDestroy(); } }
/** * */ package com.microchange.view; import java.util.List; import java.util.Map; import com.microchange.MainActivity; import com.microchange.view.application.MyApplication; import android.content.Context; import android.util.AttributeSet; import android.util.Log; import android.widget.TextView; /** * 描述: TimeView * * @author 吴传龙 Email:[email protected] QQ: 3026862225 * @version 创建时间: 2015年3月17日 上午10:15:51 * @version 最后修改时间:2015年3月17日 上午10:15:51 修改人:吴传龙 */ public class TimeView extends TextView implements Runnable { private int position; /** * @param context * @param attrs * @param defStyle */ public TimeView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); postDelayed(this, 1000); } public void setPosition(int position) { this.position = position; } @Override public void run() { String time= MyApplication.get(position, "time"); setText(time); if(!"售罄".equals(time)){ postDelayed(this, 1000); } } public TimeView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public TimeView(Context context) { this(context, null); } }
/** * */ package com.microchange.view.application; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Application; /** * 描述: MyApplication * * @author 吴传龙 Email:[email protected] QQ: 3026862225 * @version 创建时间: 2015年3月17日 下午2:45:06 * @version 最后修改时间:2015年3月17日 下午2:45:06 修改人:吴传龙 */ public class MyApplication extends Application { private static List<Map<String, String>> list; @Override public void onCreate() { super.onCreate(); } public static void set(List<Map<String, String>> l) { list = l; } public static String get(int position, String key) { return list.get(position).get(key); } public static void removeAll(){ if (list!=null) list.clear(); list = null; } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。