Skip to content

Instantly share code, notes, and snippets.

@muhammedesadcomert
Last active January 5, 2024 12:24
Show Gist options
  • Save muhammedesadcomert/4a457010f0c6418db0ae45824b58920b to your computer and use it in GitHub Desktop.
Save muhammedesadcomert/4a457010f0c6418db0ae45824b58920b to your computer and use it in GitHub Desktop.
Infinite Transition with Pager
val pagerState = rememberPagerState { Int.MAX_VALUE }
LaunchedEffect(Unit) {
while (true) {
delay(1500) // The duration of the image on the screen
if (pagerState.currentPage >= YourEnumClass.entries.lastIndex - 1) {
pagerState.animateScrollToPage(0) // If it reaches the end of the list, it returns to the beginning
} else {
pagerState.animateScrollToPage(pagerState.currentPage + 1)
}
}
}
HorizontalPager(
modifier = Modifier.fillMaxWidth(),
state = pagerState,
userScrollEnabled = false,
verticalAlignment = Alignment.CenterVertically,
) { page ->
Box(modifier = Modifier.fillMaxWidth(), contentAlignment = Alignment.Center) {
Image(
modifier = Modifier.size(72.dp),
// I'm using an Enum Class for images. But you can use a regular list here, it's not mandatory
painter = painterResource(id = YourEnumClass.entries[page].imageRes),
contentDescription = null
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment