重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
本文实例为大家分享了Android ListView列表实现倒计时的具体代码,供大家参考,具体内容如下
专业成都网站建设公司,做排名好的好网站,排在同行前面,为您带来客户和效益!创新互联公司为您提供成都网站建设,五站合一网站设计制作,服务好的网站设计公司,网站制作、成都网站设计负责任的成都网站制作公司!
效果图:
1. Activity
package com.s296267833.ybs.activity.firstPage.timedown; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ListView; import com.s296267833.ybs.R; import java.util.ArrayList; import java.util.List; /** * ListView列表实现倒计时 * */ public class TimeDownActivity extends AppCompatActivity { private TimeDownLvAdapter mAdapter; private ListView mListView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_time_down); mListView= findViewById(R.id.lv_time_down); setAdapter(); } /** * 设置适配器 * */ private void setAdapter() { mAdapter = new TimeDownLvAdapter(this,getTimerItemList()); mListView.setAdapter(mAdapter); } /** * 设置倒计时的数据 * */ public static ListgetTimerItemList() { List lstTimeDownItems = new ArrayList<>(); lstTimeDownItems.add(new TimeDownItem("A千万种心动", System.currentTimeMillis() + 11 * 1000)); lstTimeDownItems.add(new TimeDownItem("B千万种心动", System.currentTimeMillis() + 22 * 1000)); lstTimeDownItems.add(new TimeDownItem("C千万种心动", System.currentTimeMillis() + 26 * 1000)); lstTimeDownItems.add(new TimeDownItem("D千万种心动", System.currentTimeMillis() + 33 * 1000)); lstTimeDownItems.add(new TimeDownItem("E千万种心动", System.currentTimeMillis() + 24 * 1000)); lstTimeDownItems.add(new TimeDownItem("F千万种心动", System.currentTimeMillis() + 98 * 1000)); lstTimeDownItems.add(new TimeDownItem("G千万种心动", System.currentTimeMillis() + 14 * 1000)); lstTimeDownItems.add(new TimeDownItem("H千万种心动", System.currentTimeMillis() + 36 * 1000)); lstTimeDownItems.add(new TimeDownItem("I千万种心动", System.currentTimeMillis() + 58 * 1000)); lstTimeDownItems.add(new TimeDownItem("J千万种心动", System.currentTimeMillis() + 47 * 1000)); lstTimeDownItems.add(new TimeDownItem("K千万种心动", System.currentTimeMillis() + 66 * 1000)); lstTimeDownItems.add(new TimeDownItem("L千万种心动", System.currentTimeMillis() + 55 * 1000)); lstTimeDownItems.add(new TimeDownItem("M千万种心动", System.currentTimeMillis() + 62 * 1000)); lstTimeDownItems.add(new TimeDownItem("N千万种心动", System.currentTimeMillis() + 45 * 1000)); lstTimeDownItems.add(new TimeDownItem("O千万种心动", System.currentTimeMillis() + 14 * 1000)); return lstTimeDownItems; } /** * 清空当前 CountTimeDown 资源 * */ @Override protected void onDestroy() { super.onDestroy(); if (mAdapter != null) { mAdapter.cancelAllTimers(); } } }
2. activity_time_down布局
<?xml version="1.0" encoding="utf-8"?>
3. ListView的item类(TimeDownItem)
package com.s296267833.ybs.activity.firstPage.timedown; /** * Created by JiYaRuo on 2019/1/9. * @BeUsedFor 倒计时Lv的Bean类 */ public class TimeDownItem { //其他属性 public String name; //倒计时长,单位毫秒 public long expirationTime; public TimeDownItem(String name, long expirationTime) { this.name = name; this.expirationTime = expirationTime; } }
4. listview的item布局
<?xml version="1.0" encoding="utf-8"?>
5. ListView的适配器TimeDownLvAdapter
package com.s296267833.ybs.activity.firstPage.timedown; import android.content.Context; import android.os.CountDownTimer; import android.util.SparseArray; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import com.s296267833.ybs.R; import com.s296267833.ybs.activity.firstPage.TimeUtils; import java.util.List; /** * Created by JiYaRuo on 2019/1/9. * @BeUsedFor 列表倒计时listView的适配器 */ public class TimeDownLvAdapter extends BaseAdapter { private ListmDatas; private Context mContext; //用于退出 Activity,避免 Countdown,造成资源浪费。 private SparseArray countDownCounters; public TimeDownLvAdapter(Context mContext, List mDatas) { this.mContext = mContext; this.mDatas = mDatas; this.countDownCounters = new SparseArray<>(); } @Override public int getCount() { if (mDatas != null && !mDatas.isEmpty()) { return mDatas.size(); } return 0; } @Override public Object getItem(int position) { if (mDatas != null && !mDatas.isEmpty()) { return mDatas.get(position); } return null; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { final ViewHolder viewHolder; if (convertView == null) { convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item_common, parent, false); viewHolder = new ViewHolder(); viewHolder.statusTv = (TextView) convertView.findViewById(R.id.tv_status); viewHolder.timeTv = (TextView) convertView.findViewById(R.id.tv_time); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } final TimeDownItem data = mDatas.get(position); viewHolder.statusTv.setText(data.name); CountDownTimer countDownTimer = countDownCounters.get(viewHolder.timeTv.hashCode()); if (countDownTimer != null) { //将复用的倒计时清除 countDownTimer.cancel(); } long timer = data.expirationTime; timer = timer - System.currentTimeMillis(); //expirationTime 与系统时间做比较,timer 小于零,则此时倒计时已经结束。 if (timer > 0) { countDownTimer = new CountDownTimer(timer, 1000) { public void onTick(long millisUntilFinished) { viewHolder.timeTv.setText(TimeUtils.getCountTimeByLong(millisUntilFinished)); } public void onFinish() { viewHolder.timeTv.setText("00:00:00"); viewHolder.statusTv.setText(data.name + ":结束"); } }.start(); //将此 countDownTimer 放入list. countDownCounters.put(viewHolder.timeTv.hashCode(), countDownTimer); } else { viewHolder.timeTv.setText("00:00:00"); viewHolder.statusTv.setText(data.name + ":结束"); } return convertView; } /** * ViewHolder内部类 * */ public class ViewHolder { public TextView statusTv; public TextView timeTv; } /** * 清空当前 CountTimeDown 资源 */ public void cancelAllTimers() { if (countDownCounters == null) { return; } for (int i = 0, length = countDownCounters.size(); i < length; i++) { CountDownTimer cdt = countDownCounters.get(countDownCounters.keyAt(i)); if (cdt != null) { cdt.cancel(); } } } }
6. 工具类TimeUtils
package com.s296267833.ybs.activity.firstPage; /** * Created by JiYaRuo on 2019/1/9. * @BeUsedFor 倒计时工具类 */ public class TimeUtils { /** * 毫秒换成00:00:00 * */ public static String getCountTimeByLong(long finishTime) { int totalTime = (int) (finishTime / 1000);//秒 int hour = 0, minute = 0, second = 0; if (3600 <= totalTime) { hour = totalTime / 3600; totalTime = totalTime - 3600 * hour; } if (60 <= totalTime) { minute = totalTime / 60; totalTime = totalTime - 60 * minute; } if (0 <= totalTime) { second = totalTime; } StringBuilder sb = new StringBuilder(); if (hour < 10) { sb.append("0").append(hour).append(":"); } else { sb.append(hour).append(":"); } if (minute < 10) { sb.append("0").append(minute).append(":"); } else { sb.append(minute).append(":"); } if (second < 10) { sb.append("0").append(second); } else { sb.append(second); } return sb.toString(); } /** * 将LONG类型的时间戳转换为xx:xx:xx时分秒 * */ public static String formatLongToTimeStr(Long l) { int hour = 0; int minute = 0; int second = 0; second = l.intValue() ; if (second > 60) { minute = second / 60; //取整 second = second % 60; //取余 } if (minute > 60) { hour = minute / 60; minute = minute % 60; } String strtime = hour+":"+minute+":"+second; return strtime; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。