data class Item(
var item: String,
var isNew: Boolean,
)
@Composable
fun Item() {
val context = LocalContext.current
val list = mutableStateListOf<Item>()
repeat(20) {
list.add(Item(item = "Item $it", isNew = false))
}
Column(modifier = Modifier
.fillMaxSize()
.padding(start = 10.dp, end = 10.dp)) {
Button(modifier = Modifier
.fillMaxWidth()
.padding(top = 10.dp), onClick = {
val index = (0..5).random()
if (index < list.size) {
list.add(index, Item(item = "insert Item ${(0..5).random()}", isNew = true))
} else {
list.add(0, Item(item = "insert Item ${(0..5).random()}", isNew = true))
}
}) {
Text(text = "add Item")
}
Button(modifier = Modifier
.fillMaxWidth()
.padding(top = 10.dp), onClick = {
val index = (0..5).random()
if (index < list.size) {
list.removeAt(index)
} else {
Toast.makeText(context, "无此条目", Toast.LENGTH_SHORT).show()
}
}) {
Text(text = "remove Item")
}
LazyColumn(content = {
items(count = list.size, itemContent = { index ->
Box(modifier = Modifier
.fillMaxWidth()
.padding(top = 10.dp, bottom = 10.dp)
.background(color = if (list[index].isNew) Color.LightGray else Color.DarkGray)) {
Text(modifier = Modifier
.fillMaxWidth()
.padding(top = 10.dp, bottom = 10.dp), fontWeight = FontWeight.Medium, fontSize = 20.sp, color = Color.White, textAlign = TextAlign.Center, text = list[index].item)
}
})
})
}
}