Oliver.O
02/06/2023, 12:06 AMandroidx.paging.Pager and Compose LazyListState. Could someone enlighten me? Details in 🧵.Oliver.O
02/06/2023, 12:07 AMandroidx-main/frameworks/support/paging/paging-compose/samples/src/main/java/androidx/paging/compose/samples/PagingSample.kt we have the following code (simplified):
@Composable
fun PagingBackendSample() {
val pager = remember {
Pager(...) { ... }
}
val lazyPagingItems = pager.flow.collectAsLazyPagingItems()
LazyColumn {
itemsIndexed(lazyPagingItems) { index, item ->
Text("Index=$index: $item", fontSize = 20.sp)
}
}
}
If my understanding is correct, if pager.flow wants to react to scrolling and emit new stuff on demand, it must at some point observe LazyListState. Yet there seems to be no explicit connection. How does pager find the corresponding LazyListState to observe?Casey Brooks
02/06/2023, 12:52 AMLazyPagingItems returned by collectAsLazyPagingItems notifies the pager on each call to .get(), prompting the load of the next value https://github.com/androidx/androidx/blob/androidx-main/paging/paging-compose/src/main/java/androidx/paging/compose/LazyPagingItems.kt#L130-L133Oliver.O
02/06/2023, 1:19 AMOliver.O
02/06/2023, 3:31 PMPager.flow emits just for refresh and invalidation of background data. The connection between scroll position and item fetching is
• registered via the content lambda of LazyListScope.itemsIndexed (in paging.compose),
• which wraps the above get() invocation to fetch items and registers another lambda with LazyListScopeImpl.items.
• The latter lambda is then invoked repeatedly per element in response to scrolling.allan.conda
02/16/2023, 8:26 AMregistered via the content lambda ofDoes this mean it starts fetching upon(inLazyListScope.itemsIndexed),paging.compose
collect? We’re trying to actually make it lazy fetch so it only fetch when the user scrolls to the section, but can’t find a way yet other than making a fake lazy item{} to trigger creation of the pager to collect from.Oliver.O
02/16/2023, 1:56 PMPagingSample.kt mentioned above, you should see lazy fetching. Apart from that, I'm not using the Paging library, which is huge and complicated, so I cannot comment on its peculiarities. You might be seeing effects of a prefetch window size which is larger than expected, so everything gets fetched eagerly. In this case you' could try with a larger data set. But that's just a guess.