I want to achieve something like :
17 July,2018
- item
- item
16 July,2018
- item
- item
- item
I have achieved this successfully with the following link :
Divide elements on groups in RecyclerView or Grouping Recyclerview items ,say by date
But now the problem is in pagination if same date is there in next page also then it will create second header of same date.
i.e . if in first page i have data with date 16 July,2018 and in second page also if i have data continued with date 16 July,2018 then two header are created for both 16 July,2018.
Page -1
17 July,2018
- item
- item
16 July,2018
- item
- item
Page -2
16 July,2018
- item
- item
- item
- item
14 July,2018
- item
- item
I want all item of one date should be under only one header.. i want to merge two page data if same date..
My code is same as above given link.. still i am adding some important code and if any other code required then please let me know..
private LinkedHashMap<String, List<NotificationData>> groupDataIntoHashMap(List<NotificationData> listOfPojosOfJsonArray) {
LinkedHashMap<String, List<NotificationData>> groupedHashMap = new LinkedHashMap<>();
for (NotificationData pojoOfJsonArray : listOfPojosOfJsonArray) {
String hashMapKey = pojoOfJsonArray.getTicket_date();
if (groupedHashMap.containsKey(hashMapKey)) {
// The key is already in the HashMap; add the pojo object
// against the existing key.
groupedHashMap.get(hashMapKey).add(pojoOfJsonArray);
} else {
// The key is not there in the HashMap; create a new key-value pair
List<NotificationData> list = new ArrayList<>();
list.add(pojoOfJsonArray);
groupedHashMap.put(hashMapKey, list);
}
}
return groupedHashMap;
}
Inside service success :
try {
JSONObject object = new JSONObject(response);
if (!Utils.isEmptyString(response)) {
if (id == reqIdNotificationList) {
if (object.getInt(PARAMS.TAG_STATUS) == PARAMS.SUCCESS_STATUS) {
String resultArray = object.getString(PARAMS.TAG_RESULT);
if (!Utils.isEmptyString(resultArray)) {
Type listType = new TypeToken<List<NotificationData>>() {
}.getType();
ArrayList<NotificationData> tmpNotification = new Gson().fromJson(resultArray, listType);
if (tmpNotificationList != null)
tmpNotificationList = new ArrayList<>();
if (TextUtils.isEmpty(mtktNo)) {
tmpNotificationList.clear();
tmpNotificationList.addAll(tmpNotification);
LinkedHashMap<String, List<NotificationData>> groupedHashMap = groupDataIntoHashMap(tmpNotificationList);
for (String date : groupedHashMap.keySet()) {
dateItem = new DateItem();
dateItem.setTicket_date(date);
consolidatedList.add(dateItem);
for (NotificationData pojoOfJsonArray : groupedHashMap.get(date)) {
generalItem = new GeneralItem();
generalItem.setResult(pojoOfJsonArray);
consolidatedList.add(generalItem);
}
}
adapter = new NotificationListAdapter(this, consolidatedList, this);
rvNotification.setAdapter(adapter);
// notificationList.addAll(tmpNotificationList);
// adapter = new NotificationListAdapter(NotificationActivity.this, notificationList, this);
// rvNotification.setAdapter(adapter);
} else {
int startIndex = tmpNotification.size();
tmpNotificationList.addAll(tmpNotification);
Log.d("size",tmpNotificationList.size()+"");
adapter.notifyItemRangeInserted(startIndex, tmpNotificationList.size() - 1);
LinkedHashMap<String, List<NotificationData>> groupedHashMap = groupDataIntoHashMap(tmpNotificationList);
for (String date : groupedHashMap.keySet()) {
DateItem dateItem = new DateItem();
dateItem.setTicket_date(date);
consolidatedList.add(dateItem);
for (NotificationData pojoOfJsonArray : groupedHashMap.get(date)) {
GeneralItem generalItem = new GeneralItem();
generalItem.setResult(pojoOfJsonArray);
consolidatedList.add(generalItem);
}
}
if (consolidatedList.size() >= object.getInt(PARAMS.TAG_RECORD)) {
rvNotification.removeMoreListener();
}
}
if (tmpNotificationList.isEmpty()) hideMenu();
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
Note : In given link it is hashmap but i have used linked hashmap to maintain item order.
Also i have used this linear layout manager.
Thank you !