gpt4 book ai didi

android - List Adapter Diff Util 不更新 Recyclerview 中的列表项

转载 作者:行者123 更新时间:2023-12-05 00:08:42 24 4
gpt4 key购买 nike

列表适配器 diffutil 未更新 recyclerview 中的列表项。
当我从列表中添加或删除项目时,它会起作用。但不更新任何特定值,如 isSelected : Boolean = false在对象类中
当列表中的对象类之一发生更改时如何更新recyclerview View ,例如一个对象类中的值 bool 更改
谢谢
下面的适配器类

class CategoryMainAdapter(
var itemClick : CategoryItemClick,
var screenWidth : Int = 0
) : ListAdapter<CategoryModel, CategoryMainAdapter.CategoryMainViewHolder>(CategoryMainDiffUtils()) {

inner class CategoryMainViewHolder(val binding : CategorySingleListLayoutBinding) : RecyclerView.ViewHolder(binding.root)

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

override fun onBindViewHolder(holder: CategoryMainViewHolder, position: Int) {
val model = getItem(position)
holder.apply {
binding.apply {
mainLinearLayout.layoutParams.apply {
width = ((screenWidth*22.5).toInt())/100
height = (((screenWidth*22.5)/100)*1.2).toInt()
}
categoryName.text = model.name
backgroundColor.setBackgroundColor(model.colorCode)
categoryImage.load(getImageCorrectImage(model.image, model.secondaryImage)) {
error(R.drawable.ic_food_place_holder)
}

if (model.isSelected) {
categoryName.setTextColor(ContextCompat.getColor(categoryName.context, R.color.main_blue))
selectedView.setBackgroundResource(R.drawable.main_blue_curved_bg)
categoryImage.apply {
animation = AnimationUtils.loadAnimation(context, R.anim.category_zoom_in_anim)
setPadding(0, 0, 0, 0)
}
} else {
categoryName.setTextColor(setColorByAttrProgrammatically(categoryName.context, R.attr.colorOnSecondary))
selectedView.setBackgroundResource(android.R.color.transparent)
categoryImage.apply {
animation = AnimationUtils.loadAnimation(context, R.anim.category_zoom_out_anim)
setPadding(1, 1, 1, 1)
}
}
}
itemView.apply {
setOnClickListener {
itemClick.onCategoryClick(model)
}
}
}
}


class CategoryMainDiffUtils : DiffUtil.ItemCallback<CategoryModel>() {
override fun areItemsTheSame(oldItem: CategoryModel, newItem: CategoryModel): Boolean {
return oldItem.isSelected == newItem.isSelected
}

override fun areContentsTheSame(oldItem: CategoryModel, newItem: CategoryModel): Boolean {
return oldItem == newItem
}

}

override fun submitList(list: MutableList<CategoryModel>?) {
super.submitList(list?.let { ArrayList(it) })
}
}

最佳答案

一、areContentsTheSame的执行和 areItemsTheSame可能需要切换。 areContentsTheSame是检查您的元素中的东西是否不同的一种。 areItemsTheSame是关于它是否是同一个项目。如果项目有一个 id,您可能想要比较它们而不是项目本身。但仅此一项可能无法解决您的问题。
问题是,如果列表包含不可变的项目,ListAdapter 效果最好。理想情况下,您希望每次调用 submitList 时都提交一个新列表。 .因为如果您更改 isSelected 之类的属性在现有的 lisk 中,只需使用相同的列表调用 submitList 就不会发生任何事情。
所以你想要做的是而不是改变 isSelected属性,您需要创建列表的副本,并且仅在该副本中您更改 isSelected并将副本传递给 submitList .
这可能很乏味。另一种可能可行的丑陋解决方法是添加第二个属性,可能称为 oldIsSelected并且只在 DiffUtil 本身中更改那个,像这样

    override fun areContentsTheSame(oldItem: CategoryModel, newItem: CategoryModel): Boolean {
val result = oldItem.oldIsSelected == newItem.isSelected
newItem.oldIsSelected = newItem.isSelected
return result
}

关于android - List Adapter Diff Util 不更新 Recyclerview 中的列表项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70921098/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com