- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的 diffcallback areContentsTheSame(oldItem: ItemModel, newItem: ItemModel)
总是接收相同的内容。我用状态来检查,但每次状态都是一样的。尽管状态实际上正在发生变化。我打算显示每个项目的进度。所以我定期通过状态发送当前进度。使用 diffcallback,它应该检查项目的状态是否不同,然后只更新该项目。但是它接收到的 newItem 和 oldItem 似乎是一样的。
我有一个自定义模型 ItemModel
data class ItemModel(val id: String, var title: String) {
var clickListener: ClickListener? = null
var status: Status? = null
interface ClickListener{
fun onItemClick(view: View, item: ItemModel)
fun onClick(view: View, item: ItemModel)
}
companion object {
val STATUS_CHANGED = 1
val diffCallback = object : DiffUtil.ItemCallback<ItemModel>() {
override fun areItemsTheSame(oldItem: ItemModel, newItem: ItemModel): Boolean {
// Log.i("DiffUtil", "SameItem? old status: ${oldItem.status}, new Status: ${newItem.status}")
return oldItem.id == newItem.id
}
override fun areContentsTheSame(oldItem: ItemModel, newItem: ItemModel): Boolean {
// Log.i("DiffUtil", "Checking status: new: ${newItem.status}, old ${oldItem.status}")
return oldItem.status == newItem.status
}
override fun getChangePayload(oldItem: ItemModel, newItem: ItemModel): Any? {
Log.i("DiffUtil", "Payload change: ${newItem.status?.state}")
if (oldItem.status != newItem.status) {
return STATUS_CHANGED
}
return null
}
}
}
}
和状态数据类
data class Status(val max: Int, val progress: Int, val state: State = State.NONE)
这是我的 ViewModel 类;我使用 Observable.intervalRange
生成不同的数字并更改单个列表项的状态。但是似乎 diffcallback 没有正常工作。
class FunViewModel : ViewModel() {
private val itemModels: MutableLiveData<List<ItemModel>> = MutableLiveData()
fun items(): LiveData<List<ItemModel>> {
return itemModels
}
private val compositeDisposable = CompositeDisposable()
fun initialize(itemList: List<ItemModel>) {
itemModels.value = itemList
}
private fun updateItem(item: ItemModel, status: Status) {
val currentItems = mutableListOf<ItemModel>()
if (itemModels.value == null) return
currentItems.addAll(itemModels.value!!)
Log.i("UpdateItem", "Current item: ${item.id}")
if (currentItems.isNotEmpty()) {
for ((index, el) in currentItems.withIndex()) {
// Log.i("UpdateItem", "searching: ${el.id}")
if (el.id == item.id) {
val currentItem = currentItems.removeAt(index)
Log.i("UpdateItem", "old status: ${currentItem.status}")
currentItem.status = status
currentItems.add(index, currentItem)
break
}
}
itemModels.value = currentItems
Log.i("UpdateItem", "new status: ${items().value?.get(0)?.status}")
}
}
fun startProgress(item: ItemModel) {
val disposable = getProgress(item.id).map { progress ->
val status = Status(progress.second.toInt(), progress.third.toInt(), State.IN_PROGRESS)
status
}.subscribeOn(Schedulers.single())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ status ->
updateItem(item, status)
// Log.i("FunViewModel:", "Status: progress: ${status.progress}, State: ${status.state}")
},
{ throwable: Throwable? -> Log.e("FunViewModel", "Unable to process progress!", throwable) },
{
val status = Status(20, 20, State.COMPLETED)
updateItem(item, status)
Log.i("FunViewModel:", "Status: progress: ${status.progress}, State: ${status.state}")
})
compositeDisposable.add(disposable)
}
private fun getProgress(id: String): Observable<Triple<String, Long, Long>> {
return Observable.intervalRange(0, 20, 300, 500, TimeUnit.MILLISECONDS)
.map { num -> Triple<String, Long, Long>(id, 20, num) }
}
override fun onCleared() {
super.onCleared()
if (!compositeDisposable.isDisposed) compositeDisposable.dispose()
}
}
最佳答案
好吧,我发现问题是我复制原始内容的方式不是实际复制内容,而是引用。所以我通过将项目内容复制到新的 itemModel 实例中来解决问题。完成后,diff util 就能够正确区分。
代替这个,
private fun updateItem(item: ItemModel, status: Status) {
......................................................
for ((index, el) in currentItems.withIndex()) {
if (el.id == item.id) {
val currentItem = currentItems.removeAt(index)
currentItem.status = status
currentItems.add(index, currentItem)
break
}
}
itemModels.value = currentItems
......................................
}
我这样做了,
private fun updateItem(item: ItemModel, status: Status) {
...............
if (el.id == item.id) {
currentItems.removeAt(index)
val currentItem = ItemModel(item.id, "${item.title}, ${status.progress}")
currentItem.status = status
currentItems.add(index, currentItem)
break
}
...........
}
关于android - Recyclerview ListAdapter DiffUtil 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54217587/
我正在实现 AdapterView产生一个AbsListView -like 类,我可以使用 CursorAdapter在布局中。我正在实现这个,因为我想使用方便的自动数据更新行为 CursorAda
Try to focus on general "dos and donts" regarding lists, e.g. is adding and removing 30 items of a 2
我有一个listadapter,其中有很多listitems,每个项目都包含一个textview 和一个imageview。我想从后端下载图片( 以 Base64 编码 ),所以 Picasso 和通
我对 Android 还很陌生,目前在使用自定义列表适配器时遇到一些问题。 基本上,我想制作一个问题列表,每个问题都有不同的类型(单选、复选框、文本、数字)。当 ListView 生成时,它显示得很好
我需要在另一个线程中加载自定义 ListAdapter 吗?我之前已经这样做过,但我也在同一个线程中下载了数据。 使用或不使用线程加载 ListAdapter 有何优缺点?加载线程是否需要大量资源?
我刚刚开始开发 Android,但遇到了问题。我有一个 DrawerNavigationListAdapter 和一个 DrawerNavigationListView,并且工作正常,现在我尝试向其中
我有一个 ListAdapter,用于在 Listview 中显示列表。现在我添加了一个长按菜单操作来删除任何选定的项目。 public boolean onContextItemSelected(M
加载列表适配器的最佳方式是什么。简单的问题,但我已经知道你们中的大多数人会说什么。你会说使用 LoaderManager/CursorManager。每个人都会告诉我它使用起来多么容易,而且它不能在
我已经有一个使用 SQLite 数据库和列表适配器的应用程序。我正在尝试更新我的应用程序以使用全文搜索功能,但正在努力寻找问题的答案。基本上,当我使用必要的 _id 列创建虚拟表时,数据库会将其转换为
我使用这个例子中的 ListAdapter: http://code.google.com/p/au-optimizing-layouts-201/source/browse/au_optimizin
在 ListAdapter 中,如何将数据添加到现有列表?submitList()只会用新列表替换现有列表, 也许一种更新数据的方法是这样的 adapter.addNewItem(list) 最佳答案
我在 RecyclerView 中遇到了分页无限滚动的问题,我正在使用 .addAll() 添加所有新项目 movieList.addAll(it.movieList) adapter.submi
我正在使用 Android Room和 Rx Flowable更新 RecyclerView将对象从我的数据库中。为了获得更好的动画和性能,我更新了项目以使用新的 ListAdapter在我的项目中。
我正在尝试将我的 Adapter 转换为 ListAdapter,以便将来包含 Paging 库。我的应用程序是一个基本的 2 fragment 笔记应用程序,第一个 fragment 具有回收器 V
我正在设置一个 RecyclerView,它使用 ListAdapter 来计算更改时的动画。 RecyclerView 通过 ViewModel 接收数据,ViewModel 通过 Firebase
我正在尝试使用 JSON 数据源中的项目列表填充 View 。我似乎无法让适配器在我的列表中显示 JSON 数据。这就是我到目前为止所做的。 MainActivity.java package com
我看了http://developer.android.com/reference/android/widget/ListAdapter.html并看到 ListAdapter 是一个公共(publi
我正在尝试获取 HashMap 的 ArrayList,以尝试获取可用的 ListAdapter。当我运行这个时,我得到一个 java.lang.RuntimeException: Your cont
好吧,所以我创建了一个 ListView 和我自己的 ListAdapter,但是每当我将结果添加到 ListAdapter 时,它都会崩溃,并显示 NullPointerException 当 fi
我有一个包含多个字段的类,我只想在 ListView 中显示其中一个字段 class FOO { String f1; String f2; String f3; }; FOO fo
我是一名优秀的程序员,十分优秀!