强制使用 Android appcompat `SortedList`?
Force resort of Android appcompat `SortedList`?
我的 RecyclerView.Adapter
在 RecyclerView
中显示了一个 SortedList
。
我使用来自 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
,如这个问题:
- RecyclerView change data set
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).
在带有本地控制标志的比较方法中使用 switch 语句(枚举是个好主意)。
改变开关标志后,调用sortedList.replaceAll。
@Override
public int compare(PmpRole pmpRoleA, PmpRole pmpRoleB) {
switch (mSorter){
case IDX:
return pmpRoleA.getIdx().compareTo(pmpRoleB.getIdx());
case TITLE:
return pmpRoleA.getTitleIdx().compareTo(pmpRoleB.getTitleIdx());
case ID_IDX:
return pmpRoleA.getIdIdx().compareTo(pmpRoleB.getIdIdx());
}
return -1;
}
public void setSorter(Sorter sorter){
mSorter = sorter;
mPmpRoleSortedList.replaceAll(mPmpRoles);
}
维护动画功能等
我的 RecyclerView.Adapter
在 RecyclerView
中显示了一个 SortedList
。
我使用来自 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
,如这个问题:
- RecyclerView change data set
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).
在带有本地控制标志的比较方法中使用 switch 语句(枚举是个好主意)。
改变开关标志后,调用sortedList.replaceAll。
@Override
public int compare(PmpRole pmpRoleA, PmpRole pmpRoleB) {
switch (mSorter){
case IDX:
return pmpRoleA.getIdx().compareTo(pmpRoleB.getIdx());
case TITLE:
return pmpRoleA.getTitleIdx().compareTo(pmpRoleB.getTitleIdx());
case ID_IDX:
return pmpRoleA.getIdIdx().compareTo(pmpRoleB.getIdIdx());
}
return -1;
}
public void setSorter(Sorter sorter){
mSorter = sorter;
mPmpRoleSortedList.replaceAll(mPmpRoles);
}
维护动画功能等