撰写:beta08 中断可点击

Compose: beta08 breaks clickable

我有一个带有可扩展卡片视图的可组合项。

@Composable
fun ItemCard() {
    var isExpanded by remember { mutableStateOf(false) }
    Card(modifier = Modifier.clickable {
            isExpanded = !isExpanded
            Log.d(TAG, "Expanded set to $isExpanded")
        }) {
            // regular card segment goes here
            AnimatedVisibility (isExpanded) {
               // expanded card segment goes here
            }
    }
}

因此,我将 compose 更新为 1.0.0-beta08Modifier.clickable 停止工作。

依赖项如下:

implementation 'androidx.core:core-ktx:1.5.0'
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.3.0'
implementation "androidx.compose.ui:ui:$compose_version"
implementation "androidx.compose.material:material:$compose_version"
implementation "androidx.compose.ui:ui-tooling:$compose_version"
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
implementation 'androidx.activity:activity-compose:1.3.0-alpha08'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
implementation "androidx.navigation:navigation-compose:2.4.0-alpha01"
implementation "androidx.constraintlayout:constraintlayout-compose:1.0.0-alpha07"
implementation 'androidx.room:room-common:2.3.0'
implementation 'androidx.room:room-ktx:2.3.0'
implementation "androidx.room:room-runtime:2.3.0"
kapt "androidx.room:room-compiler:2.3.0"
implementation "androidx.compose.runtime:runtime-livedata:$compose_version"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.3.1"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.4.32"
api "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.9"
api "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9"
coreLibraryDesugaring("com.android.tools:desugar_jdk_libs:1.1.5")

我所做的更改是将 compose 从 1.0.0-beta07 更改为 1.0.0-beta08 并且(根据 compose 更新的要求)将 classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.32" 更改为 classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.10"

原因:

这是从 1.0.0-beta071.0 的 行为中断 API 更改。 Jetpack Compose 发行说明中提到的 0-beta08

Jetpack compose Version 1.0.0-beta08 Behavior Breaking API Change

BEHAVIOUR-BREAKING: Card now consumes clicks, making clicks added via Card(Modifier.clickable) to be a no-op. Please, use new experimental overload of a Card that accepts onClick. (Ia8744, b/183775620)


解法:

提供的解决方案是 Card 的重载,它允许处理点击以及相关属性,如指示、interactionSource、enabled/disabled。

Added a new Card overload that handles clicks as well as other clickable functionality: indication, interactionSource, enabled/disabled. It wasn't possible to use a regular non-clickable Card with the Modifier.clickable because the Card will not clip the ripple indication in those cases.


卡超载:

这是公开 onClick 以及 interactionSourceindication.

的新 Card 重载
@Composable 
fun Card(
    onClick: () -> Unit,
    modifier: Modifier = Modifier,
    shape: Shape = MaterialTheme.shapes.medium,
    backgroundColor: Color = MaterialTheme.colors.surface,
    contentColor: Color = contentColorFor(backgroundColor),
    border: BorderStroke? = null,
    elevation: Dp = 1.dp,
    interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
    indication: Indication? = LocalIndication.current,
    enabled: Boolean = true,
    onClickLabel: String? = null,
    role: Role? = null,
    content: @Composable () -> Unit
)