我对RecyclerView
的SortedList
有些问题不太理解。
假设我有一个非常简单的类,只有一个非常简单的类来保存数据:
public class Pojo {
public final int id;
public final char aChar;
public Pojo(int id, char aChar) {
this.id = id;
this.aChar = aChar;
}
@Override
public String toString() {
return "Pojo[" + "id=" + id
+ ",aChar=" + aChar
+ "]";
}
}
我理解的是排序后的列表不会包含任何重复项。
但是当我有一个带回调函数的 SortedList,像这样:
....
@Override
public boolean areContentsTheSame(Pojo oldItem, Pojo newItem) {
return oldItem.aChar == newItem.aChar;
}
@Override
public int compare(Pojo o1, Pojo o2) {
return Character.compare(o1.aChar, o2.aChar);
}
@Override
public boolean areItemsTheSame(Pojo item1, Pojo item2) {
return item1.id == item2.id;
}
当我添加具有相同ID但不同字符的多个项目时,最终会出现重复项。
sortedList.add(new Pojo(1, 'a'));
sortedList.add(new Pojo(1, 'b'));
我期望列表更新项,但现在即使areItemsTheSame
返回true
,我也有多个项。