I have a recyclerview with 2 views in each item; a textview and a checkbox. i want the user to be able to select the items by long pressing them. the checkboxes are not visible at first but when user longpresses on an item, they get visible and that specefic item's background color changes to black. tehn when user clicks on each item the background changes to black and the item checkbox gets checked. my problem is that when user clicks on one item, some other item backgrounds turn to black as well. here is my adapter:
`
class RecyclerAdapter(
val data: ArrayList<MyData>,
private val context: Context,
private val activity: MainActivity
) :
RecyclerView.Adapter<RecyclerAdapter.ViewHolder>() {
var isInSelection = false
inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val textview = view.findViewById<TextView>(R.id.textView)
val checkBox = view.findViewById<CheckBox>(R.id.checkbox)
}
@SuppressLint("NotifyDataSetChanged")
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.textview.text = data[position].name
holder.checkBox.isVisible = isInSelection
holder.checkBox.isClickable = isInSelection
holder.checkBox.isChecked=data[position].isChecked
holder.textview.setOnLongClickListener {
if (isInSelection) {
if (data[position].isChecked) {
data[position].isChecked = false
holder.checkBox.performClick()
holder.textview.setBackgroundColor(ContextCompat.getColor(context,R.color.white))
} else {
data[position].isChecked = true
holder.checkBox.performClick()
holder.textview.setBackgroundColor(ContextCompat.getColor(context,R.color.black))
}
} else {
isInSelection = true
data[position].isChecked = true
notifyDataSetChanged()
}
true
}
holder.textview.setOnClickListener {
wasInSelection = isInSelection
if (isInSelection) {
if (data[position].isChecked) {
data[position].isChecked = false
holder.checkBox.performClick()
holder.textview.setBackgroundColor(ContextCompat.getColor(context,R.color.white))
} else {
data[position].isChecked = true
holder.checkBox.performClick()
holder.textview.setBackgroundColor(ContextCompat.getColor(context,R.color.black))
}
}
}
}
`
this is the problem: 1
I also tried to set the checkbox ischecked status in the obBindViewHolder outside the clicklisteners using notifyDataSetChanged but in that way i dont get the checkbox toggle animation.
Aucun commentaire:
Enregistrer un commentaire