kotlin 我无法在单击“Jetpack编写”时为列表中的单个文本着色(单选)

klr1opcd  于 2022-11-25  发布在  Kotlin
关注(0)|答案(3)|浏览(174)

我有一个文本的字符串列表,当我点击其中一个时,我应该用一种颜色来着色,目前我的实现着色了所有的文本,我做错了什么?

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)
                    }
            )
        }
    }
olmpazwi

olmpazwi1#

我修改了您的代码,降低了动画和按下状态的级别,这样父级可组合对象就不会受到它自己的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
            }
    )
}

mnemlml8

mnemlml82#

您对所有项目使用相同的状态(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
        )
    }
}

yjghlzjz

yjghlzjz3#

对于那些只想保持选择只有一个项目的时间,这里是我去的方式

@Composable
fun BrandCategoryFilterSection(
    modifier: Modifier,
    uiState: BrandFilterUiState,
    onBrandCategoryClick: (String) -> Unit
) {
    var selectedIndex by remember { mutableStateOf(-1) }

    LazyRow(
        modifier = modifier,
        horizontalArrangement = Arrangement.spacedBy(25.dp)
    ) {
        itemsIndexed(uiState.categoryList) { index, categoryName ->
            CategoryText(
                categoryName = categoryName,
                isSelected = index == selectedIndex,
                onBrandCategoryClick = {
                    selectedIndex = index
                    onBrandCategoryClick(it)
                }
            )
        }
    }
}

@Composable
private fun CategoryText(categoryName: String, onBrandCategoryClick: (String) -> Unit, isSelected: Boolean) {
    
    val buttonColor: Color by animateColorAsState(
        targetValue = when (isSelected) {
            true -> FreshGreen
            false -> PastelPeach
        },
        animationSpec = tween()
    )

    Text(
        text = categoryName,
        modifier = Modifier
            .background(shape = RoundedCornerShape(24.dp), color = buttonColor)
            .padding(horizontal = 16.dp, vertical = 8.dp)
            .clickable(
                interactionSource = remember { MutableInteractionSource() },
                indication = null
            ) {
                onBrandCategoryClick(categoryName)
            }
    )
}

相关问题