I have paging3 & horizontal pager and I need t...
# compose
j
I have paging3 & horizontal pager and I need to get to the initial page which is not in the first loaded batch.
Copy code
val photos = viewModel.photos.collectAsLazyPagingItems()
val pagerState = rememberPagerState(
  initialPage = wantedPosition,
  pageCount = { photos.itemCount },
)
Using
initialPage
lands (in this case) on the last page of the first batch. Using Pager's
initialKey
doesn't work correctly as well - the loading starts correctly but pager scrolls "twice" the offset (as it does not know that the loaded batch is "already loaded for the wanted offset"). Not using
initialPage
when having
initialKey
has another problem, then the pager does not know you can scroll backwards. Is there any official demo/solution to this?
h
Copy code
val pagerState = rememberPagerState(
        initialPage = 0,
        pageCount = { photos.itemCount }
    )
    
    LaunchedEffect(photos.itemCount) {
        // Wait for the items to be loaded
        if (photos.itemCount > 0 && initialPosition < photos.itemCount) {
            // Ensure the initial position is loaded
            photos.loadAround(initialPosition)
            // Animate to the desired position once data is available, jf needed 
            coroutineScope.launch {
                pagerState.animateScrollToPage(initialPosition)
            }
        }
    }
j
Sorry, this seems like LLM answer 😄 The loadAround is not in paging3. I created a bug since I haven't found other solution than fetching everything until the initialPage position. https://issuetracker.google.com/issues/381216363
s
Some hits for
loadAround
in the paging artifact https://cs.android.com/search?q=loadAround
j
See my ticket - I found a workaround.
h
@Jan Skrasek load around is available on my end, it’s a method in the PagedList
👍 1
s
I just wanted to note this because you did not seem to find
loadAround
, that's all 😊
j
Maybe I am using Paging wrong, but I do not have PagedList along the way. Pager -> Flow<PagingData<T>> -> LazyPagingItems<T> 😐
TBH I still don't understand the suggested code. • why launching it every time the itemCount chagnes (i.e., loading more triggers the effect) • why doing "loadingAround" when the initial position is smaller that photos.itemCount - in that case it is not needed, is it?
Copy code
LaunchedEffect(photos.itemCount) {
// Wait for the items to be loaded
        if (photos.itemCount > 0 && initialPosition < photos.itemCount) {
            // Ensure the initial position is loaded
            photos.loadAround(initialPosition)
            // Animate to the desired position once data is available, jf needed 
            coroutineScope.launch {
                pagerState.animateScrollToPage(initialPosition)
            }
        }
Let's say I would have loadAround present, I'd expect something like
Copy code
LaunchedEffect(Unit) {
   if (initialPposition >= photos.itemCount) {
        photos.loadAround(initialPoisition)
        pagerState.scrollToPage(initialPosition) // this would work if loadAround would be suspending, otherwise I fear it could trigger to early and fail.
   }
}
Also, ideally, I do not want to animate, so
s
Looking at our paging code, I also don't see us using PagedList anywhere actually. I see the chain you are describing too Jan.