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

问题描述 投票:0回答:1

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

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

如何更改所有复选框?

android kotlin checkbox android-recyclerview adapter
1个回答
0
投票

使用适配器类中的属性来确定是否应显示复选框。调用notifyDataSetChanged()强制所有项目视图反弹,因此将有机会修改每个视图。

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
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.