android - 强制使用 Android appcompat `SortedList` ?

标签 android android-recyclerview android-appcompat

我有一个 SortedList由我的 RecyclerView.AdapterRecyclerView 中显示。

我使用来自 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/

相关文章:

android - 如何从牛轧糖上传选定的视频,路径返回 null

Android - 是否可以将滚动条的起始位置设置在 GridView 的填充顶部内?

Android 使用 StaggeredGridLayoutManager 缩放 RecyclerView 的项目高度以匹配列宽

android - ActionBarActivity 和 Fragment Activity 的区别

android - AppCompat、colorAccent、colorPrimary 和 colorPrimaryDark 在 AIDE 上不起作用

java - Ksoap : Cannot Serialize exception when passing user defined class as parameter to web method

javascript - 在按钮的 OnClick 事件中执行 Javascript 代码

android RecyclerView setOnScrollChangeListener 需要 api 23?

java - 如何使用来自 firebase 的嵌套对象填充 RecyclerView

android - 为什么setEnabled(false)不会触发主题Theme.AppCompat.Light的ColorStateList?