Skip to content

Commit a0b94c0

Browse files
authored
Add auto advance pager snippets (#377)
* Add auto advance pager snippets * Apply Spotless --------- Co-authored-by: jakeroseman <jakeroseman@users.noreply.github.com>
1 parent f67b849 commit a0b94c0

File tree

3 files changed

+113
-1
lines changed

3 files changed

+113
-1
lines changed

compose/snippets/src/main/java/com/example/compose/snippets/SnippetsActivity.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import com.example.compose.snippets.graphics.BitmapFromComposableFullSnippet
5353
import com.example.compose.snippets.graphics.BrushExamplesScreen
5454
import com.example.compose.snippets.images.ImageExamplesScreen
5555
import com.example.compose.snippets.landing.LandingScreen
56+
import com.example.compose.snippets.layouts.PagerExamples
5657
import com.example.compose.snippets.navigation.Destination
5758
import com.example.compose.snippets.navigation.TopComponentsDestination
5859
import com.example.compose.snippets.ui.theme.SnippetsTheme
@@ -87,6 +88,7 @@ class SnippetsActivity : ComponentActivity() {
8788
}
8889
Destination.ShapesExamples -> ApplyPolygonAsClipImage()
8990
Destination.SharedElementExamples -> PlaceholderSizeAnimated_Demo()
91+
Destination.PagerExamples -> PagerExamples()
9092
}
9193
}
9294
}

compose/snippets/src/main/java/com/example/compose/snippets/layouts/PagerSnippets.kt

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ package com.example.compose.snippets.layouts
2020

2121
import android.util.Log
2222
import androidx.compose.foundation.Image
23+
import androidx.compose.foundation.LocalIndication
2324
import androidx.compose.foundation.background
25+
import androidx.compose.foundation.clickable
26+
import androidx.compose.foundation.interaction.MutableInteractionSource
27+
import androidx.compose.foundation.interaction.collectIsDraggedAsState
28+
import androidx.compose.foundation.interaction.collectIsPressedAsState
2429
import androidx.compose.foundation.layout.Arrangement
2530
import androidx.compose.foundation.layout.Box
2631
import androidx.compose.foundation.layout.Column
@@ -50,6 +55,8 @@ import androidx.compose.material3.TabRow
5055
import androidx.compose.material3.Text
5156
import androidx.compose.runtime.Composable
5257
import androidx.compose.runtime.LaunchedEffect
58+
import androidx.compose.runtime.getValue
59+
import androidx.compose.runtime.remember
5360
import androidx.compose.runtime.rememberCoroutineScope
5461
import androidx.compose.runtime.snapshotFlow
5562
import androidx.compose.ui.Alignment
@@ -58,13 +65,15 @@ import androidx.compose.ui.draw.clip
5865
import androidx.compose.ui.graphics.Color
5966
import androidx.compose.ui.graphics.graphicsLayer
6067
import androidx.compose.ui.layout.ContentScale
68+
import androidx.compose.ui.text.style.TextAlign
6169
import androidx.compose.ui.tooling.preview.Preview
6270
import androidx.compose.ui.unit.Density
6371
import androidx.compose.ui.unit.dp
6472
import androidx.compose.ui.util.lerp
6573
import coil.compose.rememberAsyncImagePainter
6674
import com.example.compose.snippets.util.rememberRandomSampleImageUrl
6775
import kotlin.math.absoluteValue
76+
import kotlinx.coroutines.delay
6877
import kotlinx.coroutines.launch
6978

7079
/*
@@ -83,6 +92,18 @@ import kotlinx.coroutines.launch
8392
* limitations under the License.
8493
*/
8594

95+
@Composable
96+
fun PagerExamples() {
97+
AutoAdvancePager(
98+
listOf(
99+
Color.Red,
100+
Color.Gray,
101+
Color.Green,
102+
Color.White
103+
)
104+
)
105+
}
106+
86107
@Preview
87108
@Composable
88109
fun HorizontalPagerSample() {
@@ -392,6 +413,94 @@ fun PagerIndicator() {
392413
}
393414
}
394415

