【问题标题】:I cannot color single text from my list on click Jetpack Compose (single selection)单击 Jetpack Compose(单选)时,我无法为列表中的单个文本着色
【发布时间】: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


【解决方案1】:

您对所有项目使用相同的状态 (isPressed)。
作为 z.y's answer 的替代方案,您可以将 isPressed 声明移到 items 块内:

LazyRow(
    horizontalArrangement = Arrangement.spacedBy(25.dp)
) {
    items(itemsList) { filterName->

        var isPressed by remember { mutableStateOf(false) }

        val buttonColor: Color by animateColorAsState(
            targetValue = when (isPressed) {
                true -> Color.Green
                false -> Color.Red
            },
            animationSpec = tween()
        )
        
        Text(
          //your code
        )
    }
}

【讨论】:

  • 出色的 !!!关于情况的完整背景,因为他首先回答我会给他正确的答案,但非常感谢您抽出时间 Gabriele !
  • 是否需要索引?
  • @SNM 不,我的错,对不起。
  • 谢谢@Gabriele,我忘了你实际上可以在item{…} 中有一个范围。也谢谢你@SNM
【解决方案2】:

我修改了你的代码,我降低了动画和按下状态,这样父可组合项就不会受到它自己的影响 re-composition

@Composable
fun MyScreen(
    modifier: Modifier = Modifier,
    filterList: SnapshotStateList<String>
) {
    LazyRow(
        modifier = modifier,
        horizontalArrangement = Arrangement.spacedBy(25.dp)
    ) {

        items(filterList) { filterName ->
            FilterText(
                filterName
            )
        }
    }
}

@Composable
fun FilterText(
    filter: String
) {

    var isPressed by remember { mutableStateOf(false) }
    val buttonColor: Color by animateColorAsState(
        targetValue = when (isPressed) {
            true -> Color.Blue
            false -> Color.Green
        },
        animationSpec = tween()
    )

    Text(
        text = filter,
        modifier = Modifier
            .background(shape = RoundedCornerShape(24.dp), color = buttonColor)
            .padding(horizontal = 16.dp, vertical = 8.dp)
            .clickable {
                isPressed = !isPressed
            }
    )
}

【讨论】:

  • 有趣的是,当我们重组时,它似乎没有维护列表中单击元素的范围,谢谢!
猜你喜欢
  • 1970-01-01
  • 2022-11-04
  • 1970-01-01
  • 1970-01-01
  • 2014-02-28
  • 1970-01-01
  • 1970-01-01
  • 2022-09-28
  • 2022-06-23
相关资源
最近更新 更多