在 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 asButton
andModifier.clickable()
, useinteractionSource = 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/