Recycler视图项目中的单个CountDownTimer(Kotlin)



我有一个应用程序,它将向回收器视图数据库添加一个项目。当一个项目被添加到回收器视图时,该项目有自己的倒计时计时器。我知道如何在回收器视图中插入计时器,但每次我向回收器视图数据库添加新项目时,其他项目的计时器都会重置。我还希望计时器即使在应用程序关闭时也能运行

这是我的主要活动

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val btnAdd = findViewById<Button>(R.id.btnAdd)
val ivEdit = findViewById<ImageView>(R.id.ivEdit)
btnAdd.setOnClickListener { view ->
addRecord()
setupListofDataIntoRecyclerView()
}
}
//Method for saving the employee records in database
private fun addRecord() {
val etName = findViewById<EditText>(R.id.etName)
val etEmailId = findViewById<EditText>(R.id.etEmailId)
val name = etName.text.toString()
val email = etEmailId.text.toString()
val databaseHandler: DatabaseHandler = DatabaseHandler(this)
if (!name.isEmpty() && !email.isEmpty()) {
val status =
databaseHandler.addEmployee(EmpModelClass(0, name, email))
if (status > -1) {
Toast.makeText(applicationContext, "Record saved", Toast.LENGTH_LONG).show()
etName.text.clear()
etEmailId.text.clear()
}
} else {
Toast.makeText(
applicationContext,
"Name or Email cannot be blank",
Toast.LENGTH_LONG
).show()
}
}
private fun getItemsList(): ArrayList<EmpModelClass> {
//creating the instance of DatabaseHandler class
val databaseHandler: DatabaseHandler = DatabaseHandler(this)
//calling the viewEmployee method of DatabaseHandler class to read the records
val empList: ArrayList<EmpModelClass> = databaseHandler.viewEmployee()
return empList
}
private fun setupListofDataIntoRecyclerView() {
val rvItemsList = findViewById<RecyclerView>(R.id.rvItemsList)
val tvNoRecordsAvailable = findViewById<TextView>(R.id.tvNoRecordsAvailable)
if (getItemsList().size > 0) {
rvItemsList.visibility = View.VISIBLE
tvNoRecordsAvailable.visibility = View.GONE
// Set the LayoutManager that this RecyclerView will use.
rvItemsList.layoutManager = LinearLayoutManager(this)
// Adapter class is initialized and list is passed in the param.
val itemAdapter = ItemAdapter(this, getItemsList())
// adapter instance is set to the recyclerview to inflate the items.
rvItemsList.adapter = itemAdapter
} else {
rvItemsList.visibility = View.GONE
tvNoRecordsAvailable.visibility = View.VISIBLE
}
}
}

这是我的项目适配器

class ItemAdapter(val context: Context, val items: ArrayList<EmpModelClass>) :
RecyclerView.Adapter<ItemAdapter.ViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(
LayoutInflater.from(context).inflate(
R.layout.item_row,
parent,
false
)
)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item = items.get(position)
holder.tvEmail.text = item.email
// Updating the background color according to the odd/even positions in list.
if (position % 2 == 0) {
holder.llMain.setBackgroundColor(
ContextCompat.getColor(
context,
R.color.colorLightGray
)
)
} else {
holder.llMain.setBackgroundColor(ContextCompat.getColor(context, R.color.colorWhite))
}
holder.ivEdit.setOnClickListener { view ->
if (context is MainActivity) {
context.updateRecordDialog(item)
}
}
holder.ivDelete.setOnClickListener { view ->
if (context is MainActivity) {
context.deleteRecordAlertDialog(item)
}
}
object : CountDownTimer(1800000, 500) {
override fun onTick(millisUntilFinished: Long) {
val seconds = millisUntilFinished / 1000
val minutes = seconds / 60
val hours = minutes / 60
val time = hours.toString() + hours % 24 + ":" + minutes % 60 + ":" + seconds % 60
holder.tvName.setText(time)
}
override fun onFinish() {
holder.tvName.setText("Time up!")
}
}.start()
}
/**
* Gets the number of items in the list
*/
override fun getItemCount(): Int {
return items.size
}
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
// Holds the TextView that will add each item to
val llMain: View = itemView.findViewById(R.id.llMain)
val tvName: TextView = itemView.findViewById(R.id.tvName)
val tvEmail: TextView = itemView.findViewById(R.id.tvEmail)
val ivEdit: View = itemView.findViewById(R.id.ivEdit)
val ivDelete: View = itemView.findViewById(R.id.ivDelete)
}

}

您的计时器会重置,因为每次添加新项目时,都会为创建新计时器的每个项目调用onBindViewHolder

如果只在末尾添加项,则可以使用带有DiffCallback的ListAdapter。这将阻止适配器重新绑定未更改的项,从而阻止重新启动计时器。

样本差异回调:

class DiffCallback : DiffUtil.ItemCallback<EmpModelClass>() {
override fun areItemsTheSame(
oldItem: EmpModelClass,
newItem: EmpModelClass
): Boolean = // use id or other identifier to determine if employees are the same

override fun areContentsTheSame(
oldItem: EmpModelClass,
newItem: EmpModelClass
): Boolean = // compare classes for changes
}

如果要在旧项之间添加新项,则如果在onBindViewHolder中绑定了相同的EmpModelClass而不是启动新计时器,则必须存储并重新应用计时器状态。

最新更新