1

我们有一个可以根据推送通知进行更新的应用程序。我们发现,有时使用动画和 DiffUtil 的 RecyclerViews,动画会使应用程序崩溃。似乎在内部,recyclerview 正在为视图设置动画,而 DiffUtil 正在对它们进行操作,从而导致此异常:

 java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 0(offset:-1).state:12

我可以通过构建一个使用 diffutil 并快速随机化值(每秒 10 次更改)的 recyclerview 来使这种重现一致。它通常需要几秒钟,因此并不一致,但最终会崩溃。禁用动画修复它。

在 DefaultItemAnimator.runPendingAnimations() 完成之前触发 diffutil 似乎会发生这种情况。每次运行时,我都会将列表的副本传递给 diffutil。

这是我们的 diffutil:

class BindableDataDiffCallback(private val results: MutableList<DataClass>,
                           private val newResults: ArrayList<DataClass>) : DiffUtil.Callback() {
override fun getOldListSize() = results.size
override fun getNewListSize() = newResults.size

override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int) =
        results[oldItemPosition].getId() == newResults[newItemPosition].getId()
override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int) =
        results[oldItemPosition].hashCode == newResults[newItemPosition].hashCode

}

这是完整的堆栈跟踪:

java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 0(offset:-1).state:12 androidx.recyclerview.widget.RecyclerView{8307495 VFED..... .F....ID 0,114-1080,1545 #7f0a0a5c app:id/staggeredGridView2}, androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5923)
    at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
    at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
    at androidx.recyclerview.widget.LayoutState.next(LayoutState.java:100)
    at androidx.recyclerview.widget.StaggeredGridLayoutManager.fill(StaggeredGridLayoutManager.java:1609)
    at androidx.recyclerview.widget.StaggeredGridLayoutManager.scrollBy(StaggeredGridLayoutManager.java:2182)
    at androidx.recyclerview.widget.StaggeredGridLayoutManager.fixEndGap(StaggeredGridLayoutManager.java:1420)
    at androidx.recyclerview.widget.StaggeredGridLayoutManager.onLayoutChildren(StaggeredGridLayoutManager.java:698)
    at androidx.recyclerview.widget.StaggeredGridLayoutManager.onLayoutChildren(StaggeredGridLayoutManager.java:605)
    at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:3875)
    at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3639)
    at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4194)
    at android.view.View.layout(View.java:19590)
    at android.view.ViewGroup.layout(ViewGroup.java:6053)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
    at android.view.View.layout(View.java:19590)
    at android.view.ViewGroup.layout(ViewGroup.java:6053)
    at androidx.viewpager.widget.ViewPager.onLayout(ViewPager.java:1775)
    at android.view.View.layout(View.java:19590)
    at android.view.ViewGroup.layout(ViewGroup.java:6053)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
    at android.view.View.layout(View.java:19590)
    at android.view.ViewGroup.layout(ViewGroup.java:6053)
    at com.google.android.material.appbar.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:142)
    at com.google.android.material.appbar.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:41)
    at com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1556)
    at androidx.coordinatorlayout.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:888)
    at android.view.View.layout(View.java:19590)
    at android.view.ViewGroup.layout(ViewGroup.java:6053)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
    at android.view.View.layout(View.java:19590)
    at android.view.ViewGroup.layout(ViewGroup.java:6053)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
    at android.view.View.layout(View.java:19590)
    at android.view.ViewGroup.layout(ViewGroup.java:6053)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
    at android.view.View.layout(View.java:19590)
    at android.view.ViewGroup.layout(ViewGroup.java:6053)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
    at android.view.View.layout(View.java:19590)
    at android.view.ViewGroup.layout(ViewGroup.java:6053)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
    at com.android.internal.policy.DecorView.onLayout(DecorView.java:758)
    at android.view.View.layout(View.java:19590)
    at android.view.ViewGroup.layout(ViewGroup.java:6053)
    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2484)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2200)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1386)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6733)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
    at android.view.Choreographer.doCallbacks(Choreographer.java:723)
    at android.view.Choreographer.doFrame(Choreographer.java:658)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
    at android.os.Handler.handleCallback(Handler.java:789)
    at android.os.Handler.dispatchMessage(Handler.java:98)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6541)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

有没有人有这方面的运气?

4

3 回答 3

1

该问题可能与适配器有关。如果您setHasStableIds(true);在适配器构造函数中使用,则需要确保您使用的是稳定的 ids

确保getItemId在适配器中正确覆盖。它应该是:

@Override
public long getItemId(int position) {
   return yourList == null ? 0 : yourList.get(position).getId();
}

而不是

@Override
public long getItemId(int position) {
     return position;
}
于 2019-12-05T08:04:58.207 回答
0

确保在适配器的更新方法中,您使用每个参数的正确列表计算差异。

您尚未共享您的适配器,因此我无法确认这一点但是,如果您的新列表小于旧列表(删除了一个项目)但您的比较是倒退的,那么您的错误是 100% 可重现的。

你可能一直在做什么:

fun updateList(newData: List<DataClass>) {
    val diffResult = DiffUtil.calculateDiff(BindableDataDiffCallback(newData, oldData))
    diffResult.dispatchUpdatesTo(this)
}

你应该做什么:

fun updateList(newData: List<DataClass>) {
    val diffResult = DiffUtil.calculateDiff(BindableDataDiffCallback(oldData, newData))
    diffResult.dispatchUpdatesTo(this)
}
于 2021-05-25T20:52:25.950 回答
0

如果它与某些线程问题有关(根据您的描述可能是),您可以查看AsyncListDiffer而不是DiffUtil. 前者在后台线程上使用后者,并通知 ui。用法相当简单:AsyncListDiffer

于 2020-04-12T16:04:26.920 回答