5

Is there a way to slow down the sliding interval when auto-scrolling to next page in Accompanist Pager?

In the latest version, support for the same has been removed.

The animationSpec, initialVelocity and skipPages parameters on animateScrollToPage() have been removed. The lazy components handle this automatically.

Currently, the auto scroll to next page moves very quickly.

pager.animateScrollToPage(pageIndex)

Accompanist Pager version used: 0.22.1-rc

Ali_Waris
  • 1,944
  • 2
  • 28
  • 46
  • 1
    Consider participating to this issue on github: https://github.com/google/accompanist/issues/1261 – SVP Jul 24 '22 at 21:45

1 Answers1

0

An answer can be found here.

This animates Card components that auto-scroll forward then back to the start, in a loop.

States

val pagerState = rememberPagerState()
val isDragged by pagerState.interactionSource.collectIsDraggedAsState()
var pageSize by remember { mutableStateOf(IntSize.Zero) }
val lastIndex by remember(pagerState.currentPage) {
        derivedStateOf {pagerState.currentPage == items.size - 1 }
    }

Scroll

if (!isDragged) {
    LaunchedEffect(Unit) {
        while (true) {
            yield()
            delay(2000)
            pagerState.animateScrollBy(
                value = if (lastIndex) - (pageSize.width.toFloat() * items.size) else pageSize.width.toFloat(),
                animationSpec = tween(if (lastIndex) 2000 else 1400)
            )
        }
    }
}

pageSize is assigned with an onSizeChanged Modifier on the Card

HorizontalPager(
        count = items.size,
        state = pagerState
    ) { pageIndex ->
        Card(modifier = modifier.onSizeChanged { pageSize = it } ) { /* card content */ }
    }
SVP
  • 2,773
  • 3
  • 11
  • 14