androidx.compose.foundation.pager
PagerState
Abstract class · from org.jetbrains.compose.foundation:foundation:1.12.2
No hand-written docs yet.
The state that can be used to control VerticalPager and HorizontalPager
abstract class PagerState : ScrollableState
Is a
How to get one
Create it
PagerState(currentPage: Int, currentPageOffsetFraction: Float, pageCount: () -> Int)
Ready-made values and functions that return it
Used by
Parameters that take a PagerState.
HorizontalPager(state)VerticalPager(state)PagerDefaults.flingBehavior(state)PagerDefaults.pageNestedScrollConnection(state)
Properties
pageCount
val pageCount: Int
The total amount of pages present in this pager. The source of this data should be observable.
layoutInfo
val layoutInfo: PagerLayoutInfo
A PagerLayoutInfo that contains useful information about the Pager's last layout pass. For instance, you can query which pages are currently visible in the layout.
interactionSource
val interactionSource: InteractionSource
InteractionSource that will be used to dispatch drag events when this list is being dragged. If you want to know whether the fling (or animated scroll) is in progress, use isScrollInProgress.
currentPage
val currentPage: Int
The page that sits closest to the snapped position. This is an observable value and will change as the pager scrolls either by gesture or animation.
settledPage
val settledPage
The page that is currently "settled". This is an animation/gesture unaware page in the sense that it will not be updated while the pages are being scrolled, but rather when the animation/scroll settles.
targetPage
val targetPage: Int
The page this Pager intends to settle to. During fling or animated scroll (from animateScrollToPage this will represent the page this pager intends to settle to. When no scroll is ongoing, this will be equal to currentPage.
currentPageOffsetFraction
val currentPageOffsetFraction: Float
Indicates how far the current page is to the snapped position, this will vary from -0.5 (page is offset towards the start of the layout) to 0.5 (page is offset towards the end of the layout). This is 0.0 if the currentPage is in the snapped position. The value will flip once the current page changes.
Functions
scrollToPage
fun scrollToPage(
page: Int,
pageOffsetFraction: Float = 0f,
)
Scroll (jump immediately) to a given page.
updateCurrentPage
fun ScrollScope.updateCurrentPage(
page: Int,
pageOffsetFraction: Float = 0.0f,
)
Jump immediately to a given page with a given pageOffsetFraction inside a ScrollScope. Use this method to create custom animated scrolling experiences. This will update the value of currentPage and currentPageOffsetFraction immediately, but can only be used inside a ScrollScope, use scroll to gain access to a ScrollScope.
updateTargetPage
fun ScrollScope.updateTargetPage(targetPage: Int)
Used to update targetPage during a programmatic scroll operation. This can only be called inside a ScrollScope and should be called anytime a custom scroll (through scroll) is executed in order to correctly update targetPage. This will not move the pages and it's still the responsibility of the caller to call ScrollScope.scrollBy in order to actually get to targetPage. By the end of the scroll block, when the Pager is no longer scrolling targetPage will assume the value of currentPage.
requestScrollToPage
fun requestScrollToPage(
page: Int,
pageOffsetFraction: Float = 0.0f,
)
Requests the page to be at the snapped position during the next remeasure, offset by pageOffsetFraction, and schedules a remeasure.
animateScrollToPage
fun animateScrollToPage(
page: Int,
pageOffsetFraction: Float = 0f,
animationSpec: AnimationSpec<Float> = spring(),
)
Scroll animate to a given page's closest snap position. If the page is too far away from currentPage we will not compose all pages in the way. We will pre-jump to a nearer page, compose and animate the rest of the pages until page.
getOffsetDistanceInPages
fun getOffsetDistanceInPages(page: Int): Float
An utility function to help to calculate a given page's offset. This is an offset that represents how far page is from the settled position (represented by currentPage offset). The difference here is that currentPageOffsetFraction is a value between -0.5 and 0.5 and the value calculated by this function can be larger than these numbers if page is different than currentPage.
Library docs
The state that can be used to control VerticalPager and HorizontalPager