1

I am stuck at this for quiet a long time.I have to show the data from REALM in grouped manner.Let me give an example

  • Nov 2017
    • Item 1
    • Item 2
    • Item 3
  • OCT 2017
    • Item 1
    • Item 2
  • SEP 2017
    • Item 1
    • Item 2

This list should update itself whenever the data changes.Suppose an item can be inserted in OCT 2017 and then list should reflect the correct data. It will be something like:

  • Nov 2017
    • Item 1
    • Item 2
    • Item 3
  • OCT 2017
    • Item 1
    • Item 2
    • Item 3
  • SEP 2017
    • Item 1
    • Item 2

I already have a solution but It is not optimal in a sense that I have grouped the data and showing it in RecyclerView but for any changes I have to call notifyDataSetChanged to achieve it so that entire UI for the list is redrawn again and it matches my requirement

Farmaan Elahi
  • 1,620
  • 16
  • 18
  • In case of Section header you need to call notifyDataSetChanged (). I thing this is the only choice . You you calculate the range b/w two section and notify with the range, but that will be bit tricky . If you can Modify UX you can use[expandablerecyclerview] (https://github.com/thoughtbot/expandable-recycler-view) – ADM Nov 12 '17 at 13:27
  • Possible duplicate of [Divide elements on groups in RecyclerView or Grouping Recyclerview items ,say by date](https://stackoverflow.com/questions/41447044/divide-elements-on-groups-in-recyclerview-or-grouping-recyclerview-items-say-by) – Hafez Divandari Sep 05 '18 at 12:28
  • @HafezDivandari I know about that library but I cannot use that on Realm Db. It has issues because of Realm Live/Managed Object – Farmaan Elahi Sep 06 '18 at 05:41
  • 1
    What you can do is in `OnBindViewHolder`, have the Items list ordered then check the previous and next items, if the month and year is the same, hide the "header textview" for the item, or show it with the new date. So all items have the month/year header, just show and hide according to your data. Then when an item is added, simply call `NotifyItemInserted` or what have you – Pierre Sep 06 '18 at 05:44
  • @Pierre That's exactly what I am using in my current solution – Farmaan Elahi Sep 06 '18 at 05:57
  • Well, AFAIK this is what most apps use, like WhatsApp. – Pierre Sep 06 '18 at 05:58
  • @Pierre Also regarding your solution. Just using `notifyItemInserted` will make the UI to be in an inconsistent state. Let say you have an item a position 5 which is already showing header `June`.Now want to add an item just before that which will also have the same header `June`. It will result in a state where there are 2 item having the same header `June` because the item which was earlier at position 5, didn't got invalidate so its header was still shown – Farmaan Elahi Sep 06 '18 at 06:10
  • In that case you can use `notifyItemRangeChanged(int positionStart, int itemCount)` after you have retrieved the item positions which have the same month – Pierre Sep 06 '18 at 06:13

0 Answers0