VerticalPager
Common
Component in Compose Foundation
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.
If you need snapping with pages of different size, you can use a [snapFlingBehavior] with a [SnapLayoutInfoProvider] adapted to a LazyList.
Last updated:
Installation
dependencies {
implementation("androidx.compose.foundation:foundation:1.8.0-alpha04")
}
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
name | description |
---|---|
state | The state to control this pager |
modifier | A modifier instance to be apply to this Pager outer layout |
contentPadding | a 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. |
pageSize | Use this to change how the pages will look like inside this pager. |
beyondViewportPageCount | Pages 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. |
pageSpacing | The amount of space to be used to separate the pages in this Pager |
horizontalAlignment | How pages are aligned horizontally in this Pager. |
flingBehavior | The [TargetedFlingBehavior] to be used for post scroll gestures. |
userScrollEnabled | whether the scrolling via the user gestures or accessibility actions is allowed. You can still scroll programmatically using [PagerState.scroll] even when it is disabled. |
reverseLayout | reverse the direction of scrolling and layout. |
key | a 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. |
pageNestedScrollConnection | A [NestedScrollConnection] that dictates how this [Pager] behaves with nested lists. The default behavior will see [Pager] to consume all nested deltas. |
snapPosition | The 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). |
pageContent | This Pager's page Composable. |
Code Example
SimpleVerticalPagerSample
@Composable
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)
}
}
}