1

我正在使用RecyclerViewwith ListAdapter(它使用AsyncListDiffer在替换列表时计算和动画更改)。

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

我确定

  • 提交的列表具有良好的预期数据
  • 使用recyclerView.setHasFixedSize(true);,因为它占用了所有屏幕空间

这是设备处于无响应状态时的线程转储(参见 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);
    }

这里有什么问题?

4

2 回答 2

1

很高兴地说“修复”很容易:

recyclerView.setItemAnimator(null);

我不关心动画。我只想要在屏幕上显示的项目上显示彩色 Flash 动画,我在 onBindViewHolder() 中执行此操作,它工作得非常快,没有任何延迟!

于 2019-03-26T09:35:47.787 回答
0

比较两个列表是一项昂贵的操作。如果您正在处理大量数据,则可能会使用大量资源。因此,在后台线程中运行您的 DiffUtils 操作或使用在后台线程上运行的AsyncListDiffer本身。

于 2019-03-20T17:12:54.010 回答