This problem is caused by RecyclerView
Data modified in different thread. The best way is checking all data access. And a workaround is wrapping LinearLayoutManager
.
There was actually a bug in RecyclerView and the support 23.1.1 still not fixed.
For a workaround, notice that backtrace stacks, if we can catch this Exception
in one of some class it may skip this crash. For me, I create a LinearLayoutManagerWrapper
and override the onLayoutChildren
:
public class WrapContentLinearLayoutManager extends LinearLayoutManager {
//... constructor
@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
try {
super.onLayoutChildren(recycler, state);
} catch (IndexOutOfBoundsException e) {
Log.e("TAG", "meet a IOOBE in RecyclerView");
}
}
}
Then set it to RecyclerView
:
RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new WrapContentLinearLayoutManager(activity, LinearLayoutManager.HORIZONTAL, false));
Actually catch this exception, and seems no any side-effect yet.
Also, if you use GridLayoutManager
or StaggeredGridLayoutManager
you must create a wrapper for it.
Notice: The RecyclerView
may be in a wrong internal state.
This is an example for refreshing data with completely new content. You can easily modify it to fit your needs. I solved this in my case by calling:
notifyItemRangeRemoved(0, previousContentSize);
before:
notifyItemRangeInserted(0, newContentSize);
This is the correct solution and is also mentioned in this post by an AOSP project member.
I faced this issue once, and I solved this by wrapping the LayoutManager
and disabling predictive animations.
Here an example:
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;
}
}
And set it to RecyclerView
:
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManagerWrapper(context, LinearLayoutManager.VERTICAL, false);
New answer: Use DiffUtil for all RecyclerView updates. This will help with both performance and the bug above. See Here
Previous answer:
This worked for me. The key is to not use notifyDataSetChanged()
and to do the right things in the correct order:
public void setItems(ArrayList<Article> newArticles) {
//get the current items
int currentSize = articles.size();
//remove the current items
articles.clear();
//add all the new items
articles.addAll(newArticles);
//tell the recycler view that all the old items are gone
notifyItemRangeRemoved(0, currentSize);
//tell the recycler view how many new items we added
notifyItemRangeInserted(0, newArticles.size());
}
Reasons caused this issue:
SOLUTION:
-----------------SOLUTION 1---------------
Create a custom LinearLayoutManager as the following and set it to the ReyclerView
public class CustomLinearLayoutManager extends LinearLayoutManager {
//Generate constructors
@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
try {
super.onLayoutChildren(recycler, state);
} catch (IndexOutOfBoundsException e) {
Log.e(TAG, "Inconsistency detected");
}
}
}
Then set RecyclerVIew Layout Manager as follow:
recyclerView.setLayoutManager(new CustomLinearLayoutManager(activity));
-----------------SOLUTION 2---------------
Again, create a custom Linear Layout Manager as follow:
public class CustomLinearLayoutManager extends LinearLayoutManager {
//Generate constructors
@Override
public boolean supportsPredictiveItemAnimations() {
return false;
}
}
Then set RecyclerVIew Layout Manager as follow:
recyclerView.setLayoutManager(new CustomLinearLayoutManager(activity));
-----------------SOLUTION 3---------------
-----------------SOLUTION 4---------------
I had a similar problem.
Problem in error code below:
int prevSize = messageListHistory.size();
// some insert
adapter.notifyItemRangeInserted(prevSize - 1, messageListHistory.size() -1);
Solution:
int prevSize = messageListHistory.size();
// some insert
adapter.notifyItemRangeInserted(prevSize, messageListHistory.size() -prevSize);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With