HorizontalPager

fun HorizontalPager(count: Int, modifier: Modifier = Modifier, state: PagerState = rememberPagerState(), reverseLayout: Boolean = false, itemSpacing: Dp = 0.dp, contentPadding: PaddingValues = PaddingValues(0.dp), verticalAlignment: Alignment.Vertical = Alignment.CenterVertically, flingBehavior: FlingBehavior = PagerDefaults.flingBehavior( state = state, endContentPadding = contentPadding.calculateEndPadding(LayoutDirection.Ltr), ), key: (page: Int) -> Any? = null, userScrollEnabled: Boolean = true, content: @Composable() PagerScope.(page: Int) -> Unit)

A horizontally scrolling layout that allows users to flip between items to the left and right.

Samples

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Tab
import androidx.compose.material.TabRow
import androidx.compose.material.TabRowDefaults
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.google.accompanist.pager.ExperimentalPagerApi
import com.google.accompanist.pager.HorizontalPager
import com.google.accompanist.pager.HorizontalPagerIndicator
import com.google.accompanist.pager.VerticalPager
import com.google.accompanist.pager.VerticalPagerIndicator
import com.google.accompanist.pager.pagerTabIndicatorOffset
import com.google.accompanist.pager.rememberPagerState
import kotlinx.coroutines.flow.collect
fun main() { 
   //sampleStart 
   // Display 10 items
HorizontalPager(count = 10) { page ->
    // Our page content
    Text(
        text = "Page: $page",
        modifier = Modifier.fillMaxWidth()
    )
} 
   //sampleEnd
}

Parameters

count

the number of pages.

modifier

the modifier to apply to this layout.

state

the state object to be used to control or observe the pager's state.

reverseLayout

reverse the direction of scrolling and layout, when true items will be composed from the end to the start and PagerState.currentPage == 0 will mean the first item is located at the end.

itemSpacing

horizontal spacing to add between items.

flingBehavior

logic describing fling behavior.

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.

userScrollEnabled

whether the scrolling via the user gestures or accessibility actions is allowed. You can still scroll programmatically using the state even when it is disabled.

content

a block which describes the content. Inside this block you can reference PagerScope.currentPage and other properties in PagerScope.

Sources

androidJvm source
Link copied to clipboard