在我的应用程序中,我想在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()
}
}
}
}
})
}
}
}
我该怎么办?
答案 0 :(得分:0)
您可以在模型类中构建一个接口(需要在您的活动中对其进行初始化),并可以通过在适配器中实现它来使用它来发送信息。另一种方法是制作自己的回调和侦听器。点击这些链接; Java Interfaces,Callback & Listeners。
您可以在此处查看更多指南和示例:Beginning Android Resources
答案 1 :(得分:0)
从onResponse(..)
调用此方法 fun recyclerScroll(){
recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
if (dy > 0) {
// recyclerview is scrolling up
// your code for cancel timer
} else if(layoutManager.findFirstCompletelyVisibleItemPosition() == 0){
// recyclerview is not scrolling
// start timer
}
else{
// recyclerview is scrolling down
// your code for cancel timer
}
}
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
super.onScrollStateChanged(recyclerView, newState)
mSwipeRefreshLayout.isEnabled = gridLayoutManager.findFirstCompletelyVisibleItemPosition() == 0
}
})
}
并从适配器调用它
(context as Your_Activity_Name).recyclerScroll()