Jetpack Compose HorizontalPager offer by current page

887 Views Asked by At

I wanna implement this design: I have 2 pages, the second page's left side part visible on the first page, and the first page's right side on the second.

I played with contentPadding, pageSpacing, modifier.graphicsLayer and so on, but nothing worked. What is the correct setup? Thank you

enter image description here

enter image description here

2

There are 2 best solutions below

1
Jan Bína On BEST ANSWER

You can make use of custom PageSize. For example, we use this FillPageSize, where you can specify the fraction of available space that each page takes:

class FillPageSize(
    private val fraction: Float = 1F,
): PageSize {

    override fun Density.calculateMainAxisPageSize(availableSpace: Int, pageSpacing: Int): Int {
        return (availableSpace * fraction).roundToInt()
    }
}

Usage is simple:

HorizontalPager(
    pageSize = FillPageSize(.8F)
) { ... }

You can easily modify that to subtract specified amount of pixels from available space or something.

1
Name On

You can change contentPadding for this.

val pagerState = rememberPagerState(0) { 2 }
var contentPaddingValues by remember { mutableStateOf(PaddingValues(end = 70.dp)) }

LaunchedEffect(pagerState.currentPage) {
    contentPaddingValues = if (pagerState.currentPage == 0) PaddingValues(end = 70.dp) else PaddingValues(start = 70.dp)
}

HorizontalPager(
    state = pagerState,
    contentPadding = contentPaddingValues,
) {
    // your content
}

enter image description here