Как показать все чекбоксы в утилитарном обзоре? - PullRequest
1 голос
/ 11 марта 2020

Я хочу отобразить все флажки при длительном нажатии, но отображается только один. Есть часть моего адаптера

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

Как изменить все флажки?

1 Ответ

0 голосов
/ 11 марта 2020

Используйте свойство в классе адаптера, которое определяет, должны ли отображаться флажки. Вызовите 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
    }
}
...