如何在 recyclerview 中显示所有复选框?

How to show all checkboxes in recyclerview?

我想通过长按显示所有复选框,但只显示一个 我的适配器有一部分

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
    }
}

如何更改所有复选框?

在适配器 class 中使用 属性 来确定是否应显示复选框。调用notifyDataSetChanged()强制反弹所有item views,这样它就有机会修改它们中的每一个。

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
    }
}