Jetpack Compose - Swipe interaction on HorizontalPager not working after BoM update

511 Views Asked by At

I recently updated my compose-BoM version from 2023.06.01 to 2023.08.00 (and later 2023.10.00) and also updated the navigation-compose dependency (which is not included in the BoM) from 2.6.* to 2.7.1 (later 2.7.4).

With 2023.06.01 / 2.6.* my HorizontalPager worked perfectly. Since the update it does not. When I swipe to change the page, or when I change the page programatically by calling pagerState.animateScrollToPage(int), the page does not visibly change. When I check the pagerState's currentPage value, that has changed to the correct page.

As opposed to the examples in the official docs, the amount of pages can change dynamically in my situation.

Here's the code (simplified):

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun <T> MyPager(items: List<T>) {
    
    val pagerState = rememberPagerState(
        initialPage = 0,
        initialPageOffsetFraction = 0f,
        pageCount = { 
            Log.i(TAG, "pageCount called")
            items.size 
        }
    )

    HorizontalPager(
        state = pagerState,
        pageSpacing = -50.dp // Makes edge of next/prev page visible
    ) { pageIndex ->
        
        val item = items.getOrNull(index) ?: return@HorizontalPager
        
        Box(modifier = Modifier.fillMaxSize()) {
            Text("Item at page $pageIndex")
            // Show other things for `item` here
        }
    }
}

------

@Composable
fun MyScreen(viewModel: MyViewModel) {

    val items = viewModel.itemList.collectAsState() //itemList is of type Flow<List<MyItem>>

    MyPager(items)
}

When the list of items contains multiple items, I can see the next page due to the pageSpacing I set, but when I swipe, the screen does not respond. I do see the log statement in pagerState.pageCount is called with the correct number of pages.

With the previous versions of the Compose BoM and navigation library, where the pageCount was passed directly to the HorizontalPager, I did not have this issue.

Who can help me get my pager working again?

0

There are 0 best solutions below