- 【RecyclerView 自带Bug】java.lang.In
- RecyclerView内部报错,刷新崩溃RecyclerVie
- recyclerview bug : IndexOutOfBou
- 安卓Tips之RecycleView下拉刷新数据的一个bug
- 重走安卓进阶路——页面优化、组件优化
- 解决ViewPager2嵌套RecyclerView滑动冲突的问
- Android之RecyclerView的原生Bug-Incon
- 解决RecyclerView使用smoothScrollToPo
- RecyclerView Bug:IndexOutOfBoun
- RecyclerView Bug:IndexOutOfBound
RecyclerView 在数据源变化特别快的时候,会出现该Bug,导致app crash掉,比如频繁刷新,数据源clear ,然后又填充。解决方案如下:
自定义LinearLayoutManager,将问题try catch掉。
public class RecyclerViewNoBugLinearLayoutManager extends LinearLayoutManager {
public RecyclerViewNoBugLinearLayoutManager(Context context) {
super(context);
}
public RecyclerViewNoBugLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
public RecyclerViewNoBugLinearLayoutManager(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 (Exception e) {
e.printStackTrace();
}
}
@Override
public boolean supportsPredictiveItemAnimations() {
return false;
}
@Override
public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) {
try {
return super.scrollVerticallyBy(dy, recycler, state);
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
}
然后给RecycelerView设置我们自定义的 LinearLayoutManager,如下:
mRecyclerview.setLayoutManager(new RecyclerViewNoBugLinearLayoutManager(this));
这样就可以完美的解决RecyclerView 自带的Bug。
网友评论