带有 DiffUtil (ListAdapter) 的 RecyclerView 会阻塞 UI 线程
Posted
技术标签:
【中文标题】带有 DiffUtil (ListAdapter) 的 RecyclerView 会阻塞 UI 线程【英文标题】:RecyclerView with DiffUtil (ListAdapter) blocks UI thread 【发布时间】:2019-08-11 11:34:02 【问题描述】:我正在使用 RecyclerView
和 ListAdapter(当列表被替换时,它使用 AsyncListDiffer 来计算和动画更改)。
问题是,有时当我调用submit()
用新数据更新 RecyclerView 时,它会阻塞整个 UI 线程 5-10 秒。这发生在我恢复活动(从主屏幕返回)之后,但我不确定这是否是原因。
我确定了
提交的列表具有良好的预期数据 使用recyclerView.setHasFixedSize(true);
,因为它占用了所有屏幕空间
这是设备处于无响应状态时的线程转储 (see raw):
"main@10549" prio=5 runnable
java.lang.Thread.State: RUNNABLE
at java.util.ArrayList.get(ArrayList.java:315)
at android.support.v7.widget.OpReorderer.getLastMoveOutOfOrder(OpReorderer.java:215)
at android.support.v7.widget.OpReorderer.reorderOps(OpReorderer.java:33)
at android.support.v7.widget.AdapterHelper.preProcess(AdapterHelper.java:93)
at android.support.v7.widget.RecyclerView.processAdapterUpdatesAndSetAnimationFlags(RecyclerView.java:3583)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:3829)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3639)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:4194)
at android.view.View.layout(View.java:16967)
at android.view.ViewGroup.layout(ViewGroup.java:5584)
at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:625)
at android.view.View.layout(View.java:16967)
at android.view.ViewGroup.layout(ViewGroup.java:5584)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:2001)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1844)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1753)
at android.view.View.layout(View.java:16967)
at android.view.ViewGroup.layout(ViewGroup.java:5584)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:396)
at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
at android.view.View.layout(View.java:16967)
at android.view.ViewGroup.layout(ViewGroup.java:5584)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1189)
at android.view.View.layout(View.java:16967)
at android.view.ViewGroup.layout(ViewGroup.java:5584)
at android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:142)
at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:41)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1556)
at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:888)
at android.view.View.layout(View.java:16967)
at android.view.ViewGroup.layout(ViewGroup.java:5584)
at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1231)
at android.view.View.layout(View.java:16967)
at android.view.ViewGroup.layout(ViewGroup.java:5584)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:396)
at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
at android.view.View.layout(View.java:16967)
at android.view.ViewGroup.layout(ViewGroup.java:5584)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:396)
at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
at android.view.View.layout(View.java:16967)
at android.view.ViewGroup.layout(ViewGroup.java:5584)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:396)
at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
at android.view.View.layout(View.java:16967)
at android.view.ViewGroup.layout(ViewGroup.java:5584)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:2001)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1844)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1753)
at android.view.View.layout(View.java:16967)
at android.view.ViewGroup.layout(ViewGroup.java:5584)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:396)
at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2769)
at android.view.View.layout(View.java:16967)
at android.view.ViewGroup.layout(ViewGroup.java:5584)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2547)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2250)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1321)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6732)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:894)
at android.view.Choreographer.doCallbacks(Choreographer.java:696)
at android.view.Choreographer.doFrame(Choreographer.java:631)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:880)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5740)
at java.lang.reflect.Method.invoke(Method.java:-1)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:766)
这是我的 DiffUtil 回调:
private static final DiffUtil.ItemCallback<CoinTicker> DIFF_CALLBACK = new DiffUtil.ItemCallback<CoinTicker>()
@Override
public boolean areItemsTheSame(
@NonNull CoinTicker oldTicker, @NonNull CoinTicker newTicker)
// User properties may have changed if reloaded from the DB, but ID is fixed
return oldTicker.getSlug().equals(newTicker.getSlug()); // String
@Override
public boolean areContentsTheSame(
@NonNull CoinTicker oldTicker, @NonNull CoinTicker newTicker)
// NOTE: if you use equals, your object must properly override Object#equals()
// Incorrectly returning false here will result in too many animations.
return oldTicker.equals(newTicker); // see below
@Override
public Object getChangePayload(@NonNull CoinTicker oldTicker, @NonNull CoinTicker newTicker)
return newTicker.getComparisonPrice() - oldTicker.getComparisonPrice();
;
// equals for CoinTicker:
@Override
public boolean equals(Object o)
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CoinTicker that = (CoinTicker) o;
return Double.compare(that.price, price) == 0 && slug.equals(that.slug);
这里有什么问题?
【问题讨论】:
你又来了,哈哈! 是的,这是我遇到的最后一个单独的问题 :) 现在争取一天 :( 我没有看到上面的代码有任何问题,你能提供你的周期的快照吗? 您是在再次调用 API,还是在设置为适配器之前需要执行许多操作的巨大列表。 @IbrahimAli 你的意思是this? 【参考方案1】:很高兴地说“修复”很容易:
recyclerView.setItemAnimator(null);
我不关心动画。我只是想在屏幕上显示的项目上显示彩色 Flash 动画,我在 onBindViewHolder() 中执行此操作,它运行得非常快,没有任何延迟!
【讨论】:
【参考方案2】:比较两个列表是一项昂贵的操作。如果您正在处理大量数据,则可能会使用大量资源。因此,在后台线程中运行您的 DiffUtils 操作或使用在后台线程上运行的 AsyncListDiffer。
【讨论】:
我使用的是ListAdapter
,它使用的是AsyncListDiffer
。
@Bolling 不,如果你自己使用它不是背景以上是关于带有 DiffUtil (ListAdapter) 的 RecyclerView 会阻塞 UI 线程的主要内容,如果未能解决你的问题,请参考以下文章
DiffUtil 重绘 ListAdapter Kotlin 中的所有项目
DiffUtil 不刷新观察者调用android kotlin中的视图
DiffUtil.ItemCallback - 定义为伴随对象还是类?
在 Recyclerview 中使用 getItemCount 和 ListAdapter