gpt4 book ai didi

android - 如何在 Android 上使用定时器进入 recyclerView

转载 作者:行者123 更新时间:2023-11-29 02:22:27 30 4
gpt4 key购买 nike

为什么不帮助我的人?​​??

在我的应用程序中,我想在 recyclerView 的每个项目中显示 timer !
我用 Kotlin 语言编写了以下代码,但是当滚动 recyclerView 计时器重置重新开始startTime开始不继续啦!
我的问题是计时器重置,我希望在recyclerView 项目上滚动时,计时器未重置,我想要继续计时器!

我的适配器代码:

class AuctionsTodayAdapter(val context: Context, val model: MutableList<AuctionsTodayResponse.Res.Today>) :
RecyclerView.Adapter<AuctionsTodayAdapter.MyHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyHolder {
val view = LayoutInflater.from(context).inflate(R.layout.row_bidzila_list, parent, false)
return MyHolder(view)
}

override fun getItemCount(): Int {
return model.size
}

override fun onBindViewHolder(holder: MyHolder, position: Int) {
val modelUse = model[position]
holder.setData(modelUse)
}

override fun onViewAttachedToWindow(holder: MyHolder) {
val pos = holder.adapterPosition
val modelUse = model[pos]
holder.handler.post { holder.timer(modelUse.calculateEnd, 1000) }
}

override fun onViewDetachedFromWindow(holder: MyHolder) {
if (holder.timmer != null) {
holder.timmer.cancel()
}
}

inner class MyHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

var handler = Handler(Looper.getMainLooper())
lateinit var timmer: CountDownTimer

fun setData(model: AuctionsTodayResponse.Res.Today) {
model.image.let {
Glide.with(context)
.load(Constants.BIDZILA_BASE_URL + it)
.apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.RESOURCE))
.into(itemView.rowBidzila_img)
}
model.title.let { itemView.rowBidzila_title.text = it }
}

fun timer(millisInFuture: Long, countDownInterval: Long): CountDownTimer {
timmer = object : CountDownTimer(millisInFuture * 1000, countDownInterval) {
override fun onTick(millisUntilFinished: Long) {
var seconds = (millisUntilFinished / 1000).toInt()
val hours = seconds / (60 * 60)
val tempMint = seconds - hours * 60 * 60
val minutes = tempMint / 60
seconds = tempMint - minutes * 60
itemView.rowBidzila_timer.rowBidzila_timer.text =
String.format("%02d", hours) + ":" + String.format(
"%02d",
minutes
) + ":" + String.format("%02d", seconds)
}

override fun onFinish() {
itemView.rowBidzila_timer.rowBidzila_timer.text = "Finished"
}
}.start()

return timmer
}
}
}

我的模型:

data class Today(
@SerializedName("auction_status")
val auctionStatus: String = "", // accept
@SerializedName("base_price")
val basePrice: Int = 0, // 120000
@SerializedName("bid_number")
val bidNumber: Int = 0, // 1
@SerializedName("calculate_end")
var calculateEnd: Long = 0, // -9815
@SerializedName("can_offer_before")
val canOfferBefore: Int = 0, // 1
@SerializedName("capacity")
val capacity: Int = 0, // 25
@SerializedName("current_price")
val currentPrice: Int = 0, // 224000
@SerializedName("discount")
val discount: Int = 0, // 400000
@SerializedName("end")
val end: Int = 0, // 1548650312
@SerializedName("end_date")
val endDate: String = "", // 2019-01-28 08:08:32
@SerializedName("end_time")
val endTime: String = "", // 2019-01-28 08:08:32
@SerializedName("final_discount")
val finalDiscount: Int = 0, // 176000
@SerializedName("final_price")
val finalPrice: Int = 0, // 224000
@SerializedName("id")
val id: Int = 0, // 2629
@SerializedName("image")
val image: String = "", // /img/product/5bd94ed3cb9d2.png
@SerializedName("lastbid")
val lastbid: Any = Any(), // null
@SerializedName("live")
val live: String = "",
@SerializedName("max_price")
val maxPrice: Int = 0, // 240000
@SerializedName("max_price_percent")
val maxPricePercent: Int = 0, // 60
@SerializedName("name")
val name: String = "",
@SerializedName("number_of_users")
val numberOfUsers: Int = 0, // 14
@SerializedName("order")
val order: Int = 0, // 0
@SerializedName("price")
val price: Int = 0, // 400000
@SerializedName("product_id")
val productId: Int = 0, // 671
@SerializedName("registered")
val registered: String = "", // null
@SerializedName("registereduser")
val registereduser: Int = 0, // 14
@SerializedName("second_image")
val secondImage: String = "",
@SerializedName("start_date")
val startDate: String = "", // 2019-01-28 08:00:00
@SerializedName("tag")
val tag: String = "",
@SerializedName("tag_color")
val tagColor: String = "", // ff3232
@SerializedName("tag_description")
val tagDescription: String = "",
@SerializedName("title")
val title: String = "",
@SerializedName("winner_avatar")
val winnerAvatar: String = "", // /img/avatar/009-social-11.png
@SerializedName("winner_id")
val winnerId: Int = 0, // 57582
@SerializedName("winner_name")
val winnerName: String = "", // Arashr1

val running: Boolean = true,
var thread: Thread
) {
init {
thread = Thread(Runnable {
while (running) {
calculateEnd--
try {
Thread.sleep(1000)
} catch (e: InterruptedException) {
e.printStackTrace()
}
}
})
thread.start()
}
}

我该如何解决?

最佳答案

您可以使用模型。在此模型中,您可以设置最后的计数值。在您的 ViewHolder 中,您将从模型的最后一个计数值开始计时。希望你明白

关于android - 如何在 Android 上使用定时器进入 recyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54532708/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com