List 和MAP的区别
需求:返回的List卡片,某些卡片需要显示倒计时,解决方法:需要显示显示倒计时,则创建相对于的倒计时,当继续执行响应倒计时则不需要重新创建,更具list的标识即可判断
备注:HashMap key和value一一对应倒,key对应的value为null,则创建
/** 倒计时集合. */
private var timerList: MutableList<FixCountDownTimer?> = mutableListOf()
//标记哪些卡片带有倒计时功能
private var timerPositionList = arrayListOf<Int>()
private fun initTime(
start: Boolean = false,
tvCountTime: TextView,
timeInter: Long,
cardPosition: Int
) {
//未标记有倒计时功能的时候执行
if (!timerPositionList.contain(cardPosition) ) {
tvCountTime?.text = spannableStringBuilder(getGlobalContext()) {
append(getString(R.string.string_count_time))
append((timeInter).formatToDDHHString())
}
//添加指定卡片到标记集合
timerPositionList.add(cardPosition)
//添加指定卡片的倒计时到倒计时集合
timerList.add(object : FixCountDownTimer(timeInter, 1000L) {
override fun onFinish() {
//请求接口,重新小助手
viewModel.refresh()
cancel()
}
override fun onTiack(millisUntilFinished: Long) {
tvCountTime?.text = spannableStringBuilder(getGlobalContext()) {
append(getString(R.string.string_count_time))
append((millisUntilFinished).formatToDDHHString())
}
}
})
if (start) {
//开启指定倒计时
timerList[timerPositionList.indexOf(cardPosition)]?.setCurrentTime(timeInter)
timerList[timerPositionList.indexOf(cardPosition)]?.start()
}
}
}
private var timerMap: HashMap<Int, FixCountDownTimer>? = hashMapOf()
private fun initTime(
start: Boolean = false,
tvCountTime: TextView,
timeInter: Long,
cardPosition: Int
) {
//未标记有倒计时功能的时候执行
if (timerMap?.get(cardPosition) == null) {
tvCountTime?.text = spannableStringBuilder(getGlobalContext()) {
append(getString(R.string.string_count_time))
append((timeInter).formatToDDHHString())
}
var fixCountDownTimer = object : FixCountDownTimer(timeInter, 1000L) {
override fun onFinish() {
//请求接口,重新小助手
viewModel.refresh()
cancel()
}
override fun onTick(millisUntilFinished: Long) {
tvCountTime?.text = spannableStringBuilder(getGlobalContext()) {
append(getString(R.string.string_count_time))
append((millisUntilFinished).formatToDDHHString())
}
}
}
timerMap?.set(cardPosition, fixCountDownTimer)
if (start) {
//开启指定倒计时
timerMap?.get(cardPosition)?.setCurrentTime(timeInter)
timerMap?.get(cardPosition)?.start()
}
}
}