我使用 android.arch.paging.PagedListAdapter <Item, RecyclerView.ViewHolder>
和 数据库作为 RecyclerView
中显示项目的数据源.
每个项目都有日期。我还在 override fun onCurrentListChanged(currentList: PagedList<Item>?).
中按此日期对元素进行分组
分组通过分成两个具有假位置的列表(项目和日期)来进行。总计数是项目计数和日期计数的总和。
我的问题是 IndexOutOfBoundsException: Inconsistency detected
来自 RecyclerView
刷新或加载下一个项目时。
可能的解决方案: 1 - 拒绝分开元素(不适合我的任务)。 2 - 使用 notifyDataSetChanged()
提交项目后(但消失动画并出现闪烁更新viewHolders)。
任何人都可以帮忙吗?
private var fakePositionDates: Map<Int, Date> = emptyMap()
private var fakePositionTaskPositions: Map<Int, Int> = emptyMap()
override fun getItemCount(): Int {
return fakePositionTaskPositions.size + fakePositionDates.size
}
override fun submitList(pagedList: PagedList<Task>?) {
super.submitList(pagedList)
}
override fun onCurrentListChanged(currentList: PagedList<Task>?) {
val groupTasksPositions = currentList?.asIterable()
?.mapIndexed { index, task ->
Pair(index, task.publishDate)
}
?.groupBy { it.second }
?.mapValues {
it.value.map { it.first }
}
?.toSortedMap(Comparator { o1, o2 ->
o2.compareTo(o1)
})
?: sortedMapOf()
val fakePositionDates = mutableMapOf<Int, Date>()
val fakePositionTaskPositions = mutableMapOf<Int, Int>()
var fakePosition = 0
groupTasksPositions.forEach { date, realPositions ->
fakePositionDates[fakePosition] = date
fakePosition += 1
realPositions.forEach {
fakePositionTaskPositions[fakePosition] = it
fakePosition += 1
}
}
this.fakePositionTaskPositions = fakePositionTaskPositions
this.fakePositionDates = fakePositionDates
}
最佳答案
尝试添加adapter.submitList(null)
和 adapter.notifyDataSetChanged
在您调用 View 模型函数以刷新您的 recyclerview 之前,在您的 Activity 或 fragment 中
关于android - RecyclerView : Inconsistency detected when update grouped items by PagedListAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51766321/