Android ListView列表实现倒计时
本文实例为大家分享了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<TimeDownItem> getTimerItemList() { List<TimeDownItem> 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"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.s296267833.ybs.activity.firstPage.timedown.TimeDownActivity"> <ListView android:id="@+id/lv_time_down" android:background="#faa" android:divider="#00000000" android:layout_width="match_parent" android:layout_height="match_parent"> </ListView> </LinearLayout>
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"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="64dp" android:orientation="horizontal" android:padding="10dp"> <TextView android:id="@+id/tv_status" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" tools:text="status" android:textColor="#ffffff" android:layout_gravity="center" android:gravity="center" /> <TextView android:id="@+id/tv_time" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="15sp" android:gravity="right" android:textColor="#ffffff" android:layout_gravity="center_vertical" tools:text="00:00:00"/> </LinearLayout>
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<TimeDownItem> mDatas; private Context mContext; //用于退出 Activity,避免 Countdown,造成资源浪费。 private SparseArray<CountDownTimer> countDownCounters; public TimeDownLvAdapter(Context mContext, List<TimeDownItem> 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; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
您可能感兴趣的文章
- 01-10Android自定义View之绘制圆形头像功能
- 01-10Android实现双击返回键退出应用实现方法详解
- 01-10android实现记住用户名和密码以及自动登录
- 01-10android实现简单计算器功能
- 01-10Android 友盟第三方登录与分享的实现代码
- 01-10android实现指纹识别功能
- 01-10Emoji表情在Android JNI中的兼容性问题详解
- 01-10Android实现圆形渐变加载进度条
- 01-10android开发环境中SDK文件夹下的所需内容详解
- 01-10android异步消息机制 源码层面彻底解析(1)
阅读排行
本栏相关
- 01-10Android自定义View之绘制圆形头像功能
- 01-10Android实现双击返回键退出应用实现方
- 01-10android实现简单计算器功能
- 01-10android实现记住用户名和密码以及自动
- 01-10C++自定义API函数实现大数相乘算法
- 01-10Android 友盟第三方登录与分享的实现代
- 01-10android实现指纹识别功能
- 01-10如何给Flutter界面切换实现点特效
- 01-10Android实现圆形渐变加载进度条
- 01-10Emoji表情在Android JNI中的兼容性问题详
随机阅读
- 01-10delphi制作wav文件的方法
- 01-11ajax实现页面的局部加载
- 01-10C#中split用法实例总结
- 08-05dedecms(织梦)副栏目数量限制代码修改
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文
- 08-05织梦dedecms什么时候用栏目交叉功能?
- 08-05DEDE织梦data目录下的sessions文件夹有什
- 01-10SublimeText编译C开发环境设置
- 01-10使用C语言求解扑克牌的顺子及n个骰子
- 04-02jquery与jsp,用jquery