В моем приложении я хочу использовать несколько таймер в recyclerview
.
Чтобы не сбрасывать таймер при прокрутке элементов recyclerview
, я записываю CountDown таймер в класс модели.
Для этого я должен позвонить notifyItemChanged
в класс модели .
Но я не знаю, как мне отправить адаптер в класс модели!
Класс моей модели:
data class Today(
@SerializedName("auction_status")
val auctionStatus: String = "", // accept
@SerializedName("base_price")
val basePrice: Int = 0, // 48000
@SerializedName("bid_number")
val bidNumber: String = "", // 1
@SerializedName("calculate_end")
val calculateEnd: Long = 0, // -4458
@SerializedName("can_offer_before")
val canOfferBefore: Int = 0, // 1
@SerializedName("capacity")
val capacity: Int = 0, // 15
@SerializedName("current_price")
val currentPrice: Int = 0, // 112000
@SerializedName("discount")
val discount: Int = 0, // 160000
@SerializedName("end")
var countDownTimer: CountDownTimer? = null,
var currentVal: String
) {
init {
countDownTimer = object : CountDownTimer(calculateEnd * 1000, 1000) {
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
currentVal =
String.format("%02d", hours) + ":" + String.format(
"%02d",
minutes
) + ":" + String.format("%02d", seconds)
// call adapter.notifyItemChanged(..) here
}
override fun onFinish() {
//here you should call the adapter.notifyItemChanged(..) function with the position of this model in the model list
}
}.start()
}
}
Адаптер коды :
class AuctionsTodayAdapter(val context: Context, val model: MutableList<Today>) :
RecyclerView.Adapter<AuctionsTodayAdapter.MyHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyHolder {
val view = LayoutInflater.from(context).inflate(R.layout.row_main_list, parent, false)
val holder = MyHolder(view)
//holder.setIsRecyclable(false)
return holder
}
override fun getItemCount(): Int {
return model.size
}
override fun onBindViewHolder(holder: MyHolder, position: Int) {
}
override fun onViewAttachedToWindow(holder: MyHolder) {
var position = holder.adapterPosition
val modelUse = model[position]
holder.setData(modelUse)
}
override fun onViewDetachedFromWindow(holder: MyHolder) {
if (holder.countTimer != null) {
holder.countTimer.cancel()
}
}
inner class MyHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var newCountDownTimer: CountDownTimer? = null
lateinit var rowMain_timer: TextView
init {
rowMain_timer = itemView.findViewById(R.id.rowMain_timer)
}
fun setData(model: Today) {
model.image.let {
Glide.with(context)
.load(Constants.MAIN_BASE_URL + it)
.apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.RESOURCE))
.into(itemView.rowMain_img)
}
model.title.let { itemView.rowMain_title.text = it }
}
}
Коды деятельности:
class MainActivity : AppCompatActivity() {
private lateinit var apisList: ApisList
private lateinit var retrofit: Retrofit
private lateinit var todayAdapter: AuctionsTodayAdapter
private val todayModel: MutableList<Today> = mutableListOf()
private lateinit var layoutManager: RecyclerView.LayoutManager
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//Initialize
retrofit = ApiClient.instance
apisList = retrofit.create(ApisList::class.java)
todayAdapter = AuctionsTodayAdapter(themedContext, todayModel)
layoutManager = LinearLayoutManager(themedContext)
//RecyclerView
main_list.setHasFixedSize(true)
main_list.layoutManager = layoutManager
main_list.adapter = todayAdapter
if (isNetworkAvailable()) getData(1, 10)
}
private fun getData(page: Int, limit: Int) {
main_loader.visibility = View.VISIBLE
val call = apisList.getAuctionsToday(page, limit)
call.let {
it.enqueue(object : Callback<AuctionsTodayResponse> {
override fun onFailure(call: Call<AuctionsTodayResponse>, t: Throwable) {
main_loader.visibility = View.GONE
Log.e("auctionsTodayList", t.message)
}
override fun onResponse(call: Call<AuctionsTodayResponse>, response: Response<AuctionsTodayResponse>) {
if (response.isSuccessful) {
response.body()?.let { itBody ->
main_loader.visibility = View.GONE
if (itBody.toString().isNotEmpty()) {
todayModel.clear()
todayModel.addAll(itBody.res.today)
todayAdapter.notifyDataSetChanged()
}
}
}
}
})
}
}
}
Как я могу это сделать?