【发布时间】:2022-11-24 01:13:25
【问题描述】:
我有一个文本字符串列表,当我单击其中一个时,我应该用一种颜色为它着色,目前我的实现为所有文本着色,我做错了什么?
var isPressed by remember { mutableStateOf(false) }
val buttonColor: Color by animateColorAsState(
targetValue = when (isPressed) {
true -> FreshGreen
false -> PastelPeach
},
animationSpec = tween()
)
LazyRow(
modifier = modifier,
horizontalArrangement = Arrangement.spacedBy(25.dp)
) {
items(filterList) { filterName ->
Text(
text = filterName,
modifier = Modifier
.background(shape = RoundedCornerShape(24.dp), color = buttonColor)
.padding(horizontal = 16.dp, vertical = 8.dp)
.clickable(
interactionSource = remember { MutableInteractionSource() },
indication = null
) {
isPressed = !isPressed
onFilterClick(filterName)
}
)
}
}
【问题讨论】:
-
您对所有项目使用相同的状态
-
谢谢 Gabriel,这个让我通过了,不知道 Text 不会保持自己的状态,因为它是另一个可组合项,我认为它会为每个项目存储一个 Text 状态
标签: android kotlin android-jetpack-compose compose-recomposition