捕获/传递 webview 对表面可组合的点击

发布于 2025-01-11 22:46:38 字数 712 浏览 1 评论 0原文

我希望我的网络视图不响应任何触摸;我只是希望它显示内容(因此 touchListener 返回 true)。父 Surface 可组合项应处理触摸。但这不起作用。

@OptIn(ExperimentalMaterialApi::class)
@Preview
@Composable
fun MyComposable() {
    Surface(
        modifier = Modifier.size(250.dp).padding(10.dp),
        onClick = { Log.e("TEST", "Clicked") },
        elevation = 5.dp
    )  {
       AndroidView(
           factory = { context ->
               WebView(context).apply {
                   this.setOnTouchListener { _, _ -> true }
               }
           },
           update = {
               it.loadUrl("https://stackoverflow.com/")
           }
       )
    }
}

I want my webview to not respond to any touches; I just want it to display content(hence the touchListener returning true). The parent Surface composable should handle the touches. But it doesn't work.

@OptIn(ExperimentalMaterialApi::class)
@Preview
@Composable
fun MyComposable() {
    Surface(
        modifier = Modifier.size(250.dp).padding(10.dp),
        onClick = { Log.e("TEST", "Clicked") },
        elevation = 5.dp
    )  {
       AndroidView(
           factory = { context ->
               WebView(context).apply {
                   this.setOnTouchListener { _, _ -> true }
               }
           },
           update = {
               it.loadUrl("https://stackoverflow.com/")
           }
       )
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九命猫 2025-01-18 22:46:38

目前唯一有效的解决方案是在 WebView 顶部放置一个空的 Surface 可组合项来捕获点击。

@OptIn(ExperimentalMaterialApi::class)
@Preview
@Composable
fun MyComposable() {
    Surface(
        modifier = Modifier.size(250.dp).padding(10.dp),
//        onClick = { Log.e("TEST", "Clicked") },    // Removed
        elevation = 5.dp
    )  {
       AndroidView(
           factory = { context ->
               WebView(context).apply {
                   this.setOnTouchListener { _, _ -> true }
               }
           },
           update = {
               it.loadUrl("https://stackoverflow.com/")
           }
       )

       Surface(
           onClick = { Log.e("TEST", "clicked") },
           color = Color.Transparent,
           modifier = Modifier.fillMaxSize()
       ) { }
    }
}

但这个解决方案似乎有点老套。 欢迎更好的解决方案。

Currently the only solution that works is having an empty Surface composable on top of the WebView to capture the clicks.

@OptIn(ExperimentalMaterialApi::class)
@Preview
@Composable
fun MyComposable() {
    Surface(
        modifier = Modifier.size(250.dp).padding(10.dp),
//        onClick = { Log.e("TEST", "Clicked") },    // Removed
        elevation = 5.dp
    )  {
       AndroidView(
           factory = { context ->
               WebView(context).apply {
                   this.setOnTouchListener { _, _ -> true }
               }
           },
           update = {
               it.loadUrl("https://stackoverflow.com/")
           }
       )

       Surface(
           onClick = { Log.e("TEST", "clicked") },
           color = Color.Transparent,
           modifier = Modifier.fillMaxSize()
       ) { }
    }
}

But this solution seems a bit hacky. Better solutions are welcome.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文