[go: nahoru, domu]

blob: 25a549f340287bccbaa0139f4c771874bcd8189e [file] [log] [blame]
Ryan Mentley74f65f52020-04-08 04:59:01 -07001/*
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-Freilichddda7be2020-07-17 18:28:12 +010017package androidx.compose.foundation.demos
Ryan Mentley74f65f52020-04-08 04:59:01 -070018
19import androidx.compose.Composable
20import androidx.compose.Providers
21import androidx.compose.getValue
Andrey Kulikov9850fbc2020-06-12 18:42:10 +010022import androidx.compose.remember
Ryan Mentley74f65f52020-04-08 04:59:01 -070023import androidx.compose.setValue
24import androidx.compose.state
25import androidx.ui.core.Modifier
26import androidx.ui.demos.common.ComposableDemo
Louis Pullen-Freilichddda7be2020-07-17 18:28:12 +010027import androidx.compose.foundation.Box
28import androidx.compose.foundation.ContentColorAmbient
29import androidx.compose.foundation.ContentGravity
30import androidx.compose.foundation.lazy.LazyColumnItems
31import androidx.compose.foundation.Text
32import androidx.compose.foundation.clickable
33import androidx.compose.foundation.currentTextStyle
34import androidx.compose.foundation.lazy.LazyRowItems
35import androidx.compose.foundation.shape.corner.RoundedCornerShape
Louis Pullen-Freilich4dc4dac2020-07-22 14:39:14 +010036import androidx.compose.ui.graphics.Color
Louis Pullen-Freilich623e4052020-07-19 20:24:03 +010037import androidx.compose.foundation.layout.Column
38import androidx.compose.foundation.layout.Row
39import androidx.compose.foundation.layout.fillMaxHeight
40import androidx.compose.foundation.layout.padding
41import androidx.compose.foundation.layout.preferredWidth
Ryan Mentley74f65f52020-04-08 04:59:01 -070042import androidx.ui.unit.dp
43import androidx.ui.unit.sp
Andrey Kulikov9850fbc2020-06-12 18:42:10 +010044import kotlin.random.Random
Ryan Mentley74f65f52020-04-08 04:59:01 -070045
Andrey Kulikov6decc022020-06-12 15:23:12 +010046val LazyListDemos = listOf(
47 ComposableDemo("Simple column") { LazyColumnDemo() },
Andrey Kulikov9850fbc2020-06-12 18:42:10 +010048 ComposableDemo("Add/remove items") { ListAddRemoveItemsDemo() },
49 ComposableDemo("Horizontal list") { LazyRowItemsDemo() }
Ryan Mentley74f65f52020-04-08 04:59:01 -070050)
51
52@Composable
Andrey Kulikov6decc022020-06-12 15:23:12 +010053private fun LazyColumnDemo() {
54 LazyColumnItems(
55 items = listOf(
Ryan Mentley74f65f52020-04-08 04:59:01 -070056 "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-Freilich75e63e42020-04-28 00:26:43 +010061 Text(text = it, fontSize = 80.sp)
Ryan Mentley74f65f52020-04-08 04:59:01 -070062
63 if (it.contains("works")) {
64 Text("You can even emit multiple components per item.")
65 }
66 }
67}
68
69@Composable
70private 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 Kulikov6decc022020-06-12 15:23:12 +010081 LazyColumnItems((1..numItems).map { it + offset }.toList()) {
Ryan Mentley74f65f52020-04-08 04:59:01 -070082 Text("$it", style = currentTextStyle().copy(fontSize = 20.sp))
83 }
84 }
85 }
86}
87
88@Composable
Louis Pullen-Freilich3a54b942020-05-07 13:23:03 +010089fun Button(modifier: Modifier, onClick: () -> Unit, children: @Composable () -> Unit) {
Matvei Malkov323b7452020-05-01 16:57:52 +010090 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 Mentley74f65f52020-04-08 04:59:01 -0700101 }
102 }
Andrey Kulikov9850fbc2020-06-12 18:42:10 +0100103}
104
105@Composable
106private fun LazyRowItemsDemo() {
107 LazyRowItems(items = (1..1000).toList()) {
108 Square(it)
109 }
110}
111
112@Composable
113private 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
124private val colors = listOf(
125 Color(0xFFffd7d7.toInt()),
126 Color(0xFFffe9d6.toInt()),
127 Color(0xFFfffbd0.toInt()),
128 Color(0xFFe3ffd9.toInt()),
129 Color(0xFFd0fff8.toInt())
130)