带有DiffUtil的RecyclerView(ListAdapter)阻止UI线程

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了带有DiffUtil的RecyclerView(ListAdapter)阻止UI线程相关的知识,希望对你有一定的参考价值。

我正在使用qazxsw poi和qazxsw poi(使用qazxsw poi来计算并替换列表时的动画更改)。

问题是,有时当我调用RecyclerView用新数据更新RecyclerView时,它会阻止整个UI线程5到10秒。这是在我恢复活动(从主屏幕返回)后发生的,但我不确定这是否是原因。

我确定了

  • 提交的列表具有良好的预期数据
  • 使用ListAdapter,因为它占用了所有的屏幕空间

当设备处于无响应状态(AsyncListDiffer)时,这是线程转储:

submit()

这是我的DiffUtil回调:

recyclerView.setHasFixedSize(true);

这可能有什么问题?

答案

比较两个列表是昂贵的操作。如果您处理大量数据,它可以使用大量资源。因此,在后台线程中运行您的DiffUtils操作或使用在后台线程上运行的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)

我不关心动画。我只想在屏幕上显示的项目上使用彩色flash动画,我在onBindViewHolder()中进行操作,它的工作速度非常快,没有任何延迟!

另一答案

在这个部分

    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);
    }

缺少(oldTicker.getSomething()。equals(newTicker.getSomething())或尝试返回oldTicker.getSomething()== newTickers.getSomething()

如果comparitionis字符串使用“equals”如果是整数或双重使用“==”

以上是关于带有DiffUtil的RecyclerView(ListAdapter)阻止UI线程的主要内容,如果未能解决你的问题,请参考以下文章

Paging 3 - 如何在 PagingDataAdapter 完成刷新并且 DiffUtil 完成差异后滚动到 RecyclerView 的顶部?

RecyclerView — DiffUtil

Recyclerview DiffUtil 项目更新

Android 高性能列表:RecyclerView + DiffUtil

Android 高性能列表:RecyclerView + DiffUtil

如何使用 DiffUtil 更新 RecyclerView 适配器