416+
@Composable
417+
fun AutoAdvancePager(pageItems: List<Color>, modifier: Modifier = Modifier) {
418+
Box(modifier = Modifier.fillMaxSize()) {
419+
val pagerState = rememberPagerState(pageCount = { pageItems.size })
420+
val pagerIsDragged by pagerState.interactionSource.collectIsDraggedAsState()
421+
422+
val pageInteractionSource = remember { MutableInteractionSource() }
423+
val pageIsPressed by pageInteractionSource.collectIsPressedAsState()
424+
425+
// Stop auto-advancing when pager is dragged or one of the pages is pressed
426+
val autoAdvance = !pagerIsDragged && !pageIsPressed
427+
428+
if (autoAdvance) {
429+
LaunchedEffect(pagerState, pageInteractionSource) {
430+
while (true) {
431+
delay(2000)
432+
val nextPage = (pagerState.currentPage + 1) % pageItems.size
433+
pagerState.animateScrollToPage(nextPage)
434+
}
435+
}
436+
}
437+
438+
HorizontalPager(
439+
state = pagerState
440+
) { page ->
441+
Text(
442+
text = "Page: $page",
443+
textAlign = TextAlign.Center,
444+
modifier = modifier
445+
.fillMaxSize()
446+
.background(pageItems[page])
447+
.clickable(
448+
interactionSource = pageInteractionSource,
449+
indication = LocalIndication.current
450+
) {
451+
// Handle page click
452+
}
453+
.wrapContentSize(align = Alignment.Center)
454+
)
455+
}
456+
457+
PagerIndicator(pageItems.size, pagerState.currentPage)
458+
}
459+
}
460+
461+
@Preview
462+
@Composable
463+
private fun AutoAdvancePagerPreview() {
464+
val pageItems: List<Color> = listOf(
465+
Color.Red,
466+
Color.Gray,
467+
Color.Green,
468+
Color.White
469+
)
470+
AutoAdvancePager(pageItems = pageItems)
471+
}
472+
473+
@Composable
474+
fun PagerIndicator(pageCount: Int, currentPageIndex: Int, modifier: Modifier = Modifier) {
475+
Box(modifier = Modifier.fillMaxSize()) {
476+
Row(
477+
modifier = Modifier
478+
.wrapContentHeight()
479+
.fillMaxWidth()
480+
.align(Alignment.BottomCenter)
481+
.padding(bottom = 8.dp),
482+
horizontalArrangement = Arrangement.Center
483+
) {
484+
repeat(pageCount) { iteration ->
485+
val color = if (currentPageIndex == iteration) Color.DarkGray else Color.LightGray
486+
Box(
487+
modifier = modifier
488+
.padding(2.dp)
489+
.clip(CircleShape)
490+
.background(color)
491+
.size(16.dp)
492+
)
493+
}
494+
}
495+
}
496+
}
497+
498+
@Preview
499+
@Composable
500+
private fun PagerIndicatorPreview() {
501+
PagerIndicator(pageCount = 4, currentPageIndex = 1)
502+
}
503+
395504
// [START android_compose_pager_custom_page_size]
396505
private val threePagesPerViewport = object : PageSize {
397506
override fun Density.calculateMainAxisPageSize(

compose/snippets/src/main/java/com/example/compose/snippets/navigation/Destination.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ enum class Destination(val route: String, val title: String) {
2323
ComponentsExamples("topComponents", "Top Compose Components"),
2424
ScreenshotExample("screenshotExample", "Screenshot Examples"),
2525
ShapesExamples("shapesExamples", "Shapes Examples"),
26-
SharedElementExamples("sharedElement", "Shared elements")
26+
SharedElementExamples("sharedElement", "Shared elements"),
27+
PagerExamples("pagerExamples", "Pager examples")
2728
}
2829

2930
// Enum class for compose components navigation screen.

0 commit comments

Comments
 (0)