How to show all checkboxes in recyclerview?

Sergey1 :

I want to display all the checkboxes by long press, but only one is displayed There is a part of my adapter

override fun onBindViewHolder(holder: ViewHoldder, position: Int) {
    holder.textView?.text = lstWords[position].engWord
    holder.textView2?.text = lstWords[position].transWord
    holder.textView3?.text = lstWords[position].rusWord
    holder.checkBox?.setOnCheckedChangeListener(object : CompoundButton.OnCheckedChangeListener {
        override fun onCheckedChanged(buttonView: CompoundButton?, isChecked: Boolean) {
            val word = Words(
                lstWords[position].idWord!!,
                lstWords[position].engWord!!,
                lstWords[position].transWord!!,
                lstWords[position].rusWord!!,
                lstWords[position].check!!
            )
            if (holder.checkBox?.isChecked!!){
                words.add(word.idWord.toString())
                Log.d("MYTAG", "$words")
            }
            if (!holder.checkBox?.isChecked!!){
                words.remove(word.idWord.toString())
                Log.d("MYTAG", "$words")
            }
        }
    })
    holder.itemView.setOnLongClickListener {
        holder.checkBox?.visibility = CheckBox.VISIBLE
        true
    }
}

How to change all checkboxes?

Tenfour04 :

Use a property in the adapter class that determines if check boxes should be shown. Call notifyDataSetChanged() to force all item views to be rebound, so it will have a chance to modify each of them.

private var shouldShowCheckBoxes = false

//...

override fun onBindViewHolder(holder: ViewHoldder, position: Int) {
    //...
    holder.checkBox?.visiblility = if (shouldShowCheckBoxes) View.VISIBLE else View.INVISIBLE
    holder.itemView.setOnLongClickListener {
        shouldShowCheckBoxes = true
        notifyDataSetChanged()
        true
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=342113&siteId=1