Created
October 9, 2025 07:50
-
-
Save tomriddle25/5ee117e2a296d179adfb237992042af5 to your computer and use it in GitHub Desktop.
Creating a looping pager in compose
This file contains hidden or 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
| @Composable | |
| fun LoopingCarousel(modifier: Modifier = Modifier) { | |
| Column(modifier = modifier) { | |
| // create a caraousel of banners | |
| // use the following url https://picsum.photos/200/300 | |
| val banners = listOf( | |
| "https://picsum.photos/id/1/400/600", | |
| "https://picsum.photos/id/2/400/600", | |
| "https://picsum.photos/id/3/400/600", | |
| ) | |
| // To achieve a looping effect, we can use a very large number of pages | |
| // and start at a middle point. | |
| val pageCount = Int.MAX_VALUE | |
| val initialPage = pageCount / 2 | |
| val pagerState = rememberPagerState(initialPage = initialPage, pageCount = { pageCount }) | |
| HorizontalPager( | |
| state = pagerState, | |
| modifier = Modifier.fillMaxWidth() | |
| ) { page -> | |
| val actualPage = (page - initialPage).mod(banners.size) | |
| AsyncImage( | |
| model = banners[actualPage], | |
| contentDescription = "Banner Image ${actualPage + 1}", | |
| contentScale = ContentScale.Crop, | |
| modifier = Modifier | |
| .fillMaxWidth() | |
| .height(200.dp) | |
| ) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment