Android Cannot call this method while RecyclerView is computing a layout or scrolling

在使用RecyclerView时出现问题:

1
java.lang.IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling

出错原因:非法状态异常,即当RecyclerView在计算布局,或者在滑动的时候,不能调用notifyDataSetChanged

解决方法:在RecyclerView滑动或者计算layout的时候不刷新数据

1
2
3
if (mRecyclerView.getScrollState() == RecyclerView.SCROLL_STATE_IDLE && (!mRecyclerView.isComputingLayout())) {
mAdapter.notifyDataSetChanged();
}