【问题标题】:RecyclerView: Inconsistency detected when update grouped items by PagedListAdapterRecyclerView:通过 PagedListAdapter 更新分组项目时检测到不一致
【发布时间】:2019-01-16 21:04:05
【问题描述】:

我使用android.arch.paging.PagedListAdapter <Item, RecyclerView.ViewHolder>Room DB 作为RecyclerView 中显示项目的数据源。

每个项目都有日期。我还在override fun onCurrentListChanged(currentList: PagedList<Item>?). 中按此日期对元素进行分组 分组通过分成两个具有假位置的列表(项目和日期)来进行。总计数是项目计数和日期计数的总和。

我的问题是刷新或加载下一项时来自RecyclerViewIndexOutOfBoundsException: Inconsistency detected

可能的解决方案: 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
}

【问题讨论】:

  • 请在此处提供代码。当您在另一个线程上使用 recyclerview 或未正确使用 notifydatasetchanged() 时,不一致检测到此错误
  • 添加了我的代码。 SubmitList 从 mainThread 执行

标签: android android-recyclerview indexoutofboundsexception


【解决方案1】:

在调用视图模型函数刷新回收视图之前,尝试在活动或片段中添加adapter.submitList(null)adapter.notifyDataSetChanged

【讨论】:

    猜你喜欢
    • 2016-12-10
    • 2017-09-30
    • 1970-01-01
    • 2020-01-23
    • 2018-12-04
    • 1970-01-01
    • 2015-01-05
    • 2016-07-15
    相关资源
    最近更新 更多