您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。