Using same adapter for multiple similar reyclerview implementation

China☆狼群 提交于 2021-01-07 06:31:18

问题


i make an adapter intended to show date and checkbox like image below

it works fine, just like what i want but i got to a problem using same adapter in multiple dialog that i wanna show that have the same behavior

i expect the checkbox in my second recyclerview not checked because i havent check the checkbox on my second recyclerview

the data/ checked checkbox saved on first dialog is showing on my second adapter and so on i have try to using different adapter with same implementation. Here is my adapter,

class SelectedListDateAdapter(var listDate: List<DateDay>, private val onItemCheckListener: OnItemCheckListener) :
RecyclerView.Adapter<SelectedListDateAdapter.SelectedListDateViewHolder>() {
lateinit var  binding: ItemCheckBoxDateBinding
inner class SelectedListDateViewHolder(item: ItemCheckBoxDateBinding) : RecyclerView.ViewHolder(item.root) {
    val checkBoxList = item.checkBox
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SelectedListDateViewHolder {
    binding = ItemCheckBoxDateBinding.inflate(
        LayoutInflater.from(parent.context),
        parent,
        false
    )
    return SelectedListDateViewHolder(binding)
}

override fun onBindViewHolder(holder: SelectedListDateViewHolder, position: Int) {

    holder.itemView.tvDateList.text = listDate[position].date
    holder.checkBoxList.isChecked = listDate[position].isSelected
    holder.checkBoxList.setOnClickListener {

        listDate[position].isSelected = holder.checkBoxList.isChecked
    }

    holder.itemView.setOnClickListener {

        holder.checkBoxList.isChecked = !holder.checkBoxList.isChecked
        listDate[position].isSelected = holder.checkBoxList.isChecked

        val currentItem = listDate[position]
        if (holder.checkBoxList.isChecked) {
            onItemCheckListener.onItemCheck(currentItem.date)
        } else {
            onItemCheckListener.onItemUncheck(currentItem.date)
        }
    }
}

override fun getItemCount(): Int {
    return listDate.size
}

}

i think when im using the same adapter for different recyclerview it will reset the data if it is possible how to do that ? if not should i make different adapter and layout ?

any help appreciated. Thanks


回答1:


Use different data for each adapter, the adapter behavior depends on the data you provide it, so if you provide the same data and you have two recyclerviews they both gonna display the same thing



来源:https://stackoverflow.com/questions/65298668/using-same-adapter-for-multiple-similar-reyclerview-implementation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!