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
.
Previous answer
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.