What is the best way to make infinite scrollable c...
# compose
p
What is the best way to make infinite scrollable column capable of iterating through big amount of sequental data with windowing support (keep only N count of items in memory, fetching new ones and deleting oldest while scrolling). It is not possible to take items by index, only chunk of them starting from id. So paging is not fit for this case.
s
"Only a chunk of them starting from id", and how do you then fetch more items? Why is paging not a good fit here?
plus1 2
p
Items are kept in RocksDb column family sorted by id. To fetch some chunk i need to open cursor, seek for item by id and then iterate chunk count times to fill it. I want to make this fetch each time column scroll position is near the end of window (passing window's last item id as start id for fetch). To work with paging i need to know item index to be able to calculate page index, but it will be inefficient in my case.
c
LazyList handles the windowing of the composition/rendering, which is the main concern regarding memory and performance in a long scrolling list. https://developer.android.com/develop/ui/compose/lists#lazy Do you actually need to load/unload the items themselves? If so, you can call a function each time an item is composed that informs some other code which item/index is currently being shown in the UI. A typical way to do this would be some function in your viewmodel that accepts the current ID, and alters the list of items in the viewmodel's state accordingly.
Copy code
@Composable
private fun MyList(viewModel: MyViewModel) {
    
    val uiState by viewModel.uiState.collectAsStateWithLifecycle()
    
    LazyColumn {
        items(
            items = uiState.myItems,
            key = { myItem -> myItem.id }
        ) { myItem ->
            
            // rendered composable
            Text(myItem.name)

            // func in your VM that will load/unload uiState.myItems
            // as a result of this ID being rendered
            viewModel.registerSeenId(myItem.id)
        }
    }
}
If you don't want to do that yourself, then there is an integrationa vailable with the paging library as well - https://developer.android.com/develop/ui/compose/lists#large-datasets