Reputation: 321
I haven't been able to actually get this to reliably reproduce, but I'm trying to set up a HorizontalPager which is an image carousel, but it paginates. It seems to run into an IndexOutOfBoundException but I'm not sure why.
@Composable
fun Card(
imageUrlList: List<String>
) {
val pagerState = rememberPagerState { imageUrlList.size }
HorizontalPager(
state = pagerState
) {
val imageUrl = imageUrlList[it]
...
)
)
It seems that it will get IndexOutOfBoundException when calling imageUrlList[it]
How do I use the HorizontalPager properly with a list that the size can change?
I tried different things such as setting imageUrlList
to ImmutableList
and val pagerState = key(LocalView.current) { rememberPagerState { imageUrlList.size } }
since I thought maybe the rememberSaveable
in rememberPagerState
was causing the issue, but it still crashes, thought less frequently.
Most recently I tried adding a log inside the HorizontalPager that says
Index $it out of bounds for length ${imageUrlList.size}. PagerState page count is ${pagerState.pageCount}
and I will get errors logged like "Index 6 out of bounds for length 6. PagerState page count is 24". It seems like somehow the list gets cleared at some point but the pager state remembers the highest count it previously held, resulting in this error.
Upvotes: 2
Views: 87