如何在Jetpack Compose中禁用TabRow或Tab的涟漪效果?
解决Jetpack Compose TabRow点击时的不必要涟漪效果
问题根源
你之前在Modifier中添加.clickable并设置indication = null无效,是因为Tab组件本身内置了点击交互逻辑,它自身的indication参数优先级更高,外部Modifier的设置会被覆盖。
直接解决方案
直接给Tab组件设置indication = null,同时配合自定义的interactionSource,就能完全禁用涟漪效果:
修改后的完整代码片段:
TabRow( modifier = Modifier.height(20.dp), selectedTabIndex = selectedIndex, indicator = { tabPositions -> TabRowDefaults.Indicator( modifier = Modifier.customTabIndicatorOffset( currentTabPosition = tabPositions[lazyListState.firstVisibleItemIndex], tabWidths[lazyListState.firstVisibleItemIndex] ), color = RED ) }, backgroundColor = BLACK ) { tabList.forEachIndexed { index, tab -> val selected = (selectedIndex == index) val interactionSource = remember { MutableInteractionSource() } Tab( modifier = Modifier, selected = selected, selectedContentColor = BLACK, onClick = { animateScrollToItem(index) // 注:原代码中参数应为index,否则点击不会切换选中项 }, // 核心设置:禁用涟漪效果 indication = null, interactionSource = interactionSource, text = { Text("Text Code") } ) } }
可选:修改涟漪颜色而非禁用
如果只是想让涟漪不可见而非完全禁用,可以自定义涟漪颜色与背景色一致:
// 替换上述Tab的indication参数 indication = rememberRipple(color = BLACK), interactionSource = interactionSource
内容的提问来源于stack exchange,提问作者Android Dev
相关产品推荐
相关产品推荐

