Created
May 31, 2023 10:56
-
-
Save EmmanuelGuther/17c926ff5a39cf22220a7077767b1397 to your computer and use it in GitHub Desktop.
Compose Animate Scroll And Centralize Item
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun LazyListState.animateScrollAndCentralizeItem(index: Int, scope: CoroutineScope) { | |
val itemInfo = this.layoutInfo.visibleItemsInfo.firstOrNull { it.index == index } | |
scope.launch { | |
when { | |
itemInfo != null -> { | |
val center = this@animateScrollAndCentralizeItem.layoutInfo.viewportEndOffset / 2 | |
val childCenter = itemInfo.offset + itemInfo.size / 2 | |
this@animateScrollAndCentralizeItem.animateScrollBy((childCenter - center).toFloat()) | |
} | |
else -> { | |
this@animateScrollAndCentralizeItem.animateScrollToItem(index) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
LazyColumn(
state = listState,
modifier = Modifier
.fillMaxSize()
.padding(horizontal = 16.dp),
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
items(rankingUIModel.rankingUIModel.challengeRankingItems) {
ItemQualifying(modifier= Modifier, item = it, onRankingItemPressed = onRankingItemPressed)
}