我们有一个可以根据推送通知进行更新的应用。我们发现,有时使用动画和DiffUtil的RecyclerViews会使动画崩溃。在内部,当DiffUtil对视图进行操作时,recyclerview似乎在对视图进行动画处理,从而导致以下异常:
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 0(offset:-1).state:12
我可以通过构建一个使用diffutil的recyclerview并快速随机化值(每秒10次更改)来始终如一地进行复制。通常需要几秒钟的时间,因此不一致,但是最终会崩溃。禁用动画即可解决此问题。
似乎在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)
有人有运气吗?
答案 0 :(得分:0)
问题可能与适配器有关。如果在适配器构造函数中使用setHasStableIds(true);
,则需要确保使用stable ids。
确保在适配器中正确覆盖getItemId
。
应该是:
@Override
public long getItemId(int position) {
return yourList == null ? 0 : yourList.get(position).getId();
}
不是:
@Override
public long getItemId(int position) {
return position;
}
答案 1 :(得分:0)
如果任何可能与某个线程问题有关(根据您的描述),则可以查看AsyncListDiffer
而不是DiffUtil
。前者在后台线程上使用后者,并通知ui。用法很简单:AsyncListDiffer
答案 2 :(得分: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)
}