Question: given a LazyColumn with many items, how ...
# compose
e
Question: given a LazyColumn with many items, how can I collect the indexes of items that user has scrolled through without using a Composable (e.g. storing them to Database)? I tried the side effects:
Copy code
// Use the index as key to be notified when it is changed.
LaunchedEffect(key1 = listState.firstVisibleItemIndex) {
    // Store the listState.firstVisibleItemIndex to DB
}
But it slows down the scroll performance significantly.
a
You can use
snapshotFlow { listState.firstVisibleItemIndex }.collect()
.
e
Oh, from where should I call the
snapshotFlow
? It is a global scope method.
a
Something like:
Copy code
val listState = rememberLazyListState()
LaunchedEffect(listState) {
    snapshotFlow { listState.firstVisibleItemIndex }.collect { index ->
        // Save to DB
    }
}
e
Thanks a lot @Albert Chang, somehow I manage to do the same. Do you know any link to the document that mention the
snapshotFlow
? I want to make sure it is noted somewhere that I just happen to not read yet.
a