architecture-components-samples icon indicating copy to clipboard operation
architecture-components-samples copied to clipboard

Load a list at specified item position with Paging + Room

Open ParticleCore opened this issue 4 years ago • 3 comments

Are there any plans to provide a sample showing how to load a PagingSource from the Room's Dao at a certain item position?

Example:

  • User sends me an image.
  • I click on it and it opens in a fullscreen view
  • In that same screen I also display all the images the user sent to me at the bottom in an horizontal list using Paging + Room
  • That list is initially loaded with the clicked item visible in the view
    • the item that is in fullscreen view is also in this bottom image list visible to the user to help him locate the image in between the other images in that list.

I looked for something like this and I did not find anything.

Thanks for making these great components!

ParticleCore avatar Nov 17 '20 07:11 ParticleCore

A bit more context here: https://issuetracker.google.com/176503896

In general there are basically two ways to do this:

  • Changing initialKey passed to Pager
  • enablePlaceholders in PagingConfig, set jumpThreshold and implement jumpingSupport + getRefreshKey() in PagingSource to allow fast-scroll behavior that allows you to load from scratch from an arbitrary position, without appending / prepending until you get there.

However if you are using Room, in both cases you need a way to map the desired starting item to the position in Room which unfortunately is quite hard until we can figure out how to support item-keyed paging source in Room. The easiest way is probably to implement a custom item-keyed PagingSource that just queries from Room, but allows you to use the same key across your app + paging integration.

dlam avatar Feb 09 '21 00:02 dlam

Has there been any improvement done around this issue, or is the same dilemma present in the current versions of the Paging library?

ParticleCore avatar Sep 15 '21 14:09 ParticleCore

Room does not support item keyed paging yet - they've moved to paging3 but still have some ways to go to get to item keyed.

SQLDelight allows a key map argument for their paging integration which supports item keyed, but yigit will hunt me down for telling you this :)

dlam avatar Sep 16 '21 04:09 dlam

Any update on this?

ParticleCore avatar Feb 08 '23 23:02 ParticleCore