Ryan Mentley | 74f65f5 | 2020-04-08 04:59:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 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 | * http://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 | |
Louis Pullen-Freilich | ddda7be | 2020-07-17 18:28:12 +0100 | [diff] [blame] | 17 | package androidx.compose.foundation.demos |
Ryan Mentley | 74f65f5 | 2020-04-08 04:59:01 -0700 | [diff] [blame] | 18 | |
| 19 | import androidx.compose.Composable |
| 20 | import androidx.compose.Providers |
| 21 | import androidx.compose.getValue |
Andrey Kulikov | 9850fbc | 2020-06-12 18:42:10 +0100 | [diff] [blame] | 22 | import androidx.compose.remember |
Ryan Mentley | 74f65f5 | 2020-04-08 04:59:01 -0700 | [diff] [blame] | 23 | import androidx.compose.setValue |
| 24 | import androidx.compose.state |
| 25 | import androidx.ui.core.Modifier |
| 26 | import androidx.ui.demos.common.ComposableDemo |
Louis Pullen-Freilich | ddda7be | 2020-07-17 18:28:12 +0100 | [diff] [blame] | 27 | import androidx.compose.foundation.Box |
| 28 | import androidx.compose.foundation.ContentColorAmbient |
| 29 | import androidx.compose.foundation.ContentGravity |
| 30 | import androidx.compose.foundation.lazy.LazyColumnItems |
| 31 | import androidx.compose.foundation.Text |
| 32 | import androidx.compose.foundation.clickable |
| 33 | import androidx.compose.foundation.currentTextStyle |
| 34 | import androidx.compose.foundation.lazy.LazyRowItems |
| 35 | import androidx.compose.foundation.shape.corner.RoundedCornerShape |
Louis Pullen-Freilich | 4dc4dac | 2020-07-22 14:39:14 +0100 | [diff] [blame^] | 36 | import androidx.compose.ui.graphics.Color |
Louis Pullen-Freilich | 623e405 | 2020-07-19 20:24:03 +0100 | [diff] [blame] | 37 | import androidx.compose.foundation.layout.Column |
| 38 | import androidx.compose.foundation.layout.Row |
| 39 | import androidx.compose.foundation.layout.fillMaxHeight |
| 40 | import androidx.compose.foundation.layout.padding |
| 41 | import androidx.compose.foundation.layout.preferredWidth |
Ryan Mentley | 74f65f5 | 2020-04-08 04:59:01 -0700 | [diff] [blame] | 42 | import androidx.ui.unit.dp |
| 43 | import androidx.ui.unit.sp |
Andrey Kulikov | 9850fbc | 2020-06-12 18:42:10 +0100 | [diff] [blame] | 44 | import kotlin.random.Random |
Ryan Mentley | 74f65f5 | 2020-04-08 04:59:01 -0700 | [diff] [blame] | 45 | |
Andrey Kulikov | 6decc02 | 2020-06-12 15:23:12 +0100 | [diff] [blame] | 46 | val LazyListDemos = listOf( |
| 47 | ComposableDemo("Simple column") { LazyColumnDemo() }, |
Andrey Kulikov | 9850fbc | 2020-06-12 18:42:10 +0100 | [diff] [blame] | 48 | ComposableDemo("Add/remove items") { ListAddRemoveItemsDemo() }, |
| 49 | ComposableDemo("Horizontal list") { LazyRowItemsDemo() } |
Ryan Mentley | 74f65f5 | 2020-04-08 04:59:01 -0700 | [diff] [blame] | 50 | ) |
| 51 | |
| 52 | @Composable |
Andrey Kulikov | 6decc02 | 2020-06-12 15:23:12 +0100 | [diff] [blame] | 53 | private fun LazyColumnDemo() { |
| 54 | LazyColumnItems( |
| 55 | items = listOf( |
Ryan Mentley | 74f65f5 | 2020-04-08 04:59:01 -0700 | [diff] [blame] | 56 | "Hello,", "World:", "It works!", "", |
| 57 | "this one is really long and spans a few lines for scrolling purposes", |
| 58 | "these", "are", "offscreen" |
| 59 | ) + (1..100).map { "$it" } |
| 60 | ) { |
Louis Pullen-Freilich | 75e63e4 | 2020-04-28 00:26:43 +0100 | [diff] [blame] | 61 | Text(text = it, fontSize = 80.sp) |
Ryan Mentley | 74f65f5 | 2020-04-08 04:59:01 -0700 | [diff] [blame] | 62 | |
| 63 | if (it.contains("works")) { |
| 64 | Text("You can even emit multiple components per item.") |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | @Composable |
| 70 | private fun ListAddRemoveItemsDemo() { |
| 71 | var numItems by state { 0 } |
| 72 | var offset by state { 0 } |
| 73 | Column { |
| 74 | Row { |
| 75 | val buttonModifier = Modifier.padding(8.dp) |
| 76 | Button(modifier = buttonModifier, onClick = { numItems++ }) { Text("Add") } |
| 77 | Button(modifier = buttonModifier, onClick = { numItems-- }) { Text("Remove") } |
| 78 | Button(modifier = buttonModifier, onClick = { offset++ }) { Text("Offset") } |
| 79 | } |
| 80 | Column { |
Andrey Kulikov | 6decc02 | 2020-06-12 15:23:12 +0100 | [diff] [blame] | 81 | LazyColumnItems((1..numItems).map { it + offset }.toList()) { |
Ryan Mentley | 74f65f5 | 2020-04-08 04:59:01 -0700 | [diff] [blame] | 82 | Text("$it", style = currentTextStyle().copy(fontSize = 20.sp)) |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | @Composable |
Louis Pullen-Freilich | 3a54b94 | 2020-05-07 13:23:03 +0100 | [diff] [blame] | 89 | fun Button(modifier: Modifier, onClick: () -> Unit, children: @Composable () -> Unit) { |
Matvei Malkov | 323b745 | 2020-05-01 16:57:52 +0100 | [diff] [blame] | 90 | Box( |
| 91 | modifier = modifier.clickable(onClick = onClick), |
| 92 | shape = RoundedCornerShape(4.dp), |
| 93 | backgroundColor = Color(0xFF6200EE), |
| 94 | paddingStart = 16.dp, |
| 95 | paddingEnd = 16.dp, |
| 96 | paddingTop = 8.dp, |
| 97 | paddingBottom = 8.dp |
| 98 | ) { |
| 99 | Providers(ContentColorAmbient provides Color.White) { |
| 100 | children() |
Ryan Mentley | 74f65f5 | 2020-04-08 04:59:01 -0700 | [diff] [blame] | 101 | } |
| 102 | } |
Andrey Kulikov | 9850fbc | 2020-06-12 18:42:10 +0100 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | @Composable |
| 106 | private fun LazyRowItemsDemo() { |
| 107 | LazyRowItems(items = (1..1000).toList()) { |
| 108 | Square(it) |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | @Composable |
| 113 | private fun Square(index: Int) { |
| 114 | val width = remember { Random.nextInt(50, 150).dp } |
| 115 | Box( |
| 116 | Modifier.preferredWidth(width).fillMaxHeight(), |
| 117 | backgroundColor = colors[index % colors.size], |
| 118 | gravity = ContentGravity.Center |
| 119 | ) { |
| 120 | Text(index.toString()) |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | private val colors = listOf( |
| 125 | Color(0xFFffd7d7.toInt()), |
| 126 | Color(0xFFffe9d6.toInt()), |
| 127 | Color(0xFFfffbd0.toInt()), |
| 128 | Color(0xFFe3ffd9.toInt()), |
| 129 | Color(0xFFd0fff8.toInt()) |
| 130 | ) |