Search code examples
androidandroid-recyclerviewadapterandroid-viewholder

DiffResult dispatching lead to 'Inconsistency detected. Invalid view holder adapter positionViewHolder' error sometimes


I have an RxJava2 Observable that takes two lists, calculate diff result for them and send this data to adapter. Adapter dispatch updates on Main Thread.

Code of dispatching in adapter:

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

I've got 'Inconsistency detected. Invalid view holder adapter positionViewHolder' error sometime on some devices. And I can't figure out what's wrong with my code. Min SDK 21, Target SDK 26, RecyclerView version is 26.0.0. I know about workaround with extending LinearLayoutManager and silently catching this error but this is bad solution and I believe here should be better one.

Could anyone provide any help please?


Solution

  • I found a solution for this issue in this answer

    It seems that issue is caused by supportsPredictiveItemAnimations property on layout managers. When I set it to false no crash happens anymore.

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