SortedList关于RecyclerView的排序,去重,更新

1.SortedList的创建

sortedList 相当于一个数据源

SortedList<String> sortedList = new SortedList<String>(String.class, new SortedListAdapterCallback<String>(this) {
                //用于排序,大于0升序,小于0降序,等于0不变
                @Override
                public int compare(String o1, String o2) {
                    return 0;
                }
// 比较两个Item的内容是否一致,如不一致则会调用adapter的notifyItemChanged()
                @Override
                public boolean areContentsTheSame(String oldItem, String newItem) {
                    return false;
                }
// 两个Item是不是同一个,这里用的是string应该会返回true,如果用的是其他的对象,可以用他们的id,或者其他的字段进行比较是否一样
                @Override
                public boolean areItemsTheSame(String item1, String item2) {
                    return false;
                }
            });

2.批量更新和删除

//调用beginBatchedUpdates()之后,所有的对SortedList操作都会等到
//endBatchedUpdates()之后一起生效。
sortedList.beginBatchedUpdates();  // 开始批量更新
sortedList.addAll(items);          // 更新一批数据
sortedList.endBatchedUpdates();    // 结束更新

sortedList.beginBatchedUpdates();  // 开始批量更新
for(Item item : sortedList){       // 删除一批数据
     mData.remove(item);
}
sortedList.endBatchedUpdates();    // 结束更新
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值