Android: SortedList with duplicates

后端 未结 6 993
栀梦
栀梦 2021-02-19 21:58

I have some problems understanding RecyclerViews SortedList.

Lets say I have a very simple class only having a very simple class holding data:<

6条回答
  •  孤城傲影
    2021-02-19 22:20

    SortedList does not keep any mapping by ids (because there are no ids in the API). So when the sorting criteria changes (a to b in your case), SortedList cannot find the existing element.

    You can keep the id mapping yourself, then have your add method as follows:

    void add(Item t) {
      Item existing = idMap.get(t.id);
      if (existing == null) {        
         sortedList.add(t);
      } else {
         sortedList.updateItemAt(sortedList.indexOf(existing), t);
      }
      idMap.put(t.id, t);
    }
    

    You'll also need to implement a remove method to remove the item from the idMap.

提交回复
热议问题