java随机动态分组_关于Java动态分组排序的问题(Android中需要将数据排序给RecyclerView使用)...

问题描述

现在有这样一个TestNotification里面定义了这些字段

private long time;

private String pkg;

private String content;

private String title;

然后我需要将这些字段里面的pkg也就是PackageName分组,可能有音乐包名的,新闻包名的各种应用发来的不同消息,我需要将这些消息根据相同的包名来分组,目前可以将整个集合里面的数据用map分组。

private ArrayList notificationArrayList = new ArrayList<>();

//添加消息,转换组

public void addGroupItem(TestNotification notification) {

//添加数据

notificationArrayList.add(notification);

//转换map

Map> resultMap = new LinkedHashMap<>();

for (TestNotification testN : notificationArrayList) {

String pkg = testN.getPkg();

if (resultMap.containsKey(pkg)) {

resultMap.get(pkg).add(testN);

} else {

List tmp = new LinkedList<>();

tmp.add(testN);

resultMap.put(pkg, tmp);

}

}

//遍历map

for (String key : resultMap.keySet()) {

//将key信息放入分组的头部

Level0Item level0Item = new Level0Item(key, "---聚合---");

List notificationList = resultMap.get(key);

//遍历key下面的各个value

for (TestNotification testNotification : notificationList) {

//将key下面的value加入可折叠列表的子类中

level0Item.addSubItem(new Level1Item(testNotification.getTitle(),

testNotification.getPkg(), testNotification.isExpandItem(), testNotification.getContent()));

}

entityList.add(level0Item);

}

//为recyclerView设置数据源,通知刷新界面

setNewData(entityList);

}

希望能够解决的问题:

目前这样做,只能每次将所有的消息实体类遍历,然后每次为recyclerView设置新的数据源,导致已经展开的item,每次刷新都会重新合上。有什么好的排序方式,能够让我单处刷新新来的一条TestNOtification,而不需要每次全部遍历?

ecee747344ceca05ccdbc46f1d21771d.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值