androidx.compose.foundation.pager

HorizontalPager

Composable function · from org.jetbrains.compose.foundation:foundation:1.12.2

A HorizontalPager shows one page at a time; the user swipes left and right to change pages. Use it for onboarding screens, a photo viewer, or content under tabs.

val pagerState = rememberPagerState(pageCount = { 3 })
HorizontalPager(state = pagerState) { page ->
    Text("Page ${page + 1}")
}

Like a lazy list, it only creates the pages near the current one.

Read the current page with pagerState.currentPage. Change it from code with pagerState.animateScrollToPage(2), a suspend function. To link it with tabs, select the tab of pagerState.currentPage, and scroll the pager in the tab's onClick.

To show a part of the next page, set contentPadding = PaddingValues(horizontal = 32.dp) or a smaller pageSize.

Parameters

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

state: PagerStateRequired

The current page and the number of pages. Create it with rememberPagerState.

modifier: ModifierOptional

Default: Modifier

Changes size, padding, or position. See the Modifier guide.

contentPadding: PaddingValuesOptional

Default: PaddingValues(0.dp)

Space around the pages. Lets the neighbor pages peek in at the sides.

pageSize: PageSizeOptional

Default: PageSize.Fill

How big each page is. PageSize.Fill (default) fills the pager; PageSize.Fixed(200.dp) shows several pages at once.

beyondViewportPageCount: IntOptional

Default: PagerDefaults.BeyondViewportPageCount

How many pages outside the screen are created in advance. More makes swiping smoother but uses more memory.

pageSpacing: DpOptional

Default: 0.dp

The space between pages.

verticalAlignment: Alignment.VerticalOptional

Default: Alignment.CenterVertically

How pages lower than the pager line up.

flingBehavior: TargetedFlingBehaviorOptional

Default: PagerDefaults.flingBehavior(state = state)

How the pager snaps to a page after a swipe.

userScrollEnabled: BooleanOptional

Default: true

Set to false to stop swiping. Change pages from code instead.

reverseLayout: BooleanOptional

Default: false

Set to true to go the other way.

key: ((index: Int) -> Any)?Optional

Default: null

A stable key per page, so pages keep their state when the list of pages changes.

pageNestedScrollConnection: NestedScrollConnectionOptional

Default: PagerDefaults.pageNestedScrollConnection(state, Orientation.Horizontal)

How scrolling inside a page works together with the pager. Rarely changed.

snapPosition: SnapPositionOptional

Default: SnapPosition.Start

Where a page snaps to: start, center or end.

overscrollEffect: OverscrollEffect?OptionalSince 1.8

Default: rememberOverscrollEffect()

What shows when the user swipes past the last page. Pass null for none.

pageContent: @Composable PagerScope.(page: Int) -> UnitRequired

What each page shows. Gets the page number, starting at 0.

Pager types