Jan Skrasek
11/22/2024, 10:28 PMval 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?Hristijan
11/23/2024, 6:22 AMval 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)
}
}
}
Jan Skrasek
11/27/2024, 8:28 PMStylianos Gakis
11/28/2024, 10:59 AMloadAround
in the paging artifact https://cs.android.com/search?q=loadAroundJan Skrasek
11/28/2024, 11:00 AMHristijan
11/28/2024, 11:02 AMStylianos Gakis
11/28/2024, 11:05 AMloadAround
, that's all 😊Jan Skrasek
11/28/2024, 11:06 AMJan Skrasek
11/28/2024, 11:10 AMLaunchedEffect(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
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, soStylianos Gakis
11/28/2024, 11:11 AM