Android ListView添加倒计时

demo下载地址:https://download.csdn.net/download/qq_27400335/10732268 



//本文是在fragment中实现的
private TimerTask task;
private Timer mTimer=null;
 private Handler mHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
          //数据获取成功后发送handler
            if (adapter!=null){//更新时间
                adapter.notifyDataSetChanged();
            }
           
             timeTask();
    
        }
    };
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
  //类似Activity 失去焦点onPause  获得焦点onResume 
    if (isVisibleToUser){
        mTimer = new Timer(true);
        timeTask();
        mTimer.schedule(task, 1000, 1000);
    }else {
        if (mTimer!=null){
            mTimer.cancel();
            mTimer.purge();
        }      
    }
}
private void timeTask(){
    task=new TimerTask() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            mHandler.sendEmptyMessage(0);
        }
    };
}
@Override
public void onDestroyView() {
    super.onDestroyView();
  
    if (mHandler!=null){
        mHandler.removeMessages(0);
    }
  
}

 

Adapter 方法

@Override

public View getView(final int position, View convertView, ViewGroup parent) {

.... //其他方法

     String eTime = "结束时间";

     setDate(eTime);

   return convertView;

}

 

//eTime 倒计时结束时间(如拍卖的结束时间),时间戳字符串

public void setDate(String eTime) {
    //获取当前时间戳 
    long currentTime = System.currentTimeMillis();
    //将结束时间转换类型long
    long eLongTime = Long.parseLong((eTime));
    if (currentTime>=eLongTime) {//这里判断时间是否结束
        holder.mTvHour.setText("00");//小时
        holder.mTvMinute.setText("00");//分钟
        holder.mTvSecond.setText("00");//秒    
    } else {//时间戳转时分秒
        long differenceTime = eLongTime - currentTime;
        long allMinute = differenceTime / 1000;
        long hour = allMinute / 3600;

        long minute = (allMinute - (hour * 3600)) / 60;
        long second = allMinute - (minute * 60 + hour * 3600);

        if (hour < 1) {
            holder.mTvHour.setText("00");
        } else if (hour < 10) {
            holder.mTvHour.setText("0" + hour);
        } else {
            holder.mTvHour.setText(hour + "");
        }

        if (minute < 1) {
            holder.mTvMinute.setText("00");
        } else if (minute < 10) {
            holder.mTvMinute.setText("0" + minute);
        } else {
            holder.mTvMinute.setText(minute + "");
        }

        if (second < 1) {
            holder.mTvSecond.setText("00");
        } else if (second < 10) {
            holder.mTvSecond.setText("0" + second);
        } else {
            holder.mTvSecond.setText(second + "");
        }
    }

    notifyDataSetChanged();
}

item布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/tv_hour"
        android:layout_width="wrap_content"
        android:layout_height="20dp"
        android:textSize="10sp"
        android:textColor="@color/white"
        android:text="00"
        android:gravity="center"
        android:paddingRight="6dp"
        android:paddingLeft="6dp"
        android:background="@drawable/shape_time_5"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="20dp"
        android:text=":"
        android:gravity="center"
        />
    <TextView
        android:id="@+id/tv_minute"
        android:layout_width="wrap_content"
        android:layout_height="20dp"
        android:textSize="10sp"
        android:textColor="@color/white"
        android:text="00"
        android:gravity="center"
        android:paddingRight="6dp"
        android:paddingLeft="6dp"
        android:background="@drawable/shape_time_5"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="20dp"
        android:text=":"
        android:gravity="center"
        />

    <TextView
        android:id="@+id/tv_second"
        android:layout_width="wrap_content"
        android:layout_height="20dp"
        android:textSize="10sp"
        android:textColor="@color/white"
        android:text="00"
        android:gravity="center"
        android:paddingRight="6dp"
        android:paddingLeft="6dp"
        android:background="@drawable/shape_time_5"
        />


</LinearLayout>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值