事情是这样的
我在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,也许是自己的调用不和规范呢?