【发布时间】:2017-10-17 17:42:41
【问题描述】:
我正在使用 SortedList 对某些参数的数据进行排序,比如 RecyclerView 中的“日期”,以便在加载大量数据时提高性能。但我观察到它的非常奇怪的行为。首先让我放一些代码-
mMatchesList = new SortedList<>(Matches.class, new SortedList.Callback<Matches>() {
@Override
public int compare(Matches o1, Matches o2) {
int result = 0;
try {
result = DateTimeOperationUtil.dateComparator("dd-MMM-yyyy", o2.MatchDate, o1.MatchDate);
} catch (ParseException e) {
e.printStackTrace();
}
return result;
}
@Override
public void onChanged(int position, int count) {
notifyItemRangeChanged(position, count);
}
@Override
public boolean areContentsTheSame(Matches oldItem, Matches newItem) {
return oldItem.equals(newItem);
}
@Override
public boolean areItemsTheSame(Matches item1, Matches item2) {
return item1.hashCode() == item2.hashCode();
}
@Override
public void onInserted(int position, int count) {
notifyItemRangeInserted(position, count);
}
@Override
public void onRemoved(int position, int count) {
notifyItemRangeRemoved(position, count);
}
@Override
public void onMoved(int fromPosition, int toPosition) {
notifyItemMoved(fromPosition, toPosition);
}
});
在这里,我有一些匹配项
我想按日期降序排序
。第一次,订单和我想要的一样,但是
当我更改任何匹配的日期时,应该根据 到定义的顺序。但它没有发生,数据也没有改变 这份名单。
已编辑 我正在使用以下代码更新我的列表:
if (mMatchesList.indexOf(matches) != -1)
mMatchesList.updateItemAt(mMatchesList.indexOf(matches), matches);
我的 Matches 模型类实现了 equals() 和 hashcode()。
告诉我我做错了什么。
【问题讨论】:
-
我对SortedList这个类不太熟悉,但是你有没有调用updateItemAt(int index, T item)?
-
@Achez9 是的,我正在调用 updateItemAt(int index, int item) 方法..
-
您能否发布您如何更新您的对象,以及您如何将该更新分派到 SortedList?
标签: android performance android-recyclerview sortedlist