您的位置:首页 > 其它

recycleView 遇到IndexOutOfBoundsException 及解决

2016-04-17 16:55 274 查看
事情是这样的

我在activity 中继承的MVPView,通过presenter加载输入,然后调用MVPview中的下面这个方法"

@Override
public void setLocationData(List<Location> locationData) {
locationList.clear();
locationList.addAll(locationData);
chooseLocationAdapter.notifyItemRangeInserted(0, locationData.size()+1);
//        chooseLocationAdapter.notifyDataSetChanged();
}


结果报了下面这个错误

java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder


排除了List的数组越界问题后,我想到了可能是recycleView的一个BUG.

果然,在Google一番之后发现确实有这个问题

1
down vote
It is a bug of RV, see the discussion here.
In most cases, use notifyDataSetChanged() will avoid this crash, but it will kill Animation and Performance.


按照上面这个回答,我改成了

chooseLocationAdapter.notifyDataSetChanged();


果然问题解决了.不过又转念一想,既然这这个notify的问题,或许,这样试试?

<span style="white-space:pre">	</span>locationList.clear();
chooseLocationAdapter.notifyDataSetChanged();
locationList.addAll(locationData);
chooseLocationAdapter.notifyItemRangeInserted(0, locationData.size()+1);
是的,清除数据后notify一次,加载数据在notify一次,问题解决了!

不要老是想着是个BUG,也许是自己的调用不和规范呢?
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: