DiffResult调度导致"检测到不一致".有时,视图持有者适配器positionViewHolder错误无效

Ole*_*iov 5 android adapter android-viewholder android-recyclerview

我有一个RxJava2 Observable,它接受两个列表,为它们计算diff结果并将此数据发送到适配器.主线程上的适配器分派更新.

适配器中的调度代码:

 public void dispatchStreams(List<StreamV3> streams, @Nullable DiffUtil.DiffResult diffResult) {

    if (streams == null) return;

    streamsList.clear();
    streamsList.addAll(streams);

    if (diffResult != null) {
        diffResult.dispatchUpdatesTo(this);
    }
}
Run Code Online (Sandbox Code Playgroud)

我检测到了"不一致".某些设备上的某些时候视图持有者适配器positionViewHolder错误无效.我无法弄清楚我的代码有什么问题.Min SDK 21,Target SDK 26,RecyclerView版本为26.0.0.我知道扩展LinearLayoutManager并默默地捕获此错误的解决方法,但这是一个糟糕的解决方案,我相信这里应该更好.

有人可以提供任何帮助吗?

Ole*_*iov 10

我在这个答案中找到了解决这个问题的方法

似乎问题是由布局管理器上的supportsPredictiveItemAnimations属性引起的.当我将其设置为false时,不再发生崩溃.

public class LinearLayoutManagerWrapper extends LinearLayoutManager {

 public LinearLayoutManagerWrapper(Context context) {
   super(context);
 } 

 public LinearLayoutManagerWrapper(Context context, int orientation, boolean reverseLayout) {
   super(context, orientation, reverseLayout);
 }

 public LinearLayoutManagerWrapper(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
   super(context, attrs, defStyleAttr, defStyleRes);
 }

 @Override
 public boolean supportsPredictiveItemAnimations() {
   return false;
 }
}
Run Code Online (Sandbox Code Playgroud)