0

I'm trying to jump to a specific item in paging3. I'm using Room as local PagingSource (documentation says that it already handles the getRefreshKey) and i'm using it inside a ViewPager2. When i set the currentItem the view pager tries to jump to it, but it stops at the last item of the current page. Is there currently a way to jump to an item outside the current page?

Edit:

The code is in the commit "Refactored to orbit" in the branch "mvi-refactoring" in this project https://gitlab.com/sedestrian/pokedex/-/tree/feature/mvi-refactoring.

There are 2 pages, a list and a detail in the modules "pokemonlist" and "pokemondetails" respectively. The list is a paging3 list. It has some issues (like sometimes paging doesn't recognize that it shoud load more items) but it mostly works correctly, the problem is in the details page. At the top i have a viewpager2 with the same items of the list, also paged with paging3.

The goal is to click on a pokemon in the list and go to the same pokemon in the details.

The data is loaded from the network and cached in room, the code that ties them together is in the module "pokemon" in the class "PokemonRepositoryImpl".

I've tried wrapping room datasource like in the comments but i'm not sure how to do such a thing, my attempts either didn't build or didn't load any data.

alessandro gaboardi
  • 889
  • 3
  • 11
  • 26
  • 1
    Do you have `placeholdersEnabled = true` set in `PagingConfig`? You cannot scroll to a non-existent item, so letting paging send `null` items to bind in adapter will let you scroll to them, and then Paging will trigger the correct load request and fill them in. – dlam Jan 28 '21 at 07:00
  • Yes i do have them enabled but they don't seem to work, i call currentItem = 500 and it jumps from element 0 to like element 30. I see null items being provided in the adapter so one would think it's working, but it doesn't seem to be because the scrolling stops – alessandro gaboardi Jan 28 '21 at 09:35
  • Also i must clarify that items are coming from a remote source, being cached in room, and room doesn't know the size of the dataset. Only the api does. It looks like at the moment the viewPager is jumping to the page of the last known item by room, if the page contains the target item setting the position againg moves the view pager to the right item, if the page doesn't contain the target item paging starts to load more items, then if i try again it just repeats. – alessandro gaboardi Jan 28 '21 at 09:57
  • 1
    The easiest way to do what you want is to probably wrap the paging source you get from room and modify the placeholder count in the pagest returned, because it's impossible for UI to jump to an item that doesn't exist, you have to give it a placeholder so that it knows the list has n items, allowing you to scroll to the nth position. – dlam Jan 29 '21 at 19:46
  • 1
    In the same vein, paging needs to understand you want to scroll to the 500th item, but if it thinks it's scrolling to the 30th item because there are only that many placeholders available, that's where it will continue to trigger loads from. It's not really clear to me what your setup looks like without any code so it's hard to comment further. Could you post / link your project? – dlam Jan 29 '21 at 19:46
  • Absolutely! I have updated the question with more details and a link to the project. If you need any more detail please let me know and I'll try to provide it. Thanks for the help – alessandro gaboardi Jan 31 '21 at 01:46
  • @dlam I'm experimenting some more with writing my own pagingsource, and i'm having some results, one of the things i don't understand is why where does remote mediator receive its PagingState.anchorPosition, because when i reach a placeholder i receive a position, say 500, in the pagingsource getRefreshKey, i return that 500 as the refreshKey, but the remote mediator receives no data to indicate what page to download. It triggers a REFRESH state, but no anchor position or other data useful to load the correct page – alessandro gaboardi Feb 02 '21 at 15:12
  • @dlam I have also just tried ignoring everything i asked before and setting the initialKey to something that isn't 0 and the RemoteMediator still doesn't receive any indication of what page to load, i also tried adding a initialKey to the paging3 codelab code with no better luck, list still loads from position 0 – alessandro gaboardi Feb 02 '21 at 22:27
  • There is an open bug to address null anchorPosition in remote refresh on subsequent loads (not initial one) https://issuetracker.google.com/178518643. Please follow / star this. – dlam Feb 04 '21 at 04:26
  • Thank you, i did look through the open issue but I missed this one. – alessandro gaboardi Feb 04 '21 at 07:27

0 Answers0