可组合的可点击功能不再起作用

时间:2021-06-08 20:28:15

标签: android kotlin android-jetpack-compose

这些是我的项目版本:

    val compose_version by extra("1.0.0-beta07")
    val kotlin_version by extra("1.4.32")
    val hilt_version by extra("2.36")

所以我有一个 LazyColum 显示自定义的可组合卡片列表:

package com.veloce.montageservice.ui.composables

// Here are imports but its too much code for stackoverflow

@Composable
fun SimpleCard(
    imageVector: ImageVector,
    title: String,
    description: String,
    @DrawableRes trailingButton: Int? = null,
    clickable: () -> Unit
) {
    Card(
        modifier = Modifier
            .padding(8.dp)
            .fillMaxWidth()
            .clickable {
                clickable()
            },
        border = BorderStroke(
            (0.5).dp,
            brush = Brush.horizontalGradient(
                colors = listOf(
                    MaterialTheme.colors.primary, MaterialTheme.colors.primaryVariant
                )
            )
        ),
        elevation = 5.dp
//        backgroundColor = Color.Gray
    ) {
        Row(
            modifier = Modifier.padding(8.dp),
            horizontalArrangement = if (trailingButton != null) Arrangement.SpaceEvenly else Arrangement.Start
        ) {
            Surface(
                shape = CircleShape,
                modifier = Modifier.size(50.dp),
                color = MaterialTheme.colors.primaryVariant
            ) {
                Icon(
                    modifier = Modifier.padding(4.dp),
                    imageVector = imageVector,
                    contentDescription = "Icon of a building",
                    tint = Color.White
                )
            }
            Column(
                modifier = Modifier.padding(8.dp),
                verticalArrangement = Arrangement.SpaceEvenly
            ) {
                Text(
                    title,
                    fontWeight = FontWeight.Bold
                )
                Text(description)
            }
            trailingButton?.let {
                Surface(
                    shape = CircleShape,
                    modifier = Modifier.size(35.dp),
                    color = MaterialTheme.colors.primaryVariant
                ) {
                    Icon(
                        modifier = Modifier.padding(4.dp),
                        imageVector = ImageVector.vectorResource(id = it),
                        contentDescription = "Icon of a building",
                        tint = Color.White
                    )
                }
            }
        }
    }
}

这应该是可点击的。是的,但现在不再是了,我在这个可组合中没有改变任何东西。 有谁知道出了什么问题? LazyColumn 的滚动功能运行良好,只是我一天到一天都无法再点击它。

1 个答案:

答案 0 :(得分:3)

在 compose 1.0.0-beta08 中有一个破坏性的 API 更改 (https://developer.android.com/jetpack/androidx/releases/compose-material),导致可点击修饰符被忽略。您必须改用 onClickCard 参数:

Card(onClick = { count++ }) {
    Text("Clickable card content with count: $count")
}