Created
November 30, 2022 16:46
-
-
Save niusounds/847a12a22bef4383904cdd1796b419c7 to your computer and use it in GitHub Desktop.
Carousel (auto scrolling & manual scrolling & infinite scrolling horizontal pager) with accompanist pager https://google.github.io/accompanist/pager/
This file contains hidden or 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.interaction.collectIsDraggedAsState | |
import androidx.compose.foundation.layout.Box | |
import androidx.compose.foundation.layout.PaddingValues | |
import androidx.compose.foundation.layout.padding | |
import androidx.compose.runtime.Composable | |
import androidx.compose.runtime.LaunchedEffect | |
import androidx.compose.runtime.getValue | |
import androidx.compose.ui.Alignment | |
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.PagerScope | |
import com.google.accompanist.pager.rememberPagerState | |
import kotlinx.coroutines.delay | |
@OptIn(ExperimentalPagerApi::class) | |
@Composable | |
fun Carousel( | |
pageCount: Int, | |
modifier: Modifier = Modifier, | |
contentPadding: PaddingValues = PaddingValues(0.dp), | |
intervalMs: Long = 0, | |
content: @Composable PagerScope.(page: Int) -> Unit, | |
) { | |
val actualPageCount = Int.MAX_VALUE | |
val startIndex = actualPageCount / 2 | |
val state = rememberPagerState(initialPage = startIndex) | |
val isDragged by state.interactionSource.collectIsDraggedAsState() | |
if (!isDragged && intervalMs > 0) { | |
LaunchedEffect(Unit) { | |
while (true) { | |
delay(intervalMs) | |
state.animateScrollToPage(page = state.currentPage + 1) | |
} | |
} | |
} | |
Box( | |
modifier = modifier, | |
) { | |
HorizontalPager( | |
count = actualPageCount, | |
state = state, | |
contentPadding = contentPadding, | |
) { index -> | |
val virtualPage = (index - startIndex).floorMod(pageCount) | |
content(virtualPage) | |
} | |
HorizontalPagerIndicator( | |
pagerState = state, | |
pageIndexMapping = { (it - startIndex).floorMod(pageCount) }, | |
pageCount = pageCount, | |
modifier = Modifier | |
.align(Alignment.BottomCenter) | |
.padding(16.dp) | |
) | |
} | |
} | |
private fun Int.floorMod(other: Int): Int = when (other) { | |
0 -> this | |
else -> this - floorDiv(other) * other | |
} |
This file contains hidden or 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
Carousel( | |
pageCount = colors.size, | |
intervalMs = 3000, | |
) { index -> | |
Box( | |
Modifier | |
.fillMaxSize() | |
.background(colors[index]) | |
) { | |
Text(text = "page $index", Modifier.align(Alignment.Center)) | |
} | |
} |
Author
niusounds
commented
Nov 30, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment