Sunday 31 January 2021

Android paging and grouping models

I have MVVM-like architecture, data flow is the following:

  • I have data (image src url + linked data) in SQLite

  • I have Repository layer and method like this:

    fun <T> getPhotos(): LiveData<PagedList<PhotoInfo>>

  • I'm using androidx.paging library v.2 and it's PagedListAdapter as adapter for my RecyclerView

Now I want to group data by some field, f.e. by date and have ability to have groups to be either horizontal (with horizontal scrolling) or vertical.

I went through existing answers & solutions for similar issue and here're options I found:

Option 1

I can update query to group data and insert 'special' rows as separators. Then in Adapter I can have some branching logic to handle 'regular' rows and 'separators'.

I don't like this option because it involves this undesirable change of SQL query. Also I can't imagine how to implement 'horizontal' group scrolling in this case.

Option 2

I can have nested recycler views, but in this case I don't know how to update my code so that I could have PagedList<PhotoGroup> from 'linear' PagedList<PhotoInfo>.

The only thing I can think:

  • have two methods in repository, which return 'paged' data, for groups and item in a particular group, like:
    • fun <T> getPhotoGroups(): LiveData<PagedList<PhotoGroup>>
    • fun <T> getPhotosForGroup(groupId: Long): LiveData<PagedList<PhotoInfo>>
  • every group has it's own PagedList and RecyclerView.

But I'm afraid it can be over-complicated and might have overhead. Also not sure it will work fine if I have parent RecyclerView and child RecyclerView in the same direction (vertical)

I have a strong feeling I'm missing something and over-complicating things.



from Android paging and grouping models

No comments:

Post a Comment