New Compose Multiplatform components arrived on Composables UICheck it out →

VerticalPager

A Pager that scrolls vertically. Pages are lazily placed in accordance to the available viewport size. By definition, pages in a [Pager] have the same size, defined by [pageSize] and use a snap animation (provided by [flingBehavior] to scroll pages into a specific position). You can use [beyondViewportPageCount] to place more pages before and after the visible pages.

Last updated:

Installation

dependencies {
   implementation("androidx.compose.foundation:foundation:1.7.0-beta03")
}

Overloads

@Composable
fun VerticalPager(
    state: PagerState,
    modifier: Modifier = Modifier,
    contentPadding: PaddingValues = PaddingValues(0.dp),
    pageSize: PageSize = PageSize.Fill,
    beyondViewportPageCount: Int = PagerDefaults.BeyondViewportPageCount,
    pageSpacing: Dp = 0.dp,
    horizontalAlignment: Alignment.Horizontal = Alignment.CenterHorizontally,
    flingBehavior: TargetedFlingBehavior = PagerDefaults.flingBehavior(state = state),
    userScrollEnabled: Boolean = true,
    reverseLayout: Boolean = false,
    key: ((index: Int) -> Any)? = null,
    pageNestedScrollConnection: NestedScrollConnection = PagerDefaults.pageNestedScrollConnection(
        state,
        Orientation.Vertical
    ),
    snapPosition: SnapPosition = SnapPosition.Start,
    pageContent: @Composable PagerScope.(page: Int) -> Unit
)

Parameters

namedescription
stateThe state to control this pager
modifierA modifier instance to be apply to this Pager outer layout
contentPaddinga padding around the whole content. This will add padding for the content after it has been clipped, which is not possible via [modifier] param. You can use it to add a padding before the first page or after the last one. Use [pageSpacing] to add spacing between the pages.
pageSizeUse this to change how the pages will look like inside this pager.
beyondViewportPageCountPages to compose and layout before and after the list of visible pages. Note: Be aware that using a large value for [beyondViewportPageCount] will cause a lot of pages to be composed, measured and placed which will defeat the purpose of using lazy loading. This should be used as an optimization to pre-load a couple of pages before and after the visible ones. This does not include the pages automatically composed and laid out by the pre-fetcher in * the direction of the scroll during scroll events.
pageSpacingThe amount of space to be used to separate the pages in this Pager
horizontalAlignmentHow pages are aligned horizontally in this Pager.
flingBehaviorThe [TargetedFlingBehavior] to be used for post scroll gestures.
userScrollEnabledwhether the scrolling via the user gestures or accessibility actions is allowed. You can still scroll programmatically using [PagerState.scroll] even when it is disabled.
reverseLayoutreverse the direction of scrolling and layout.
keya stable and unique key representing the item. When you specify the key the scroll position will be maintained based on the key, which means if you add/remove items before the current visible item the item with the given key will be kept as the first visible one. If null is passed the position in the list will represent the key.
pageNestedScrollConnectionA [NestedScrollConnection] that dictates how this [Pager] behaves with nested lists. The default behavior will see [Pager] to consume all nested deltas.
snapPositionThe calculation of how this Pager will perform snapping of Pages. Use this to provide different settling to different positions in the layout. This is used by [Pager] as a way to calculate [PagerState.currentPage], currentPage is the page closest to the snap position in the layout (e.g. if the snap position is the start of the layout, then currentPage will be the page closest to that).
pageContentThis Pager's page Composable.

Code Example

SimpleVerticalPagerSample

@Composable
@Sampled
fun SimpleVerticalPagerSample() {
    // Creates a 1-pager/viewport vertical pager with single page snapping
    val state = rememberPagerState { 10 }
    VerticalPager(
        state = state,
        modifier = Modifier.fillMaxSize()
    ) { page ->
        Box(
            modifier = Modifier
                .padding(10.dp)
                .background(Color.Blue)
                .fillMaxWidth()
                .aspectRatio(1f),
            contentAlignment = Alignment.Center
        ) {
            Text(text = page.toString(), fontSize = 32.sp)
        }
    }
}