|
16 | 16 |
|
17 | 17 | package com.example.wear.snippets.rotary
|
18 | 18 |
|
| 19 | +import android.view.MotionEvent |
| 20 | +import androidx.compose.foundation.focusable |
| 21 | +import androidx.compose.foundation.gestures.animateScrollBy |
| 22 | +import androidx.compose.foundation.gestures.scrollBy |
| 23 | +import androidx.compose.foundation.layout.Arrangement |
| 24 | +import androidx.compose.foundation.layout.Box |
| 25 | +import androidx.compose.foundation.layout.Row |
| 26 | +import androidx.compose.foundation.layout.Spacer |
| 27 | +import androidx.compose.foundation.layout.fillMaxSize |
19 | 28 | import androidx.compose.foundation.layout.fillMaxWidth
|
| 29 | +import androidx.compose.foundation.layout.size |
| 30 | +import androidx.compose.foundation.layout.width |
20 | 31 | import androidx.compose.runtime.Composable
|
| 32 | +import androidx.compose.runtime.LaunchedEffect |
| 33 | +import androidx.compose.runtime.derivedStateOf |
| 34 | +import androidx.compose.runtime.getValue |
| 35 | +import androidx.compose.runtime.mutableIntStateOf |
| 36 | +import androidx.compose.runtime.remember |
| 37 | +import androidx.compose.runtime.rememberCoroutineScope |
| 38 | +import androidx.compose.runtime.setValue |
| 39 | +import androidx.compose.ui.Alignment |
| 40 | +import androidx.compose.ui.ExperimentalComposeUiApi |
21 | 41 | import androidx.compose.ui.Modifier
|
| 42 | +import androidx.compose.ui.focus.FocusRequester |
| 43 | +import androidx.compose.ui.focus.focusRequester |
| 44 | +import androidx.compose.ui.input.pointer.pointerInteropFilter |
| 45 | +import androidx.compose.ui.input.rotary.onRotaryScrollEvent |
| 46 | +import androidx.compose.ui.unit.dp |
| 47 | +import androidx.wear.compose.foundation.ExperimentalWearFoundationApi |
22 | 48 | import androidx.wear.compose.foundation.lazy.ScalingLazyColumn
|
23 | 49 | import androidx.wear.compose.foundation.lazy.ScalingLazyColumnDefaults
|
24 | 50 | import androidx.wear.compose.foundation.lazy.rememberScalingLazyListState
|
| 51 | +import androidx.wear.compose.foundation.rememberActiveFocusRequester |
25 | 52 | import androidx.wear.compose.material.Chip
|
26 | 53 | import androidx.wear.compose.material.ChipDefaults
|
27 | 54 | import androidx.wear.compose.material.ListHeader
|
| 55 | +import androidx.wear.compose.material.MaterialTheme |
| 56 | +import androidx.wear.compose.material.Picker |
28 | 57 | import androidx.wear.compose.material.PositionIndicator
|
29 | 58 | import androidx.wear.compose.material.Scaffold
|
30 | 59 | import androidx.wear.compose.material.Text
|
| 60 | +import androidx.wear.compose.material.rememberPickerState |
31 | 61 | import androidx.wear.compose.ui.tooling.preview.WearPreviewDevices
|
32 | 62 | import androidx.wear.compose.ui.tooling.preview.WearPreviewFontScales
|
| 63 | +import kotlinx.coroutines.launch |
33 | 64 |
|
| 65 | +@OptIn(ExperimentalWearFoundationApi::class) |
34 | 66 | @Composable
|
35 | 67 | fun ScrollableScreen() {
|
| 68 | + // This sample doesn't add a Time Text at the top of the screen. |
| 69 | + // If using Time Text, add padding to ensure content does not overlap with Time Text. |
| 70 | + // [START android_wear_rotary_input] |
| 71 | + val listState = rememberScalingLazyListState() |
| 72 | + Scaffold( |
| 73 | + positionIndicator = { |
| 74 | + PositionIndicator(scalingLazyListState = listState) |
| 75 | + } |
| 76 | + ) { |
| 77 | + |
| 78 | + val focusRequester = rememberActiveFocusRequester() |
| 79 | + val coroutineScope = rememberCoroutineScope() |
| 80 | + |
| 81 | + ScalingLazyColumn( |
| 82 | + modifier = Modifier |
| 83 | + .onRotaryScrollEvent { |
| 84 | + coroutineScope.launch { |
| 85 | + listState.scrollBy(it.verticalScrollPixels) |
| 86 | + listState.animateScrollBy(0f) |
| 87 | + } |
| 88 | + true |
| 89 | + } |
| 90 | + .focusRequester(focusRequester) |
| 91 | + .focusable() |
| 92 | + .fillMaxSize(), |
| 93 | + state = listState |
| 94 | + ) { |
| 95 | + // Content goes here |
| 96 | + // [START_EXCLUDE] |
| 97 | + items(count = 5) { |
| 98 | + Chip(onClick = { }, label = { Text("Item #$it") }) |
| 99 | + } |
| 100 | + // [END_EXCLUDE] |
| 101 | + } |
| 102 | + } |
| 103 | + // [END android_wear_rotary_input] |
| 104 | +} |
| 105 | + |
| 106 | +@OptIn(ExperimentalComposeUiApi::class) |
| 107 | +@Composable |
| 108 | +fun TimePicker() { |
| 109 | + val textStyle = MaterialTheme.typography.display1 |
| 110 | + |
| 111 | + // [START android_wear_rotary_input_picker] |
| 112 | + var selectedColumn by remember { mutableIntStateOf(0) } |
| 113 | + |
| 114 | + val hoursFocusRequester = remember { FocusRequester() } |
| 115 | + val minutesRequester = remember { FocusRequester() } |
| 116 | + // [START_EXCLUDE] |
| 117 | + val coroutineScope = rememberCoroutineScope() |
| 118 | + |
| 119 | + @Composable |
| 120 | + fun Option(column: Int, text: String) = Box(modifier = Modifier.fillMaxSize()) { |
| 121 | + Text( |
| 122 | + text = text, style = textStyle, |
| 123 | + color = if (selectedColumn == column) MaterialTheme.colors.secondary |
| 124 | + else MaterialTheme.colors.onBackground, |
| 125 | + modifier = Modifier |
| 126 | + .pointerInteropFilter { |
| 127 | + if (it.action == MotionEvent.ACTION_DOWN) selectedColumn = column |
| 128 | + true |
| 129 | + } |
| 130 | + ) |
| 131 | + } |
| 132 | + // [END_EXCLUDE] |
| 133 | + Scaffold(modifier = Modifier.fillMaxSize()) { |
| 134 | + Row( |
| 135 | + // [START_EXCLUDE] |
| 136 | + modifier = Modifier.fillMaxSize(), |
| 137 | + verticalAlignment = Alignment.CenterVertically, |
| 138 | + horizontalArrangement = Arrangement.Center, |
| 139 | + // [END_EXCLUDE] |
| 140 | + // ... |
| 141 | + ) { |
| 142 | + // [START_EXCLUDE] |
| 143 | + val hourState = rememberPickerState( |
| 144 | + initialNumberOfOptions = 12, |
| 145 | + initiallySelectedOption = 5 |
| 146 | + ) |
| 147 | + val hourContentDescription by remember { |
| 148 | + derivedStateOf { "${hourState.selectedOption + 1 } hours" } |
| 149 | + } |
| 150 | + // [END_EXCLUDE] |
| 151 | + Picker( |
| 152 | + readOnly = selectedColumn != 0, |
| 153 | + modifier = Modifier.size(64.dp, 100.dp) |
| 154 | + .onRotaryScrollEvent { |
| 155 | + coroutineScope.launch { |
| 156 | + hourState.scrollBy(it.verticalScrollPixels) |
| 157 | + } |
| 158 | + true |
| 159 | + } |
| 160 | + .focusRequester(hoursFocusRequester) |
| 161 | + .focusable(), |
| 162 | + onSelected = { selectedColumn = 0 }, |
| 163 | + // ... |
| 164 | + // [START_EXCLUDE] |
| 165 | + state = hourState, |
| 166 | + contentDescription = hourContentDescription, |
| 167 | + option = { hour: Int -> Option(0, "%2d".format(hour + 1)) } |
| 168 | + // [END_EXCLUDE] |
| 169 | + ) |
| 170 | + // [START_EXCLUDE] |
| 171 | + Spacer(Modifier.width(8.dp)) |
| 172 | + Text(text = ":", style = textStyle, color = MaterialTheme.colors.onBackground) |
| 173 | + Spacer(Modifier.width(8.dp)) |
| 174 | + val minuteState = |
| 175 | + rememberPickerState(initialNumberOfOptions = 60, initiallySelectedOption = 0) |
| 176 | + val minuteContentDescription by remember { |
| 177 | + derivedStateOf { "${minuteState.selectedOption} minutes" } |
| 178 | + } |
| 179 | + // [END_EXCLUDE] |
| 180 | + Picker( |
| 181 | + readOnly = selectedColumn != 1, |
| 182 | + modifier = Modifier.size(64.dp, 100.dp) |
| 183 | + .onRotaryScrollEvent { |
| 184 | + coroutineScope.launch { |
| 185 | + minuteState.scrollBy(it.verticalScrollPixels) |
| 186 | + } |
| 187 | + true |
| 188 | + } |
| 189 | + .focusRequester(minutesRequester) |
| 190 | + .focusable(), |
| 191 | + onSelected = { selectedColumn = 1 }, |
| 192 | + // ... |
| 193 | + // [START_EXCLUDE] |
| 194 | + state = minuteState, |
| 195 | + contentDescription = minuteContentDescription, |
| 196 | + option = { minute: Int -> Option(1, "%02d".format(minute)) } |
| 197 | + // [END_EXCLUDE] |
| 198 | + ) |
| 199 | + LaunchedEffect(selectedColumn) { |
| 200 | + listOf( |
| 201 | + hoursFocusRequester, |
| 202 | + minutesRequester |
| 203 | + )[selectedColumn] |
| 204 | + .requestFocus() |
| 205 | + } |
| 206 | + } |
| 207 | + } |
| 208 | + // [END android_wear_rotary_input_picker] |
| 209 | +} |
| 210 | + |
| 211 | +@Composable |
| 212 | +fun SnapScrollableScreen() { |
36 | 213 | // This sample doesn't add a Time Text at the top of the screen.
|
37 | 214 | // If using Time Text, add padding to ensure content does not overlap with Time Text.
|
38 | 215 | // [START android_wear_rotary_input_snap_fling]
|
|
0 commit comments