Adapter
class SelectMallAllOrderFragmentAdapter(layout: Int) : BaseQuickAdapter<OrderDetailPageBean.RecordsDTO, SelectMallAllOrderFragmentAdapter.TimerViewHolder>(layout) {
private var timeList = arrayListOf<CountDownTimer>()
override fun convert(holder:TimerViewHolder, item: OrderDetailPageBean.RecordsDTO) {
//避免复用释放资源
holder?.countTimer?.cancel()
var date = holder.getView<TextView>(R.id.date)
var Time:Long= 163947332699//定义时间
if (Time!= null){
//获取时间差
var imeDifference = System.currentTimeMillis() -Time //当前时间-时间
var endTime = Time * 1000 - imeDifference
if (endTime != null && endTime > 0) {
date.setVisibility(View.VISIBLE)
holder?.countTimer = object : CountDownTimer(endTime, 1000) {
override fun onTick(millisUntilFinished: Long) {
date.setVisibility(View.VISIBLE)
var timeShow =
setTimeShow((millisUntilFinished / 1000).toLong())
date.setText(timeShow)
}
override fun onFinish() {//结束倒计时 隐藏view
date.setVisibility(View.GONE)
button2.visibility = View.GONE
//释放资源
timeList.remove(holder?.countTimer)
}
}.start()
timeList.add(holder?.countTimer!!)
} else {
date.setVisibility(View.GONE)
button2.visibility = View.GONE
}
} else {
date.setVisibility(View.GONE)
button2.visibility = View.GONE
」
}
}
} catch (e: Exception) {
e.printStackTrace()
}
//点击条目跳转
holder.itemView.setOnClickListener {
var intent = Intent(context, SelectMallOrderDetailsActivity::class.java)
intent.putExtra("orderDetailId", item.orderDetailId)
context.startActivity(intent)
}
}
private fun setTimeShow(useTime: Long): String {
var day = (useTime / 86400)
val daytag = (useTime % 86400).toInt()
var hour = daytag / 3600
var hourTag = daytag % 3600
var min = hourTag / 60
var second = hourTag % 60
var mDay: String
val mHour: String
val mMin: String
val mSecond: String //天,小时,分钟,秒
mHour = if (hour < 10) {
"0$hour"
} else {
"" + hour
}
mMin = if (min < 10) {
"0$min"
} else {
"" + min
}
mSecond = if (second < 10) {
"0$second"
} else {
"" + second
}
// val strTime = "距结束:" + day.toInt() + "天" + mHour + "时" + mMin + "分" + mSecond + "秒"
val strTime =mMin + "分" + mSecond + "秒"
return strTime
}
class TimerViewHolder(item: View) : BaseViewHolder(item) { var countTimer:CountDownTimer?=null }
public fun clearTimeList() {
timeList.forEach {
it.cancel()
}
timeList.clear()
}
}
Fragment 或者activity(释放资源)
override fun onDestroyView() {
super.onDestroyView()
if (newSelectMallAdapter != null) {
try {
if (newSelectMallAdapter != null) {
newSelectMallAdapter.clearTimeList()
}
}catch (e:Exception){
e.printStackTrace()
}
}
}
|