Hello all, wondering if anyone has had issues with...
# compose
b
Hello all, wondering if anyone has had issues with animateScrollToItem using a LazyColumn. I have a list of items, users can add new items, on add I would like to scroll to the top of the new item. Trying something like this:
Copy code
val items by state.items
  val scope = rememberCoroutineScope()
  var itemCount by remember { mutableStateOf(state.items.value.size) }

  LazyColumn(
    state = listState,
    modifier = Modifier.fillMaxHeight()
  ) {
    item {
      HeaderContent()
    }

    itemsIndexed(items) { index, item ->
      ItemContent(item)
    }

    if (itemCount < items.size) {
      scope.launch {
        // items.size, not items.size - 1 to account for header
        listState.animateScrollToItem(items.size)
      }

      itemCount = items.size
    }
  }
This works most of the time. However, every so often the scroll will end at the bottom or middle of the newly added item, not the top of the new item.
k
d
Idk if it's related but you should replace
scope.launch
with
LauncedEffect
.
Yeah that should definitely be
LaunchedEffect(items.size)
. I think it's related now.
b
Using LaunchedEffect inside LazyColumn produces
Copy code
@Composable invocations can only happen from the context of a @Composable function
d
Do it outside the
LAzyColumn
.
b
Same thing, first item add, scrolls to bottom of item, not top
Still very random, sometimes it will scroll to the middle, other times the bottom. Most of the time it is working and scrolling to the top of the new item.
m
I have exactly the same issue, and also ScrollTo misbehaves .... what is weird is the random nature of the error now I work, now I don't
b
I am going to put together a small sample on github, and then possibly write up a ticket. There is for sure something going on here.
👍 1