我有一个 SortedList
由我的 RecyclerView.Adapter
在 RecyclerView
中显示。
我使用来自 SortedListAdapterCallback.compare()
方法的 2 个自定义 Comparator
实例对 A-Z 或 Z-A 进行排序。
static class A2Z implements Comparator<Item> {
@Override
public int compare(Item t0, Item t1) {
return t0.mText.compareTo(t1.mText);
}
}
static class Z2A extends A2Z {
@Override
public int compare(Item t0, Item t1) {
return -1 * super.compare(t0, t1);
}
}
Item
只包含一个 String mText;
我在 SortedListAdapterCallback.compare()
方法中使用我的比较器:
private Comparator<Item> a2z = new A2Z();
private Comparator<Item> z2a = new Z2A();
private Comparator<Item> comparator = z2a;
@Override
public int compare(Item t0, Item t1) {
return comparator.compare(t0, t1);
}
我在按下按钮时更改比较器。屏幕上的列表不会更新。
在各种方法中记录值后,我可以看出列表本身没有更新。通知适配器更改只是重绘旧列表,而不是求助于它。
那么我如何强制底层 SortedList
求助所有项目?
也许最好每次都创建一个新的Adapter
,就像这个问题一样:
最佳答案
SortedList
没有自行求助的功能 - 每个实例只有一个排序顺序。
根据 Yigit's answer to the above referenced question 为每个度假村创建一个新的适配器:
If you have stable ids in your adapter, you can get pretty good results (animations) if you create a new array containing the filtered items and call
recyclerView.swapAdapter(newAdapter, false);
Using swapAdapter hints RecyclerView that it can re-use view holders. (vs in setAdapter, it has to recycle all views and re-create because it does not know that the new adapter has the same ViewHolder set with the old adapter).
关于android - 强制使用 Android appcompat `SortedList` ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32437235/