本文adapter使用的是BaseQuickAdapter,感谢这种好的三方库
两个问题,一个是倒计时复用问题,另外一个是倒计时滑动回来倒计时又从头开始的问题。
第一个问题,new 一个新的ViewHolder需要继承BaseViewHolder
第二个问题,开始时间我是计算好,赋值到数据里面的
class MineTaskAdapter : BaseQuickAdapter<Info, MineTaskAdapter.TaskNewViewHolder>(R.layout.adapter_mine_task) {
private var timeList = arrayListOf<CountDownTimer>()
override fun convert(helper: TaskNewViewHolder?, item: Info?) {
helper?.countDownTimer?.cancel()
if (item?.status == 1 || item?.status == 3) {
if (item?.currentTime != null) {
if (item?.currentTime > 0) {
helper?.countDownTimer = object:CountDownTimer(item?.currentTime, 1000){
override fun onFinish() {
helper?.getView<TextView>(R.id.task_tv_limit_time)?.text = "结束"
}
override fun onTick(millisUntilFinished: Long) {
item?.currentTime = millisUntilFinished
helper?.getView<TextView>(R.id.task_tv_limit_time)?.text =
TimeUtil.LongtoDuration(millisUntilFinished / 1000.toDouble(), ":", ":", "")
}
}.start()
timeList.add(helper?.countDownTimer!!)
} else {
helper?.getView<TextView>(R.id.task_tv_limit_time)!!.text = ""
}
}
}
}
fun clearTimeList() {
timeList.forEach {
it.cancel()
}
timeList.clear()
}
fun setCurrentTime() {
notifyDataSetChanged()
}
//countDownTime与viewholder绑定
class TaskNewViewHolder(item: View) : BaseViewHolder(item) {
var countDownTimer:CountDownTimer?=null
}
}