Jetpack Compose - 在自定义布局中获取点击位置
Jetpack Compose - get click position in custom layout
我正在开发一个自定义布局,该布局使用 Layout
可组合项在 Jetpack Compose 中呈现一天的时间线。结果看起来像 Google's Calenar and code is similar to the one described in that great tutorial: https://danielrampelt.com/blog/jetpack-compose-custom-schedule-layout-part-1/
但是我的问题是:
我如何在 Layout
可组合项中接收点击的 (x, y) 位置。通过向 placeables
添加 clickable
修饰符,我可以轻松地接收对 Layout
的子项的点击。然而,我的问题是,我如何接收在 childeren
之间的 space 布局上点击的位置
(根据前面提到的示例,这将是日期时间轴中约会之间的 space。在这种情况下,我需要点击的位置来获取有关用户点击时间的信息)
我尝试在整个 Layout
可组合项上添加 .clickable
修饰符,但我没有收到 onClick
lambda 中布局内点击的 (x, y) 位置(它的类型是 () -> Unit
)
您可以使用 detectTapGestures
,它将 return 偏移量:
Modifier.pointerInput(Unit) {
detectTapGestures { offset ->
}
}
如果您需要为此手势添加波纹效果,请参阅。在您的情况下,每个子视图可能需要一组 MutableInteractionSource
,父视图需要一个。
您可以在 Compose documentation 中找到更多自定义手势。
我正在开发一个自定义布局,该布局使用 Layout
可组合项在 Jetpack Compose 中呈现一天的时间线。结果看起来像 Google's Calenar and code is similar to the one described in that great tutorial: https://danielrampelt.com/blog/jetpack-compose-custom-schedule-layout-part-1/
但是我的问题是:
我如何在 Layout
可组合项中接收点击的 (x, y) 位置。通过向 placeables
添加 clickable
修饰符,我可以轻松地接收对 Layout
的子项的点击。然而,我的问题是,我如何接收在 childeren
(根据前面提到的示例,这将是日期时间轴中约会之间的 space。在这种情况下,我需要点击的位置来获取有关用户点击时间的信息)
我尝试在整个 Layout
可组合项上添加 .clickable
修饰符,但我没有收到 onClick
lambda 中布局内点击的 (x, y) 位置(它的类型是 () -> Unit
)
您可以使用 detectTapGestures
,它将 return 偏移量:
Modifier.pointerInput(Unit) {
detectTapGestures { offset ->
}
}
如果您需要为此手势添加波纹效果,请参阅MutableInteractionSource
,父视图需要一个。
您可以在 Compose documentation 中找到更多自定义手势。