这篇文章将为大家详细讲解有关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:pider="#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; }}
关于Android中怎么利用ListView实现列表实现倒计时功能就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。