Last active
December 15, 2022 08:10
-
-
Save One-cy/4179fba144016c30d35480b1436d1711 to your computer and use it in GitHub Desktop.
Jetpack compose MutableList LazyColumn
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.composelist | |
import androidx.compose.foundation.BorderStroke | |
import androidx.compose.foundation.background | |
import androidx.compose.foundation.clickable | |
import androidx.compose.foundation.layout.* | |
import androidx.compose.foundation.shape.RoundedCornerShape | |
import androidx.compose.material.Card | |
import androidx.compose.material.Text | |
import androidx.compose.runtime.Composable | |
import androidx.compose.ui.Alignment | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.graphics.Color | |
import androidx.compose.ui.text.style.TextAlign | |
import androidx.compose.ui.tooling.preview.Preview | |
import androidx.compose.ui.unit.dp | |
@Composable | |
fun LogItem(country: String) { | |
Card( | |
border = BorderStroke(4.dp, Color.Red), | |
shape = RoundedCornerShape(20.dp), elevation = 10.dp, | |
) { | |
val padding = 16.dp | |
Column( | |
Modifier | |
.padding(padding) | |
.fillMaxWidth() | |
) { | |
Row( | |
Modifier.padding(padding), | |
verticalAlignment = Alignment.CenterVertically, | |
horizontalArrangement = Arrangement.Center | |
) { | |
Text( | |
text = "$country", textAlign = TextAlign.Center, | |
modifier = Modifier.fillMaxWidth() | |
) | |
} | |
Spacer(Modifier.size(padding)) | |
Row() { | |
Column( | |
Modifier | |
.weight(1f) | |
.background(Color.Blue) | |
) { | |
Text( | |
modifier = Modifier | |
.fillMaxWidth() | |
.background(Color.Red), | |
text = "Weight = 1", | |
color = Color.White, | |
textAlign = TextAlign.Center | |
) | |
Text(text = "azer", color = Color.White) | |
Text(text = "tyui", color = Color.White) | |
Text(text = "qsdfgh", color = Color.White) | |
Text(text = "xcvb", color = Color.White) | |
} | |
Column( | |
Modifier | |
.weight(1f) | |
.background(Color.DarkGray) | |
) { | |
Text(text = "Weight = 1") | |
Text(text = "text 1") | |
Text(text = "tex2 \n toto \n\n\n\n\n azerty") | |
} | |
} | |
} | |
} | |
} | |
@Preview | |
@Composable | |
fun MyPreview() { | |
LogItem("America") | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.composelist | |
import android.os.Bundle | |
import androidx.activity.ComponentActivity | |
import androidx.activity.compose.setContent | |
import androidx.compose.foundation.background | |
import androidx.compose.foundation.layout.* | |
import androidx.compose.foundation.lazy.LazyColumn | |
import androidx.compose.foundation.lazy.items | |
import androidx.compose.material.* | |
import androidx.compose.runtime.* | |
import androidx.compose.runtime.getValue | |
import androidx.compose.runtime.setValue | |
import androidx.compose.runtime.saveable.rememberSaveable | |
import androidx.compose.ui.graphics.Color | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.text.TextStyle | |
import androidx.compose.ui.tooling.preview.Preview | |
import androidx.compose.ui.unit.dp | |
import androidx.compose.ui.unit.sp | |
import com.example.composelist.ui.theme.ComposeListTheme | |
class MainActivity : ComponentActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContent { | |
ComposeListTheme { | |
// A surface container using the 'background' color from the theme | |
Surface( | |
modifier = Modifier.fillMaxSize(), | |
color = MaterialTheme.colors.background | |
) { | |
Greeting("Android") | |
} | |
} | |
} | |
} | |
} | |
@Composable | |
fun Greeting(name: String) { | |
Column { | |
Text(text = "Hello $name!") | |
HelloScreen() | |
var clickCount = rememberSaveable { mutableStateOf(0) } | |
Column { | |
Button(onClick = { clickCount.value++ }) { | |
Text(text = "" + clickCount.value + " times clicked") | |
} | |
} | |
DynamicListScreen() | |
} | |
} | |
@Preview(showBackground = true) | |
@Composable | |
fun DefaultPreview() { | |
ComposeListTheme { | |
Greeting("Android") | |
HelloScreen() | |
} | |
} | |
@Composable | |
fun HelloScreen() { | |
var name by rememberSaveable { mutableStateOf("") } | |
HelloContent(name = name, onNameChange = { name = it }) | |
} | |
@Composable | |
fun HelloContent(name: String, onNameChange: (String) -> Unit) { | |
Column(modifier = Modifier.padding(16.dp)) { | |
Text( | |
text = "Hello, $name", | |
modifier = Modifier.padding(bottom = 8.dp), | |
style = MaterialTheme.typography.h5 | |
) | |
OutlinedTextField( | |
value = name, | |
onValueChange = onNameChange, | |
label = { Text("Name") } | |
) | |
} | |
} | |
private val listModifier = Modifier | |
.fillMaxSize() | |
.background(Color.Gray) | |
.padding(10.dp) | |
var mainList = mutableStateListOf("India", "Pakistan", "China", "United States") | |
fun addItem() { | |
mainList.add("France") | |
} | |
@Composable | |
fun DynamicListScreen() { | |
Text(text = "lazyColumn") | |
val countryList = remember { mainList } | |
Button(onClick = { | |
// countryList.add("France") | |
addItem() | |
}) { | |
Text(text = "new line") | |
} | |
LazyColumn(modifier = listModifier) { | |
items(countryList) { country -> | |
LogItem(country = country) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment