这篇文章将为大家详细讲解有关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 List getTimerItemList() { 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布局
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布局
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 List mDatas; 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; }}
关于Android中怎么利用ListView实现列表实现倒计时功能就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
本文标题:Android中怎么利用ListView实现列表实现倒计时功能
网站URL:
http://kswsj.cn/article/gpohps.html