Android ListView中有倒计时

@ Android ListView中有倒计时
如果ListView中有倒计时,那么倒计时一定要自定义View,如果使用notifyDataChangge()更新页面会很卡。
下面我贴出我项目中的倒计时代码

Java代码

public class CountDownLL extends LinearLayout {
private TextView hourTv, minTv, secondTv;
private Context context;
private CountDownTimer countDownTimer;
private Drawable itemBg;
public CountDownLL(Context context) {
    this(context, null, 0);
}

public CountDownLL(Context context, @Nullable AttributeSet attrs) {
    this(context, attrs, 0);
}

public CountDownLL(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    this.context = context;
    TypedArray typedArray=context.obtainStyledAttributes(attrs,R.styleable.count_down);
    itemBg=typedArray.getDrawable(R.styleable.count_down_item_bg);
    typedArray.recycle();
    initView();
}

public void initView() {
    View view = LayoutInflater.from(context).inflate(R.layout.coundown, null);
    addView(view);
    hourTv = view.findViewById(R.id.hour_tv);
    if (itemBg!=null){
        hourTv.setBackground(itemBg);
    }
    minTv = view.findViewById(R.id.minute_tv);
    minTv.setBackground(itemBg);
    secondTv = view.findViewById(R.id.second_tv);
    secondTv.setBackground(itemBg);
}


public void setCountDownNum(long countDownNum) {
    if (countDownNum <= 0) {
        return;
    }
    countDownTimer = new CountDownTimer(countDownNum * 1000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            CountDownLL.this.setVisibility(VISIBLE);
            TimeModel timeModel = longToTimeModel(millisUntilFinished / 1000);
            hourTv.setText(timeModel.getHour());
            minTv.setText(timeModel.getMinute());
            secondTv.setText(timeModel.getSecond());
        }

        @Override
        public void onFinish() {
            stopTimer();
            CountDownLL.this.setVisibility(GONE);
        }
    };
    countDownTimer.start();
}

public void stopTimer() {
    if (countDownTimer != null) {
        countDownTimer.cancel();
        countDownTimer = null;
    }
}
class TimeModel {
    String day;
    String hour;
    String minute;
    String second;
    TimeModel(String day, String hour, String minute, String second) {
        this.day = day;
        this.hour = hour;
        this.minute = minute;
        this.second = second;
    }
    String getDay() {
        return day;
    }
    String getHour() {
        return hour;
    }

    String getMinute() {
        return minute;
    }
    String getSecond() {
        return second;
    }
}

private TimeModel longToTimeModel(long m) {
    long min = 60;
    long hour = min * 60;
    long day = 24 * hour;

    long dayNum = m / day;
    long hourNum = m % day / hour;
    long minNum = m % hour / min;
    long secNum = m % min;
    String hourNumStr;
    if (0 <= hourNum && hourNum <= 9) {
        hourNumStr = "0" + hourNum;
    } else {
        hourNumStr = "" + hourNum;
    }
    String minNumStr;
    if (0 <= minNum && minNum <= 9) {
        minNumStr = "0" + minNum;
    } else {
        minNumStr = "" + minNum;
    }
    String secNumStr;
    if (0 <= secNum && secNum <= 9) {
        secNumStr = "0" + secNum;
    } else {
        secNumStr = "" + secNum;
    }
    return new TimeModel(dayNum + "", hourNumStr, minNumStr, secNumStr);
}

布局文件

<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:id="@+id/day_tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="0天"
    android:textColor="#333333"
    android:textSize="13dp"
    android:visibility="gone" />

<TextView
    android:id="@+id/hour_tv"
    android:layout_width="17dp"
    android:layout_height="14dp"
    android:background="@drawable/time_bg"
    android:gravity="center"
    android:text="00"
    android:textColor="@color/white"
    android:textSize="11dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=":"
    android:textColor="#333333"
    android:textSize="11dp" />

<TextView
    android:id="@+id/minute_tv"
    android:layout_width="17dp"
    android:layout_height="14dp"
    android:background="@drawable/time_bg"
    android:gravity="center"
    android:text="00"
    android:textColor="@color/white"
    android:textSize="11dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=":"
    android:textColor="#333333"
    android:textSize="11dp" />

<TextView
    android:id="@+id/second_tv"
    android:layout_width="17dp"
    android:layout_height="14dp"
    android:background="@drawable/time_bg"
    android:gravity="center"
    android:text="00"
    android:textColor="@color/white"
    android:textSize="11dp" />
# 在attrs文件中添加
<declare-styleable name="count_down">
    <attr name="item_bg" format="reference"></attr>
</declare-styleable>

猜你喜欢

转载自blog.csdn.net/w517272812/article/details/88639959
0条评论
添加一条新回复