gpt4 book ai didi

android - DiffUtil.ItemCallback 不更新项目位置(删除后)

转载 作者:行者123 更新时间:2023-12-05 00:13:44 78 4
gpt4 key购买 nike

我使用 ListAdapter 的绑定(bind)和 DiffUtil.ItemCallback 的定义。删除项目(至少 2 个)时出现 IndexOutOfBoundsException。列表的更新有效(删除后元素的数量确实是 N-1)但不是项目的位置,保留的是调用。因此在调用 getItem(position) 时抛出异常(在 onBindViewHolder 中)。注意:getItem(position) 之前的 getItemCount() 日志显示该列表包含 N-1 个元素。我创建了一个小仓库:https://github.com/jeremy-giles/DiffListAdapterTest (与我的项目具有相同的配置)重现了问题。

ItemAdapter 类

class ItemAdapter(
var listener: ListAdapterListener) : DataBindingAdapter<Item>(DiffCallback()) {

class DiffCallback : DiffUtil.ItemCallback<Item>() {

override fun areItemsTheSame(oldItem: Item, newItem: Item): Boolean {
return oldItem == newItem
}

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

override fun getItemViewType(position: Int) = R.layout.recycler_item

override fun onBindViewHolder(holder: DataBindingViewHolder<Item>, position: Int) {
super.onBindViewHolder(holder, position)

holder.itemView.tv_position.text = "Pos: $position"

holder.itemView.setOnLongClickListener {
Timber.d("List item count: ${itemCount}, position: $position")
listener.onLongViewClick(getItem(position), position)
}
}

interface ListAdapterListener {
fun onLongViewClick(item: Item, position: Int) : Boolean
}
}

BindingUtils 类

abstract class DataBindingAdapter<T>(diffCallback: DiffUtil.ItemCallback<T>) :
ListAdapter<T, DataBindingViewHolder<T>>(diffCallback) {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DataBindingViewHolder<T> {
val layoutInflater = LayoutInflater.from(parent.context)
val binding = DataBindingUtil.inflate<ViewDataBinding>(layoutInflater, viewType, parent, false)

return DataBindingViewHolder(binding)
}

override fun onBindViewHolder(holder: DataBindingViewHolder<T>, position: Int) {
holder.bind(getItem(position))
}
}

class DataBindingViewHolder<T>(private val binding: ViewDataBinding) :
RecyclerView.ViewHolder(binding.root) {

fun bind(item: T) {
binding.setVariable(BR.item, item)
binding.executePendingBindings()
}
}

在我的 MainActivity 类中,我使用 LiveData 更新 recyclerView

itemViewModel.getListObserver().observe(this, Observer {
Timber.d("List Observer, items count ${it.size}")
itemAdapter.submitList(it.toList())
})

最佳答案

在您的 onBindViewHolder 中,将“position”的用法更新为“holder.getAdapterPosition()”:

override fun onBindViewHolder(holder: DataBindingViewHolder<Item>, position: Int) {
super.onBindViewHolder(holder, position)

holder.itemView.tv_position.text = "Pos: $position"

holder.itemView.setOnLongClickListener {
Timber.d("List item count: ${itemCount}, position: $position")
listener.onLongViewClick(getItem(holder.getAdapterPosition()), holder.getAdapterPosition())
}
}

关于android - DiffUtil.ItemCallback 不更新项目位置(删除后),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59560135/

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