KoskiA
09/12/2022, 8:18 PMLazyColumn
at a specific scroll amount, instead keeping an item/key visible, when inserting items? (i.e. sticking to the top of the list when inserting at zero)KoskiA
09/12/2022, 8:19 PM@Composable
fun Example() {
val state = rememberLazyListState()
val items = remember { mutableStateListOf<String>() }
LaunchedEffect(Unit) {
while (isActive) {
delay(1000)
items.add(0, "Item ${items.size}")
}
}
LazyColumn(
state = state,
modifier = Modifier.fillMaxSize(),
contentPadding = PaddingValues(vertical = 32.dp),
) {
items(items = items, key = { it }) { item ->
Text(text = item, modifier = Modifier.padding(16.dp))
}
}
}
Alex Vanyo
09/12/2022, 8:59 PMitem
as a trick:
LazyColumn(
state = state,
modifier = Modifier.fillMaxSize(),
contentPadding = PaddingValues(vertical = 32.dp),
) {
item {
Spacer(Modifier.fillMaxWidth())
}
items(items = items, key = { it }) { item ->
Text(text = item, modifier = Modifier.padding(16.dp))
}
}
It doesn’t change anything visually in this case, but it does change the behavior:
When you’re scrolled all the way to the top, the “currently visible item” is the Spacer
, so adding new items below it keeps the Spacer
at the top.Alex Vanyo
09/14/2022, 7:51 PMKoskiA
09/18/2022, 1:45 PMAlex Vanyo
09/18/2022, 11:24 PMLazyGrid
you might be able to make the item use the max span? And you might be able to mess around with the height of the spacer to change behavior a bit, but yeah not super configurable otherwise