|
| 1 | +/* |
| 2 | + * Copyright 2023 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.compose.snippets.adaptivelayouts |
| 18 | + |
| 19 | +import androidx.compose.foundation.background |
| 20 | +import androidx.compose.foundation.clickable |
| 21 | +import androidx.compose.foundation.layout.Column |
| 22 | +import androidx.compose.foundation.layout.Spacer |
| 23 | +import androidx.compose.foundation.layout.fillMaxSize |
| 24 | +import androidx.compose.foundation.layout.padding |
| 25 | +import androidx.compose.foundation.layout.size |
| 26 | +import androidx.compose.foundation.lazy.LazyColumn |
| 27 | +import androidx.compose.material3.Card |
| 28 | +import androidx.compose.material3.ListItem |
| 29 | +import androidx.compose.material3.Text |
| 30 | +import androidx.compose.material3.adaptive.ExperimentalMaterial3AdaptiveApi |
| 31 | +import androidx.compose.material3.adaptive.ListDetailPaneScaffold |
| 32 | +import androidx.compose.material3.adaptive.ListDetailPaneScaffoldRole |
| 33 | +import androidx.compose.material3.adaptive.rememberListDetailPaneScaffoldState |
| 34 | +import androidx.compose.runtime.Composable |
| 35 | +import androidx.compose.runtime.getValue |
| 36 | +import androidx.compose.runtime.mutableStateOf |
| 37 | +import androidx.compose.runtime.saveable.rememberSaveable |
| 38 | +import androidx.compose.runtime.setValue |
| 39 | +import androidx.compose.ui.Modifier |
| 40 | +import androidx.compose.ui.graphics.Color |
| 41 | +import androidx.compose.ui.tooling.preview.Preview |
| 42 | +import androidx.compose.ui.unit.dp |
| 43 | +import androidx.compose.ui.unit.sp |
| 44 | + |
| 45 | +@OptIn(ExperimentalMaterial3AdaptiveApi::class) |
| 46 | +@Composable |
| 47 | +fun SampleListDetailPaneScaffoldParts() { |
| 48 | + // [START android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_part02] |
| 49 | + val state = rememberListDetailPaneScaffoldState() |
| 50 | + // [END android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_part02] |
| 51 | + |
| 52 | + // [START android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_part01] |
| 53 | + var selectedItem: MyItem? by rememberSaveable { mutableStateOf(null) } |
| 54 | + // [END android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_part01] |
| 55 | + |
| 56 | + // [START android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_part03] |
| 57 | + ListDetailPaneScaffold( |
| 58 | + scaffoldState = state, |
| 59 | + /* ... */ |
| 60 | + // [START_EXCLUDE] |
| 61 | + listPane = {}, |
| 62 | + detailPane = {}, |
| 63 | + // [END_EXCLUDE] |
| 64 | + ) |
| 65 | + // [END android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_part03] |
| 66 | + |
| 67 | + // [START android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_part04] |
| 68 | + ListDetailPaneScaffold( |
| 69 | + scaffoldState = state, |
| 70 | + listPane = { |
| 71 | + MyList( |
| 72 | + onItemClick = { id -> |
| 73 | + // Set current item |
| 74 | + selectedItem = id |
| 75 | + // Switch focus to details pane |
| 76 | + state.navigateTo(ListDetailPaneScaffoldRole.Detail) |
| 77 | + } |
| 78 | + ) |
| 79 | + }, |
| 80 | + /* ... */ |
| 81 | + // [START_EXCLUDE] |
| 82 | + detailPane = {}, |
| 83 | + // [END_EXCLUDE] |
| 84 | + ) |
| 85 | + // [END android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_part04] |
| 86 | + |
| 87 | + // [START android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_part05] |
| 88 | + ListDetailPaneScaffold( |
| 89 | + scaffoldState = state, |
| 90 | + listPane = /* ... */ |
| 91 | + // [START_EXCLUDE] |
| 92 | + {}, |
| 93 | + // [END_EXCLUDE] |
| 94 | + detailPane = { |
| 95 | + selectedItem?.let { item -> |
| 96 | + MyDetails(item) |
| 97 | + } |
| 98 | + }, |
| 99 | + ) |
| 100 | + // [END android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_part05] |
| 101 | +} |
| 102 | + |
| 103 | +@OptIn(ExperimentalMaterial3AdaptiveApi::class) |
| 104 | +@Preview |
| 105 | +@Composable |
| 106 | +fun SampleListDetailPaneScaffoldFull() { |
| 107 | +// [START android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_full] |
| 108 | + // Create the ListDetailPaneScaffoldState |
| 109 | + val state = rememberListDetailPaneScaffoldState() |
| 110 | + |
| 111 | + // Currently selected item |
| 112 | + var selectedItem: MyItem? by rememberSaveable { mutableStateOf(null) } |
| 113 | + |
| 114 | + ListDetailPaneScaffold( |
| 115 | + scaffoldState = state, |
| 116 | + listPane = { |
| 117 | + MyList( |
| 118 | + onItemClick = { id -> |
| 119 | + // Set current item |
| 120 | + selectedItem = id |
| 121 | + // Display the details pane |
| 122 | + state.navigateTo(ListDetailPaneScaffoldRole.Detail) |
| 123 | + }, |
| 124 | + ) |
| 125 | + }, |
| 126 | + detailPane = { |
| 127 | + // Show the details pane content if selected item is available |
| 128 | + selectedItem?.let { item -> |
| 129 | + MyDetails(item) |
| 130 | + } |
| 131 | + }, |
| 132 | + ) |
| 133 | +// [END android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_full] |
| 134 | +} |
| 135 | + |
| 136 | +@Composable |
| 137 | +fun MyList( |
| 138 | + onItemClick: (MyItem) -> Unit, |
| 139 | +) { |
| 140 | + Card { |
| 141 | + LazyColumn { |
| 142 | + shortStrings.forEachIndexed { id, string -> |
| 143 | + item { |
| 144 | + ListItem( |
| 145 | + modifier = Modifier |
| 146 | + .background(Color.Magenta) |
| 147 | + .clickable { |
| 148 | + onItemClick(MyItem(id)) |
| 149 | + }, |
| 150 | + headlineContent = { |
| 151 | + Text( |
| 152 | + text = string, |
| 153 | + ) |
| 154 | + }, |
| 155 | + ) |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +@Composable |
| 163 | +fun MyDetails(item: MyItem) { |
| 164 | + val text = shortStrings[item.id] |
| 165 | + Card { |
| 166 | + Column( |
| 167 | + Modifier |
| 168 | + .fillMaxSize() |
| 169 | + .padding(16.dp) |
| 170 | + ) { |
| 171 | + Text( |
| 172 | + text = "Details page for $text", |
| 173 | + fontSize = 24.sp, |
| 174 | + ) |
| 175 | + Spacer(Modifier.size(16.dp)) |
| 176 | + Text( |
| 177 | + text = "TODO: Add great details here" |
| 178 | + ) |
| 179 | + } |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +class MyItem(val id: Int) |
| 184 | + |
| 185 | +val shortStrings = listOf( |
| 186 | + "Android", |
| 187 | + "Petit four", |
| 188 | + "Cupcake", |
| 189 | + "Donut", |
| 190 | + "Eclair", |
| 191 | + "Froyo", |
| 192 | + "Gingerbread", |
| 193 | + "Honeycomb", |
| 194 | + "Ice cream sandwich", |
| 195 | + "Jelly bean", |
| 196 | + "Kitkat", |
| 197 | + "Lollipop", |
| 198 | + "Marshmallow", |
| 199 | + "Nougat", |
| 200 | + "Oreo", |
| 201 | + "Pie", |
| 202 | +) |
0 commit comments