美文网首页
【RecyclerView 自带Bug】java.lang.In

【RecyclerView 自带Bug】java.lang.In

作者: 心安1989 | 来源:发表于2018-09-18 10:16 被阅读0次

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。

相关文章

网友评论

      本文标题:【RecyclerView 自带Bug】java.lang.In

      本文链接:https://www.haomeiwen.com/subject/onyynftx.html