记录一次RecyclerView删除数据后更新数据报IndexO

2017-11-21  本文已影响16人  jy006

这好像是第一次在这写文章,以此记录一些BUG和学习心得。最近在使用RecyclerView过程中出现了一个问题,没错就是我们也常见的数组越界,如图:

删除数据报数组越界

据查阅了相关资料发现这是Recycleview自身的一个BUG,通过重写LinearLayoutManager可以将这个错误抛出,让程序正常运行。如下

public class WrapContentLinearLayoutManager extends LinearLayoutManager {

public WrapContentLinearLayoutManager(Context context) {

        super(context);

}

publicWrapContentLinearLayoutManager(Context context,intorientation,booleanreverseLayout) {

           super(context, orientation, reverseLayout);

}

publicWrapContentLinearLayoutManager(Context context, AttributeSet attrs,intdefStyleAttr,intdefStyleRes) {

        super(context, attrs, defStyleAttr, defStyleRes);

}

@Override

public voidonLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {

try{

        super.onLayoutChildren(recycler, state);

}catch(IndexOutOfBoundsException e) {

        e.printStackTrace();

        }

    }

}

这样程序就不会报错了

上一篇 下一篇

猜你喜欢

热点阅读