Mubashir P A
Mubashir P A

Reputation: 31

IllegalArgumentException when using Int.MAX_VALUE as page size in Jetpack compose HorizontalPager

I have implemented a infinite scrolling HorizontalPager in Jetpack compose. Intially it worked ok but recently i am getting (java.lang.IllegalArgumentException: Cannot coerce value to an empty range: maximum -1564.0 is less than minimum 0.0) error when i scroll the pager. I dont actually know when did this happened.

Sample code

val listSize = exploreList.size
val pageCount = Int.MAX_VALUE
val maxRounds = pageCount / listSize
val initialPage = (maxRounds / 2) * listSize
val pagerState =
    rememberPagerState(initialPage = initialPage, pageCount = { pageCount })

HorizontalPager(
state = pagerState,
modifier = Modifier.fillMaxWidth(),
contentPadding = PaddingValues(horizontal = 54.dp),
) {
    val page = it % exploreList.size
    val id = exploreList.getOrNull(page)?.id ?: 0
    val posterPath = exploreList.getOrNull(page)?.posterPath
    ExploreListItem(
        posterPath = posterPath,
        modifier =
        Modifier
            .fillMaxWidth()
            .aspectRatio(4F / 5F)
            .carousalTransition(it, pagerState),
        onClick = { navigateToMovieDetails(id) },
    )
}

Version details

Compose BOM version: 2023.10.01

Kotlin version: 1.9.21

For now I replaced Int.MAX_VALUE with a constant value like: val pageCount = 1000. Here no problem.

How to fix this issue?

Upvotes: 0

Views: 703

Answers (1)

jdvp
jdvp

Reputation: 1

For reference, this appears to have been a bug in the beta version of the compose foundation library. See the relevant issue here:

https://issuetracker.google.com/issues/311414925

That being said, you still might run into this other issue with trying to do infinite paging in 1.6.x versions:

https://issuetracker.google.com/issues/326887746

Upvotes: 0

Related Questions