Inconsistency Detected in Recyclerview, How to Change Contents of Recyclerview While Scrolling

Inconsistency detected in RecyclerView, How to change contents of RecyclerView while scrolling

Edit: The bug is fixed now, if you're still getting the same Exception, please make sure you're updating your Adapter data source only from the main thread and calling appropriate adapter notify method after it.

Old answer: It seems to be a bug in RecyclerView, it's reported here and here. Hopefully it will be fixed in the next release.

IndexOutOfBoundsException: Inconsistency detected error while scrolling

You pass your messageList to the adapter, so the messageList in the adapter and in the activity are the same object references. Then, as I understand, somehow method fetchMessageList is called, it's where the problem appears. You call .clear() on your list and start an asynchronous operation to fetch a new list, to then synchronously post it to your adapter.

The thing is, after you have cleared your list, your adapter keeps the reference to an empty list now, without being notified about the changes. The adapter still "thinks" that your list is the same size as it was before, so when you scroll the RecyclerView, it tries to call at least onBindViewHolder for new appearing items. But, as the list is empty, it throws IndexOutOfBoundsException.

You could try to notify the adapter about the changes immediately after calling messageList.clear(), but it seems to me that just deleting this clearing will solve the problem.

private void fetchMessageList(){
//messageList.clear(); <- delete this
AndroidNetworking.get(Util.url)
...
}

Inconsistency detected. Invalid item position 0(offset:-1)... RecyclerView crashes, on deleting first item

The issue was with the Adapter. I was using setHasStableIds(true); in Adapter Constructor, to avoid "blinking of items on change". I had overrided the 2 methods:
public long getItemId(int position) {...} and public int getItemViewType(int position) {...}

The crash disappeared and the 0th item was removed, once I deleted the line: setHasStableIds(true); in the constructor. But the items were blinking.
The issue was with what I was returning in the overridden methods. Below code had a problem:

    @Override
public long getItemId(int position) {
return position;
}

@Override
public int getItemViewType(int position) {
return position;
}

The position I was returning wasn't unique. I returned the id of the item as a unique ID, and the problem disappeared.

Correct code:

    @Override
public long getItemId(int position) {
return mAlarmEntityList.get(position).getAlarmId();
}

@Override
public int getItemViewType(int position) {
return position;
}

I spent hours figuring out what was wrong. These answers helped me: Inconsistency detected in RecyclerView, How to change contents of RecyclerView while scrolling

How to prevent RecyclerView item from blinking after notifyItemChanged(pos)?

Hope this helps someone!



Related Topics



Leave a reply



Submit