I create a checkboxes adapter in a bottom sheet like this :
I need 3 conditions :
- the "CHECK ALL" checkbox can check all checkboxes in adapter, also can uncheck all.
- save all datas after check the "CHECK ALL" checkbox
- set checkbox isChecked condition from saved data when re-open the bottom sheet adapter.
in my onBindViewHolder, i do like
holder.binding.checkbox.setOnCheckedChangeListener(null)
holder.binding.checkbox.isChecked = item.isChecked
setCheckboxTextColor(item.isChecked, holder)
holder.binding.checkbox.setOnCheckedChangeListener { buttonView, isChecked ->
if (item.isChecked != isChecked) {
setCheckboxTextColor(isChecked, holder)
item.isChecked = isChecked
val indexList = mutableListOf<Int>()
listData.filter { it.id == item.id }.forEach {
it.isChecked = isChecked
indexList.add(listData.indexOf(it))
if (isChecked) {
datas.add(it)
} else {
datas.remove(it)
}
}
indexList.forEach {
notifyItemChanged(it)
}
onClick(datas)
}
}
It's ok for a single selection checkbox, but I can't set the checked condition for the checkbox. So what should I do for the "CHECK ALL" and set a checkbox state for the re-open condition ?
in addition for activity - bottom sheet - adapter codes :
//activity
binding.edittext.setOnClickListener {
val dialog = ListBottomSheet("list-tools", id_location)
dialog.show(childFragmentManager, "dialog")
dialog.setOnclick(object : ListBottomSheet.OnSelectedString {
override fun onItemSelected(data: List<ListCheckboxModel>, position: List<Int>) {
posJenis.clear()
posJenis.addAll(position)
datas.clear()
datas.addAll(data)
isLog("act::$datas")
binding.etJenisItem.setText("${position.size} terpilih")
dialog.dismiss()
}
})
}
//bottom sheet
private val adapter by lazy(LazyThreadSafetyMode.NONE) {
ListCheckboxAdapter(
::onClicked
)
}
private var datas: MutableList<ListCheckboxModel> = ArrayList()
private var selectedDatas: MutableList<ListCheckboxModel> = ArrayList()
datas.clear()
datas.add(ListCheckboxModel("0000", nama = "CHECK ALL", keterangan = "check semua", isChecked = false))
datas.addAll(it)
adapter.clear()
adapter.insertAll(datas, selectedDatas)
private fun onClicked(data: List<ListCheckboxModel>) {
selectedDatas.clear()
selectedDatas.addAll(data)
}
//adapter
class ListCheckboxAdapter(
private val onClick: (List<ListCheckboxModel>) -> Unit
) : RecyclerView.Adapter<ListCheckboxAdapter.ViewHolder>() { ...
private var datas: MutableList<ListCheckboxModel> = ArrayList()
var listData: MutableList<ListCheckboxModel> = ArrayList()
fun insertAll(
data: List<ListCheckboxModel>,
data2: List<ListCheckboxModel>
) {
data.forEach {
listData.add(it)
notifyItemInserted(listData.size - 1)
}
datas.addAll(data2)
}
Aucun commentaire:
Enregistrer un commentaire