【问题标题】:How to show all checkboxes in recyclerview?如何在recyclerview中显示所有复选框?
【发布时间】:2020-06-23 20:19:00
【问题描述】:

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

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

如何更改所有复选框?

【问题讨论】:

  • 您的 ViewHolder 的文本视图和复选框真的需要为空吗?

标签: android kotlin checkbox android-recyclerview adapter


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2020-09-03
    • 2018-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-23
    • 2018-04-23
    相关资源
    最近更新 更多