Created
August 26, 2023 08:41
-
-
Save gold24park/34598641f28f31e5b79f27f35092640b to your computer and use it in GitHub Desktop.
jetpack compose infinite pager with right order
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import androidx.compose.foundation.ExperimentalFoundationApi | |
import androidx.compose.foundation.background | |
import androidx.compose.foundation.layout.Box | |
import androidx.compose.foundation.layout.fillMaxSize | |
import androidx.compose.foundation.layout.height | |
import androidx.compose.foundation.pager.HorizontalPager | |
import androidx.compose.foundation.pager.PagerState | |
import androidx.compose.foundation.pager.rememberPagerState | |
import androidx.compose.foundation.shape.RoundedCornerShape | |
import androidx.compose.material3.MaterialTheme | |
import androidx.compose.material3.Surface | |
import androidx.compose.material3.Text | |
import androidx.compose.runtime.Composable | |
import androidx.compose.ui.Alignment | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.graphics.Color | |
import androidx.compose.ui.tooling.preview.Preview | |
import androidx.compose.ui.unit.dp | |
@OptIn(ExperimentalFoundationApi::class) | |
@Composable | |
fun TestScreen() { | |
val actualPageCount = 3 | |
val pageCount = Int.MAX_VALUE | |
val maxNumOfRounds = Int.MAX_VALUE / actualPageCount | |
val pagerState = rememberPagerState( | |
initialPage = (maxNumOfRounds / 2) * actualPageCount | |
) | |
HorizontalPager( | |
modifier = Modifier | |
.fillMaxSize() | |
.background(Color.LightGray), | |
pageCount = pageCount, | |
state = pagerState, | |
pageSpacing = 10.dp, | |
) { page -> | |
Box( | |
modifier = Modifier.fillMaxSize() | |
) { | |
Card( | |
page = page % actualPageCount, | |
modifier = Modifier | |
.align(Alignment.Center) | |
.height(300.dp) | |
) | |
} | |
} | |
} | |
@Composable | |
fun Card(page: Int, modifier: Modifier = Modifier) { | |
Surface( | |
modifier = modifier, | |
color = Color.DarkGray, | |
shape = RoundedCornerShape(16.dp) | |
) { | |
Box(modifier = Modifier.fillMaxSize()) { | |
Text( | |
text = "Page $page", | |
modifier = Modifier.align(Alignment.Center), | |
style = MaterialTheme.typography.headlineLarge, | |
color = Color.White | |
) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this help me to do an infinite pager with my room db, thanks youuuuu