【RecyclerView 自带Bug】java.lang.In
2018-09-18 本文已影响0人
心安1989
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。