如何清除 (RecyclerView) SortedList?

How do I clear a (RecyclerView) SortedList?

我有一个 SortedList,我正在使用它来表示我的回收站视图数据,但我正在努力 "clear" API 调用之间的数据。有什么帮助吗?我目前只是像这样遍历列表:

for(int i = 0; i < mList.size(); i++){
   removeItemAt(i);
}

这似乎不一致地删除了一些项目?

提前致谢! :)

如果您查看 source code,您所做的问题是 SortedList 在调用 removeItemAt 索引时更改了大小。因此 mList.size() 将在您的 for 循环迭代时发生变化,从而导致不一致的结果。

这是一种从回收站视图中删除项目的方法 SortedList

public void clear() {
     mList.beginBatchedUpdates();
     //remove items at index 0 so the remove callback will be batched
     while (mList.size() > 0) {
         mList.remove(mList.get(0));
     }
     mList.endBatchedUpdates();
}

新版本的支持库有一个SortedList#clear()方法可以使用。