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>