如何为Recyclerview中未选择的其他项目设置操作

时间:2020-09-04 07:34:39

标签: android android-recyclerview

我正在使用回收站视图适配器库来显示我的清单和回收站视图。我有一个要显示的复选框。我想要的是一次选择复选框。一旦我选中了一个复选框,则所有先前选中的复选框都应取消选中。

适配器库

https://github.com/utsmannn/Recycling

代码


    val genderList = resources.getStringArray(R.array.gender_list).toList()
    gender_rv.setupAdapter<String>(R.layout.specialty_layout_item){adapter, context, list ->
        bind { itemView, position, item ->
            itemView.specialty_item_checkbox.show()
            itemView.specialty_item_checkbox.text = item

            if (itemView.specialty_item_checkbox.isChecked){
                genderValue += itemView.specialty_item_checkbox.text.toString().toLowerCase()
            }
        }
        setLayoutManager(LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, true))
        submitList(genderList)
    }

2 个答案:

答案 0 :(得分:0)

我在一个项目中所做的就是:

fun bind(item: Item, onItemToggled: (Item) -> Unit) {
        itemView.checkbox.setOnCheckedChangeListener(null)
        itemView.checkbox.isChecked = item.selected
        itemView.checkbox.setOnCheckedChangeListener { compoundButton, b ->
            onItemToggled(item.copy(selected = !item.selected))
        }
}

如果您只想检查一项,我将映射该列表以确保只选择了一项:

data class RecyclerItem(val text: String, val selected: Boolean = false)

var genderList = resources.getStringArray(R.array.gender_list).toList().map {
    RecyclerItem(text = it)
}
gender_rv.setupAdapter<String>(R.layout.specialty_layout_item){adapter, context, list ->
        bind { itemView, position, item ->
            itemView.specialty_item_checkbox.show()
            itemView.specialty_item_checkbox.text = item.text
          itemView.specialty_item_checkbox.setOnCheckedChangeListener(null)
            itemView.specialty_item_checkbox.isChecked = item.selected
            itemView.checkbox.specialty_item_checkbox { compoundButton, b ->
            genderList = genderList.map {
                if(it == item) {
                    it.copy(selected = !item.selected)
                } else {
                    it.copy(selected = false)
                }
            adapter.submitList(genderList)
        }
    }

答案 1 :(得分:0)

我可以使用下面的方法来处理它。

var genderList = resources.getStringArray(R.array.gender_list).toList().map {
            RecyclerItem(text = it)
        }
        val checkboxes = arrayListOf<CheckBox>()

        gender_rv.setupAdapter<RecyclerItem>(R.layout.specialty_layout_item) { adapter, context, list ->
            bind { itemView, position, item ->
                itemView.specialty_item_checkbox.show()
                itemView.specialty_item_checkbox.text = item?.text
                itemView.specialty_item_checkbox.isChecked = item?.selected!!

                checkboxes.add(itemView.specialty_item_checkbox)
                itemView.specialty_item_checkbox.setOnCheckedChangeListener { compoundButton, b ->
                    if (b) {
                        compoundButton.isChecked = true
                        val otherCheckboxes =
                            checkboxes.filter { checkBox -> checkBox.text != compoundButton.text }
                        otherCheckboxes.forEach { checkbox ->
                            if (checkbox.isChecked) {
                                checkbox.isChecked = !checkbox.isChecked
                            } else {

                            }
                        }
                        val sz = otherCheckboxes.size
                        Log.i("checkbox", "hello ${compoundButton.text} othersSize $sz")
                    }


                }

            }
            setLayoutManager(
                LinearLayoutManager(
                    requireContext(),
                    LinearLayoutManager.HORIZONTAL,
                    false
                )
            )
            submitList(genderList)
        }