Skip to content

Commit bd5988f

Browse files
authored
Add horizontal pager (#650)
1 parent 73156c1 commit bd5988f

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

wear/src/main/java/com/example/wear/snippets/m3/MainActivity.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import androidx.activity.ComponentActivity
2121
import androidx.activity.compose.setContent
2222
import androidx.compose.runtime.Composable
2323
import com.example.wear.snippets.m3.list.ComposeList
24+
import com.example.wear.snippets.m3.pager.HorizontalPager
2425

2526
class MainActivity : ComponentActivity() {
2627
override fun onCreate(savedInstanceState: Bundle?) {
@@ -35,5 +36,5 @@ class MainActivity : ComponentActivity() {
3536
@Composable
3637
fun WearApp() {
3738
// insert here the snippet you want to test
38-
ComposeList()
39+
HorizontalPager()
3940
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.wear.snippets.m3.pager
18+
19+
import androidx.compose.foundation.layout.fillMaxWidth
20+
import androidx.compose.runtime.Composable
21+
import androidx.compose.ui.Modifier
22+
import androidx.wear.compose.foundation.lazy.TransformingLazyColumn
23+
import androidx.wear.compose.foundation.lazy.rememberTransformingLazyColumnState
24+
import androidx.wear.compose.foundation.pager.HorizontalPager
25+
import androidx.wear.compose.foundation.pager.rememberPagerState
26+
import androidx.wear.compose.material3.AnimatedPage
27+
import androidx.wear.compose.material3.AppScaffold
28+
import androidx.wear.compose.material3.HorizontalPagerScaffold
29+
import androidx.wear.compose.material3.ListHeader
30+
import androidx.wear.compose.material3.ScreenScaffold
31+
import androidx.wear.compose.material3.Text
32+
import com.google.android.horologist.compose.layout.ColumnItemType
33+
import com.google.android.horologist.compose.layout.rememberResponsiveColumnPadding
34+
35+
@Composable
36+
fun HorizontalPager() {
37+
// [START android_wear_horizontal_pager]
38+
AppScaffold {
39+
val pagerState = rememberPagerState(pageCount = { 10 })
40+
val columnState = rememberTransformingLazyColumnState()
41+
val contentPadding = rememberResponsiveColumnPadding(
42+
first = ColumnItemType.ListHeader,
43+
last = ColumnItemType.BodyText,
44+
)
45+
HorizontalPagerScaffold(pagerState = pagerState) {
46+
HorizontalPager(
47+
state = pagerState,
48+
) { page ->
49+
AnimatedPage(pageIndex = page, pagerState = pagerState) {
50+
ScreenScaffold(
51+
scrollState = columnState,
52+
contentPadding = contentPadding
53+
) { contentPadding ->
54+
TransformingLazyColumn(
55+
state = columnState,
56+
contentPadding = contentPadding
57+
) {
58+
item {
59+
ListHeader(
60+
modifier = Modifier.fillMaxWidth()
61+
) {
62+
Text(text = "Pager sample")
63+
}
64+
}
65+
item {
66+
if (page == 0) {
67+
Text(text = "Page #$page. Swipe right")
68+
}
69+
else{
70+
Text(text = "Page #$page. Swipe left and right")
71+
}
72+
}
73+
}
74+
}
75+
76+
}
77+
}
78+
}
79+
}
80+
// [END android_wear_horizontal_pager]
81+
}

0 commit comments

Comments
 (0)