本文介绍了对多个相似的reyclerview实现使用相同的适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个适配器,用于显示日期和复选框,如下图所示

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

它工作正常,就像我想要的一样但是我在多个对话框中使用相同的适配器却遇到了问题,我想证明它们具有相同的行为

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

我希望第二个Recyclerview中的复选框未选中,因为我还没有选中第二个Recyclerview中的复选框

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

保存在第一个对话框中的data/checked复选框显示在我的第二个适配器上,依此类推我尝试使用具有相同实现方式的不同适配器.这是我的适配器,

the data/ checked checkbox saved on first dialog is showing on my second adapter and so oni 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
}

}

我认为当即时通讯将同一适配器用于不同的recyclerview时,它将重置数据如果有可能怎么做?如果不是,我应该制作不同的适配器和布局吗?

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

任何帮助表示赞赏.谢谢

any help appreciated. Thanks

推荐答案

每个适配器使用不同的数据,适配器的行为取决于您提供的数据,因此,如果您提供相同的数据并且有两个recyclerviews,它们都会显示相同的东西

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

这篇关于对多个相似的reyclerview实现使用相同的适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 21:52