I tried adding a stickyHeader
as suggested in this answer. However, the performance is not close to the production-ready code because of the loop as my list is a bit bigger.
Here is my attempt:
LazyColumn(
reverseLayout = true
) {
val itemCount = list.itemCount
var lastHeader: String? = null
for (index in 0 until itemCount) {
val item = list.peek(index)
val headerString = item?.date
if (item !== null && headerString != lastCharacter) {
stickyHeader(key = headerString) {
Text(
text = "Header: $headerString",
modifier = Modifier
.fillMaxWidth()
.background(Color.Red)
.padding(16.dp)
)
}
}
item(key = item?.communicationId) {
// Gets item, triggering page loads if needed
when (val item = list[index]) {
is Type1 -> {
[email protected] {
Type1Cell()
}
}
is Type2 -> {
[email protected] {
Type2Cell()
}
}
null -> {
[email protected] {
LoadingCell()
}
}
}
}
lastHeader = headerString
}
}
The data-source is a usual flow being collected as:
val items = viewModel.items.collectAsLazyPagingItems()
Also, I noticed the paging API calls keep hitting the backend even without scrolling the list. Is there any better or a stable way to have the stickyHeader
with paging 3?