我正在使用新的支持库ListAdapter.这是我的适配器代码
class ArtistsAdapter : ListAdapter<Artist, ArtistsAdapter.ViewHolder>(ArtistsDiff()) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(parent.inflate(R.layout.item_artist))
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bind(getItem(position))
}
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
fun bind(artist: Artist) {
itemView.artistDetails.text = artist.artistAlbums
.plus(" Albums")
.plus(" \u2022 ")
.plus(artist.artistTracks)
.plus(" Tracks")
itemView.artistName.text = artist.artistCover
itemView.artistCoverImage.loadURL(artist.artistCover)
}
}
}
Run Code Online (Sandbox Code Playgroud)
我正在更新适配器
musicViewModel.getAllArtists().observe(this, Observer {
it?.let {
artistAdapter.submitList(it)
}
})
Run Code Online (Sandbox Code Playgroud)
我的差异类
class ArtistsDiff : DiffUtil.ItemCallback<Artist>() {
override fun areItemsTheSame(oldItem: Artist?, newItem: Artist?): Boolean { …Run Code Online (Sandbox Code Playgroud) 我正在尝试将 ListAdapter 与 Room 和 LifeData 一起使用。但是我遇到了 DiffUtil.ItemCallback 的奇怪行为 - areContentsTheSame() 方法中的对象总是相同的。添加和删除对象没问题,但更改内容有问题。
物品类别:
@Entity(tableName = "item")
data class Item(var num: Int) {
@PrimaryKey(autoGenerate = true)
var key: Int = 0
}
Run Code Online (Sandbox Code Playgroud)
适配器类
class LifeAdapter : ListAdapter<Item, LifeAdapter.ViewHolder>(DiffCallback()) {
private class DiffCallback : DiffUtil.ItemCallback<Item>() {
override fun areItemsTheSame(oldItem: Item, newItem: Item) = oldItem.key == newItem.key
override fun areContentsTheSame(oldItem: Item, newItem: Item) = oldItem.num == newItem.num
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_item, parent, false)
return …Run Code Online (Sandbox Code Playgroud) 我正在使用 DiffUtil.ItemCallback 和 ListAdapter 来更新 RecyclerView。
它工作得很好,但它的行为让我感到困扰。
根据文档,areContentsTheSame只有在areItemsTheSame返回时才应调用该方法true。
从文档:
仅当 areItemsTheSame(int, int) 为这些项目返回 true 时才调用此方法。
但是,就我而言,在使用新列表更新列表时,如下所示:
adapter.submitList(items.toMutableList())
Run Code Online (Sandbox Code Playgroud)
DiffUtil.ItemCallback 确实被触发,但即使areItemsTheSame返回true它也不会立即调用areContentTheSame(如预期的那样?),而是areItemsTheSame在不同的对象对上一次又一次地调用,(我在那里使用了带有断点的调试器)。经过areItemsTheSame几次调用并返回true,最后它会调用areContentTheSame,有时,我看到传递给的对象areContentTheSame实际上不一样!!并且有不同的id!!这意味着areContentTheSame甚至不应该调用这些对!
这是我的 DiffUtilCallback 的实现方式:
class AppDiffCallback : DiffUtil.ItemCallback<Item?>() {
override fun areItemsTheSame(oldItem: Item, newItem: Item): Boolean {
Timber.i("areItemsTheSame: called..")
if (oldItem.getItemType() == newItem.getItemType()) {
Timber.i("areItemsTheSame: done true..")
return true
}
Timber.w("areItemsTheSame: done false..")
return false
}
override fun areContentsTheSame(oldItem: Item, …Run Code Online (Sandbox Code Playgroud) android android-recyclerview android-diffutils android-listadapter
我正在使用MVVM体系结构来构建简单的订购应用程序。我在我的ProductsFragment中使用RecyclerView列出了所有可以订购的产品。我还在ViewModel中使用LiveData,在Fragment中可以观察到它,以检查对产品列表的任何更改。
在我的列表项中,我有3个按钮:1个按钮,用于将产品添加到购物篮中,另一个按钮,用于增加客户想要添加到购物篮中的数量,第三个按钮用于减少客户想要添加到购物篮中的数量。
在我的产品数据类中,每当客户单击增量或减量按钮时,数量都会在数据模型中更新。
我还使用数据绑定将产品绑定到recyclerview列表项布局和Click侦听器。我正在使用ListAdapter开箱即用地访问DiffUtil。
我遇到的问题是,当通知了observable时,我想使用ListAdapter的SubmitList方法,以便在RecyclerView中仅更新已更改的项目。但是,我注意到DiffUtil方法areContentsTheSame()始终返回true。因此,列表项未更新。我不想使用notifyDatasetChanged,因为这会阻塞UI线程。
我遇到的另一个问题是,当我向购物篮中添加产品时,会保留对该产品的引用,因此当我向购物篮中添加产品时,当我只想更新MutableLiveData时,MutableLiveData也将更新。将产品添加到购物篮时,如何停止对正在创建的LiveData的引用?
产品浏览模型
class ProductsViewModel : ViewModel() {
// LIVE DATA
private val _basket = MutableLiveData<Basket>()
val basket: LiveData<Basket>
get() = _basket
private val _products = MutableLiveData<List<Product>>()
val products: LiveData<List<Product>>
get() = _products
init {
_basket.value = Basket()
_products.value = dummyData
}
fun onIncrementProductQuantityButtonPressed(product: Product) {
//product.quantity += 1
//val newList = _products.value
//_products.value = newList
val newProduct = product.copy(quantity = product.quantity.plus(1))
_basket.value!!.updateProductInBasket(newProduct)
_basket.value = _basket.value
}
fun onDecrementProductQuantityButtonPressed(product: Product) {
if (product.quantity>1) {
//product.quantity = …Run Code Online (Sandbox Code Playgroud) android kotlin android-recyclerview android-mvvm android-livedata