[英]Hoist pressed state in Jetpack Compose Beta 1 after InteractionState was removed
Until compose-beta01 it was very easy to hoist the pressed state in Jetpack Compose using InteractionState
:在 compose-beta01 之前,使用
InteractionState
在 Jetpack Compose 中提升按下的 state 非常容易:
@Composable
fun App() {
val interactionState = remember { InteractionState() }
val pressed = interactionState.contains(Interaction.Pressed)
MyComposable(Modifier.clickable(interactionState = interactionState) { })
}
InteractionState was removed in beta01 and there is now obvious way to replicate this behaviour. InteractionState 在 beta01 中被删除,现在有明显的方法来复制这种行为。 How can I hoist the pressed state using the
clickable
modifier?如何使用
clickable
的修饰符提升按下的 state?
Have you tried applying what's explained in the release notes of Compose beta01 ?您是否尝试过应用Compose beta01发行说明中解释的内容?
InteractionState
has been replaced with[Mutable]InteractionSource
InteractionState
已替换为[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() }
.不要将
interactionState = remember { InteractionState() }
传递给Button
和Modifier.clickable()
等组件,而是使用interactionSource = remember { MutableInteractionSource() }
。- Instead of:
Interaction.Pressed in interactionState
you should instead use the extension functions on InteractionSource, such as InteractionSource.collectIsPressedAsState().而不是:
Interaction.Pressed in interactionState
您应该使用 InteractionSource 上的扩展函数,例如 InteractionSource.collectIsPressedAsState()。- For complex use cases you can use InteractionSource.interactions to observe the stream of Interactions.
对于复杂的用例,您可以使用 InteractionSource.interactions 来观察交互的 stream。 See the InteractionSource documentation and samples for more information.
有关详细信息,请参阅 InteractionSource 文档和示例。
- ( I85965 , b/152525426 , b/171913923 , b/171710801 , b/174852378 )
( I85965 、 b/152525426 、 b/171913923 、 b/171710801 、 b/174852378 )
So, in your example, I would try something like:所以,在你的例子中,我会尝试这样的事情:
val interactionSource = remember { MutableInteractionSource() }
val pressedState = interactionSource.collectIsPressedAsState()
MyComposable(
Modifier.clickable(
interactionSource = interactionSource,
indication = LocalIndication.current
) {}
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.