android recyclerview inconsistency detected invalid recycler viewholder position

I had the same issue and i searched all over the internet and tried alot of the solutions. Finally my issue was fixed by setting ItemAnimator to null for RecyclerView.

recyclerView.setItemAnimator(null);

This will fix the issue


In onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) check,

if(position != RecyclerView.NO_POSITION){
   // Do your binding here
}

This worked for me.


Since you're actually replacing all data, calling notifyItemRangeInserted() without actually inserting anything into the adapter raises this issue. You have to call the correct notifier just after setting the actual data to the adapter or to be more precise underlying list the list.

notifyItemRangeInserted(pos, count) notifies the adapter to expect count items inserted at pos. Therefore the size of the list should have grown by count, not just be count. Since you replace all data, you should call notifyDataSetChanged() instead or use DiffUtil to calculate the actual diff of your data. But then you don't have to worry about which notification to use anymore.

listAdapter.notifyDataSetChanged();