如何在 LazyColumn 列文本中的两个不同文本中交替颜色。按照下面的代码
LazyColumn(/*contentPadding = PaddingValues(horizontal = 16.dp, vertical = 8.dp),*/
modifier = Modifier.fillMaxSize()
) {
items(50) {index ->
Text("R$ 120,00/UN", fontSize = 13.sp,
color = if(selectd==index) colorResource(id = R.color.red)
else colorResource(id = R.color.white),
modifier = Modifier
.border(1.dp, Color.LightGray, RoundedCornerShape(4.dp))
.background(if(selectd==index) colorResource(id = R.color.pastel_green)
else colorResource(id = R.color.cinza))
.padding(5.dp)
.clickable { selectd=index})
Text("R$ 120,00/KG", fontSize = 13.sp,
color = if(selectd==index) colorResource(id = R.color.red)
else colorResource(id = R.color.white),
modifier = Modifier.border(1.dp, Color.LightGray, RoundedCornerShape(4.dp))
.background(if(selectd==index) colorResource(id = R.color.pastel_green)
else colorResource(id = R.color.cinza))
.padding(5.dp)
.clickable { selectd=index})
}
}
答案 0 :(得分:0)
您可以使用 itemsIndexed
代替 index
。
类似的东西:
val itemsList = (0..12).toList()
LazyColumn(
modifier = Modifier.fillMaxSize()
) {
itemsIndexed(itemsList) { index, item ->
val backgroundColor = if (selectd == index) Color.Blue
else{
if (index % 2 == 0) Color.Yellow else Color.Red
}
Text("R$ 120,00/UN", fontSize = 13.sp,
modifier = Modifier
.border(1.dp, Color.LightGray, RoundedCornerShape(4.dp))
.background(backgroundColor)
.padding(5.dp)
.clickable { selectd=index})
}
}