使用 RecyclerView 有时莫名其妙的就报数组越界异常了
1、重写 LinearLayoutManager
public class WrapContentLinearLayoutManager extends LinearLayoutManager { public WrapContentLinearLayoutManager(Context context) { super(context); } public WrapContentLinearLayoutManager(Context context, int orientation, boolean reverseLayout) { super(context, orientation, reverseLayout); } public WrapContentLinearLayoutManager(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 e) { e.printStackTrace(); } } }
2、设置 LayoutManager
// mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); // 解决RecyclerView可能出现的holder数组越界Bug mRecyclerView.setLayoutManager( new WrapContentLinearLayoutManager(this,LinearLayoutManager.VERTICAL, false));
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于