I have a horizontal pager with two images that isn...
# compose
c
I have a horizontal pager with two images that isn't working how I expect it would. Anyone have any ideas? Code in thread.
Copy code
val myPics = listOf<String>(
    "<https://images.unsplash.com/photo-1631804924848-1f01a975972c?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=934&q=80>",
    "<https://images.unsplash.com/photo-1606787366850-de6330128bfc?ixid=MnwxMjA3fDF8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=2000&q=80>"
)

val pagerState = rememberPagerState(pageCount = myPics.size)

if (myPics.isNotEmpty()) {
    HorizontalPager(
        state = pagerState, modifier = Modifier
            .fillMaxWidth()
            .weight(.75F)
    ) { page ->
        Image(
            modifier = Modifier.fillMaxSize(),
            contentScale = ContentScale.Crop,
            painter = rememberImagePainter(
                data = myPics[pagerState.currentPage],
                builder = {
                    crossfade(true)
                }
            ),
            contentDescription = null,
        )
    }
}
a
Change
myPics[pagerState.currentPage]
to
myPics[page]
.
c
🤦 That worked. Thank you!
😀 1
😄 1
😁 1
👍 2