这是 RecyclerView 的 bug (参考: stackoverflow),目前最省事的办法就是继承 LayoutManager 然后覆写捕获异常,如 LinearLayoutManager (这里取名 BaseLinearLayoutManager):
public class BaseLinearLayoutManager extends LinearLayoutManager { public BaseLinearLayoutManager(Context context) { super(context); } public BaseLinearLayoutManager(Context context, int orientation, boolean reverseLayout) { super(context, orientation, reverseLayout); } public BaseLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } @Override public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) { try { super.onLayoutChildren(recycler, state); } catch (IndexOutOfBoundsException ignored) { } } }
然后原来使用原生 LinearLayoutManager 的地方换成 BaseLinearLayoutManager 就可以了。