You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在点击Jetpack Compose子组件时触发父组件点击并显示全局波纹?

实现子组件点击触发父组件事件+全局波纹

完全可以实现,核心要处理事件传递逻辑和波纹效果的共享,具体有两种可行方案:

方案一:共享交互源+手动触发父逻辑

通过共享InteractionSource让父组件的波纹在子组件点击时同步生效,同时在子组件的点击回调里手动调用父组件的处理逻辑:

@Composable
fun Test() {
    val parentClickAction = {
        // 父组件点击逻辑
        println("父组件被点击")
    }
    val sharedInteractionSource = remember { MutableInteractionSource() }
    
    Column(
        modifier = Modifier
            .clickable(
                interactionSource = sharedInteractionSource,
                indication = rememberRipple(bounded = true) // 父组件的全局波纹
            ) {
                parentClickAction()
            }
    ) {
        Row(
            modifier = Modifier
                .clickable(
                    interactionSource = sharedInteractionSource,
                    indication = null, // 禁用子组件自身波纹,复用父组件的
                    onClick = {
                        // 子组件点击逻辑
                        println("子组件被点击")
                        parentClickAction() // 手动触发父组件逻辑
                    }
                )
        ) {
            Column {
                Text("Hi")
                Spacer(modifier = Modifier.size(10.dp))
                Text("Test")
            }
            Text("Test2")
        }
    }
}

方案二:禁用子组件事件消费,让事件透传

如果不想手动调用父逻辑,可以改用pointerInput替代默认的clickable(默认clickable会消费事件,阻止父组件触发),让事件自然透传到父组件,同时共享交互源保证波纹覆盖整个父组件:

@Composable
fun Test() {
    val sharedInteractionSource = remember { MutableInteractionSource() }
    
    Column(
        modifier = Modifier
            .clickable(
                interactionSource = sharedInteractionSource,
                indication = rememberRipple(bounded = true)
            ) {
                println("父组件被点击")
            }
    ) {
        Row(
            modifier = Modifier
                .pointerInput(Unit) {
                    detectTapGestures(
                        onTap = {
                            println("子组件被点击")
                            // 不消费事件,让父组件的点击逻辑自动触发
                        }
                    )
                }
                .indication(sharedInteractionSource, rememberRipple(bounded = true))
        ) {
            Column {
                Text("Hi")
                Spacer(modifier = Modifier.size(10.dp))
                Text("Test")
            }
            Text("Test2")
        }
    }
}

关键说明

  • 波纹效果:共享同一个MutableInteractionSource后,子组件点击时会激活父组件的波纹,实现整个父布局的波纹扩散效果。
  • 事件传递:方案一通过主动调用父逻辑确保两者都执行;方案二利用pointerInput的手势检测不拦截事件,让父组件的点击事件自动触发。

内容的提问来源于stack exchange,提问作者Donghoon Kim

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 19:49:59