https://kotlinlang.org logo
Title
r

rudolf.hladik

11/23/2021, 8:07 AM
Hello there, i’m experiencing disappearing
stickyHeader
😞 in
LazyColumn
when I use
Paging
is it a bug or am I doing something wrong? Code in 🧵
@Composable
fun EventHistoryList(pagingItems: LazyPagingItems<EventHistoryAdapter.Data>) {
    
        LazyColumn(
            modifier = Modifier.fillMaxSize(),
        ) {
            for (index in 0 until pagingItems.itemCount) {
                when (val item = pagingItems.peek(index)) {
                    is EventHistoryAdapter.Data.Header -> stickyHeader {
                        val header = (pagingItems.getAsState(index = index).value as EventHistoryAdapter.Data.Header)
                        EventHistoryHeader(label = header.title)
                    }
                    is EventHistoryAdapter.Data.Item -> item {
                        val data = (pagingItems.getAsState(index = index).value as EventHistoryAdapter.Data.Item)
                       EventHistoryItem(data)
                    }
                }
            }
}
m

myanmarking

03/24/2022, 6:30 PM
why is peek needed here? You will access it anyway. Why not extract to a variable before the when ?
ok. So that’s because
items
block is lazy. If you do as i suggested, it would lose the ‘paging’ ability and load all pages. Do not do this!!
you cant get the item outside
items
also. It would trigger all the page loads!