A library which provides paging layouts for Jetpack Compose. If you've used Android's
ViewPager
before, it has similar properties.
Warning
This library is deprecated, with official pager support in
androidx.compose.foundation.pager
.
The original documentation is below the migration guide.
Make sure you are using Compose 1.4.0+ before attempting to migrate to
androidx.compose.foundation.pager
.
Change
com.google.accompanist.pager.HorizontalPager
to
androidx.compose.foundation.pager.HorizontalPager
, and the same for
com.google.accompanist.pager.VerticalPager
to change to
androidx.compose.foundation.pager.VerticalPager
Change
count
variable to
pageCount
.
Change
itemSpacing
parameter to
pageSpacing
.
Change any usages of
rememberPagerState()
to
androidx.compose.foundation.pager.rememberPagerState()
For more mappings - see the migration table below.
Run your changes on device and check to see if there are any differences.
One thing to note is that there is a new parameter on
androidx.compose.foundation.Pager
, for
pageSize
, by default this
uses a
PageSize.Fill
, but can also be changed to use a fixed size, like
PageSize.Fixed(200.dp)
for a fixed size paging.
The following is a mapping of the pager classes from accompanist to androidx.compose:
accompanist/pager
androidx.compose.foundation
calculateCurrentOffsetForPage
Use
(pagerState.currentPage - page) + pagerState.currentPageOffsetFraction
PagerState#currentPageOffset
PagerState#currentPageOffsetFraction
Modifier.pagerTabIndicatorOffset()
Implement it yourself, or still include and use
accompanist-pager-indicators
, it now supports
androidx.compose.foundation.pager.PagerState
HorizontalPagerIndicator
Implement it yourself, or still include and use
accompanist-pager-indicators
, it now supports
androidx.compose.foundation.pager.HorizontalPager
by explicitly providing a
pageCount
param to the
HorizontalPagerIndicator
method
VerticalPagerIndicator
Implement it yourself, or still include and use
accompanist-pager-indicators
, it now supports
androidx.compose.foundation.pager.HorizontalPager
by explicitly providing a
pageCount
param to the
HorizontalPagerIndicator
method
PagerDefaults.flingBehavior()
androidx.compose.foundation.pager.PagerDefaults.flingBehavior()
The biggest change is that
HorizontalPager
and
VerticalPager
's number of pages is now called
pageCount
instead of
count
.
If you want to jump to a specific page, you either call call pagerState.scrollToPage(index) or pagerState.animateScrollToPage(index) method in a CoroutineScope.
valpagerState=rememberPagerState()HorizontalPager(count=10,state=pagerState){page->// ...page content// Later, scroll to page 2scope.launch{pagerState.scrollToPage(2)
Pages in both HorizontalPager and VerticalPager are lazily composed and laid-out as required by the layout. As the user scrolls through pages, any pages which are no longer required are removed from the content.
Under the covers, HorizontalPager use LazyRow, and VerticalPager uses LazyColumn.
Similar effects for VerticalPager can be achieved by setting the top and bottom values. The value 32.dp is only used
here as an example, you can set each of the padding dimensions to whatever value you wish.
Your browser does not support the video tag.
Item effects demo
The scope provided to your pager content allows apps to easily reference the currentPage and currentPageOffset. The effects can then be calculated using those values. We provide the calculateCurrentOffsetForPage() extension functions to support calculation of the 'offset' for a given page:
importcom.google.accompanist.pager.calculateCurrentOffsetForPageHorizontalPager(count=4){page->Card(Modifier.graphicsLayer{// Calculate the absolute offset for the current page from the// scroll position. We use the absolute value which allows us to mirror// any effects for both directionsvalpageOffset=calculateCurrentOffsetForPage(page).absoluteValue// We animate the scaleX + scaleY, between 85% and 100%lerp(start=0.85f,stop=1f,fraction=1f-pageOffset.coerceIn(0f,1f)).also{scale->scaleX=scalescaleY=scale// We animate the alpha, between 50% and 100%alpha=lerp(start=0.5f,stop=1f,fraction=1f-pageOffset.coerceIn(0f,1f)// Card content
The PagerState.currentPage property is updated whenever the selected page changes. You can use the snapshotFlow function to observe changes in a flow:
valpagerState=rememberPagerState()LaunchedEffect(pagerState){// Collect from the pager state a snapshotFlow reading the currentPagesnapshotFlow{pagerState.currentPage}.collect{page->AnalyticsService.sendPageSelectedEvent(page)VerticalPager(count=10,state=pagerState,){page->Text(text="Page: $page")
We also publish a sibling library called pager-indicators which provides some simple indicator composables for use with HorizontalPager and VerticalPager.
Your browser does not support the video tag.
Pager indicators demo
Your browser does not support the video tag.
HorizontalPager + TabRow
Provided in the pager-indicators library is a modifier which can be used on a tab indicator like so:
valpagerState=rememberPagerState()TabRow(// Our selected tab is our current pageselectedTabIndex=pagerState.currentPage,// Override the indicator, using the provided pagerTabIndicatorOffset modifierindicator={tabPositions->TabRowDefaults.Indicator(Modifier.pagerTabIndicatorOffset(pagerState,tabPositions)// Add tabs for all of our pagespages.forEachIndexed{index,title->Tab(text={Text(title)},selected=pagerState.currentPage==index,onClick={/* TODO */},HorizontalPager(count=pages.size,state=pagerState,){page->// TODO: page content
In v0.19.0 both HorizontalPager and VerticalPager were re-written to be based on LazyRow and LazyColumn respectively. As part of this change, a number of feature and API changes were made:
The horizontalAlignment parameter on HorizontalPager, and the verticalAlignment parameter on VerticalPager have been removed. A similar effect can be implemented with an appropriate content padding (see above).
The infiniteLooping parameter and feature have been removed. A sample demonstrating how to achieve this effect can be found here.
The offscreenLimit parameter has been removed. We no longer have control of what items are laid out 'off screen'.
The dragEnabled parameter has removed.
PagerScope (the page item scope) no longer implements BoxScope.
repositories{mavenCentral()dependencies{implementation"com.google.accompanist:accompanist-pager:<version>"// If using indicators, also depend onimplementation"com.google.accompanist:accompanist-pager-indicators:<version>"
Snapshots of the current development version of this library are available, which track the latest commit. See here for more information on how to use them.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.