【问题标题】:Using same adapter for multiple similar reyclerview implementation对多个类似的 reyclerview 实现使用相同的适配器
【发布时间】:2020-12-16 15:53:46
【问题描述】:

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

效果很好,就像我想要的一样 但我在多个对话框中使用相同的适配器时遇到了问题,我想展示它具有相同的行为

我希望我的第二个 recyclerview 中的复选框没有选中,因为我没有选中我的第二个 recyclerview 上的复选框

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

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 使用相同的适配器时,它会重置数据 如果有可能怎么办?如果不是,我应该制作不同的适配器和布局吗?

任何帮助表示赞赏。谢谢

【问题讨论】:

    标签: android kotlin android-recyclerview


    【解决方案1】:

    为每个适配器使用不同的数据,适配器的行为取决于你提供的数据,所以如果你提供相同的数据并且你有两个回收器视图,它们都会显示相同的东西

    【讨论】:

    • 你的意思是数据是我在适配器参数中提供的列表吗?
    • 我试过了,我的问题仍然存在。你需要一些关于我的项目的额外信息吗?
    • 这将对您有所帮助,也许您可​​以发布一些显示问题原因的 gif 或屏幕截图,据我了解,您正在为相同的适配器提供相同的数据并期望不同的结果
    • 如果我没有检查,我希望我的复选框没有检查,这是我的问题ibb.co/wsSLFBR
    猜你喜欢
    • 1970-01-01
    • 2021-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-05
    • 2014-10-25
    • 2013-08-27
    • 1970-01-01
    相关资源
    最近更新 更多