欢迎来到入门教程网!

Android

当前位置:主页 > 软件编程 > Android >

Android ListView列表实现倒计时

来源:本站原创|时间:2020-01-10|栏目:Android|点击:

本文实例为大家分享了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;
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

上一篇:android实现倒计时功能(开始、暂停、0秒结束)

栏    目:Android

下一篇:android自定义圆形倒计时显示控件

本文标题:Android ListView列表实现倒计时

本文地址:https://www.xiuzhanwang.com/a1/Android/9189.html

网页制作CMS教程网络编程软件编程脚本语言数据库服务器

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:835971066 | 邮箱:835971066#qq.com(#换成@)

Copyright © 2002-2020 脚本教程网 版权所有