I built a complex lazyColumn composable that mimic a card (indeed, I need to have a list of cards containing potentialy many items each, and nesting a LazyColumn in a Card is not possible (due to the “Vertically scrollable component was measured with an infinity maximum height contraints” issue). So my composable gets a list of data items of this kind :
data class CardItems(val cards: ImmutableList<Card>)
data class Card(
val header: String,
val items: ImmutableList<CardItem>,
)
In the LazyColumn, it can takes several LazyListScope.item to draw one CardItem.
So far so good.
But I also need sometimes to scroll to a specific CardItem. Unfortunately, I can’t use the LazyListState.animateScrollToItem(index: Int) function as it takes the item index as parameter, and i can’t tell what is the lazyColumn item index I need as it can be tricky to calculate.
The best for me would be to have a LazyListState.animateScrollToItem(key: Any) as all my LazyColumn items have a unique key that I can pass as a parameter to my state like this :
data class CardItems(
val cards: ImmutableList<Card>,
val scrollToItemWithKey: Any?, // The key of the lazyColumn item I want to scroll to
)
data class Card(
val header: String,
val items: ImmutableList<CardItem>,
)
@Composable
fun MyContent(cardItems: CardItems) {
val scrollState = rememberLazyListState()
LazyColumn {
// compose cards
}
LaunchedEffect(key1 = cardItems.scrollToItemWithKey) {
cardItems.scrollToItemWithKey?.let { key ->
scrollState.animateScrollToItem(key = key, scrollOffset = 0)
}
}
}
But obviously this scrollToItemWithKey doesn’t exist. Is there another way for me to achieve this scrolling ?