android - 删除 InteractionState 后,Jetpack Compose Beta 1 中提升按下状态

标签 android android-jetpack-compose

在 compose-beta01 之前,使用 InteractionState 在 Jetpack Compose 中提升按下状态非常容易:

@Composable
fun App() {
    val interactionState = remember { InteractionState() }
    val pressed = interactionState.contains(Interaction.Pressed)

    MyComposable(Modifier.clickable(interactionState = interactionState) { })
}

InteractionState 在 beta01 中被删除,现在有明显的方法可以复制这种行为。如何使用 clickable 修饰符提升按下状态?

最佳答案

您是否尝试过应用Compose beta01发行说明中解释的内容? ?

InteractionState has been replaced with [Mutable]InteractionSource

  • Interfaces are responsible for emitting / collecting Interaction events.
  • Instead of passing interactionState = remember { InteractionState() } to components such as Button and Modifier.clickable(), use interactionSource = remember { MutableInteractionSource() }.
  • Instead of: Interaction.Pressed in interactionState you should instead use the extension functions on InteractionSource, such as InteractionSource.collectIsPressedAsState().
  • For complex use cases you can use InteractionSource.interactions to observe the stream of Interactions. See the InteractionSource documentation and samples for more information.
  • (I85965, b/152525426, b/171913923, b/171710801, b/174852378)

因此,在您的示例中,我会尝试以下操作:

val interactionSource = remember { MutableInteractionSource() }
val pressedState = interactionSource.collectIsPressedAsState()

MyComposable(
    Modifier.clickable(
        interactionSource = interactionSource,
        indication = LocalIndication.current
    ) {}
)

关于android - 删除 InteractionState 后,Jetpack Compose Beta 1 中提升按下状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66376112/

相关文章:

android - 使用 WindowManager 调整窗口大小时显示屏幕保护程序

AFW 注册后,Android O 无法扩充 webview

android - 防止 Jetpack Compose 中的文本在设备字体大小增加时放大

android - 升级到 v0.19.0 后如何禁用拖动 Compose 寻呼机?

android - Jetpack Compose 中带有分页库的粘性 header

android - 获取 "No compose views found in the app. Is your Activity resumed?"

安卓 SQLite : should I use beginTransaction and endTransaction only at insert or both insert and read queries?

android - 我可以在 VMWare 内的 Linux 上使用 HAXM 吗?

java - 如何更改弹出窗口 TextView 中的文本?

android - 为什么我的bottomSheet 来自jetpack compose 中的屏幕顶部