Feri Nagy
06/15/2022, 7:54 AMitems(pagingItems) {}
, we have to iterate through the items manually, do pagingItems.peek(index)
and add single item {}
one by one:
@Composable
fun PlainPaging(pagingItems: LazyPagingItems<Item>) {
LazyColumn {
items(pagingItems) { item ->
when (item) {
is Item.Header -> Text(text = item.title)
is Item.Data -> Text(text = item.text)
null -> Text("placeholder")
}
}
}
}
// vs
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun StickyPaging(pagingItems: LazyPagingItems<Item>) {
LazyColumn {
for (index in 0 until pagingItems.itemCount) {
when (val item = pagingItems.peek(index)) {
is Item.Header -> stickyHeader { Text(text = item.title) }
is Item.Data -> item { Text(text = item.text) }
null -> item { Text("placeholder") }
}
}
}
}
Is this going to have some performance implications? Is there a better way to do it? ThanksFeri Nagy
06/15/2022, 8:09 AMitems(list) {...}
vs list.forEach { item {...} }
?Andrey Kulikov
06/15/2022, 12:19 PMitems
instead of forEach
is a bit more efficient. when we lookup the index to composable mapping we will have to use a binary search in order to find a correct item
as there are could be any amount of item
items
calls. but for Paging use case there is no better solution for now, so probably it is okFeri Nagy
06/15/2022, 2:10 PMpagingItems.get(index)
, so it would not trigger loading. 😬
What about using items(count) {}
? Would it be worth pre-processing the list so it could be used? Something along:
val sectionIndices = (0 until pagingItems.itemCount).mapNotNull {
it.takeIf { pagingItems.peek(it) is Item.Header }
}
val countBefore = sectionIndices.firstOrNull() ?: pagingItems.itemCount
items(countBefore) {
//...
}
sectionIndices.forEachIndexed { index, section ->
stickyHeader { Text(text = (pagingItems[section] as Item.Header).title) }
val end = if (index == sectionIndices.lastIndex) pagingItems.itemCount else sectionIndices[index + 1]
items(end - section) {
// ...
}
}
Filip Wiesner
04/11/2023, 10:33 AM