简体   繁体   English

单击 Jetpack Compose 时如何禁用波纹效果

[英]How to disable ripple effect when clicking in Jetpack Compose

In Jetpack Compose, when you enable clickable {} on a modifier for a composable, by default it enables ripple effect for it.在 Jetpack Compose 中,当您在可组合项的修饰符上启用clickable {}时,默认情况下会为其启用涟漪效应。 How to disable this behavior?如何禁用此行为?

Example code示例代码

Row(modifier = Modifier
         .clickable { // action }
)

Short answer :简短的回答
to disable the ripple pass null in the indication parameter in the clickable modifier:clickable修饰符的indication参数中禁用波纹传递null

val interactionSource = remember { MutableInteractionSource() }
Column {
    Text(
        text = "Click me and my neighbour will indicate as well!",
        modifier = Modifier
            .clickable(
                interactionSource = interactionSource,
                indication = null
            ) {
                /* .... */
            }
    )

Long answer :长答案
If you add the clickable modifier to the element to make it clickable within its bounds it will show an Indication as specified in indication parameter.如果您将clickable修饰符添加到元素以使其在其边界内可点击,它将显示Indication参数中指定的指示。

By default, indication from LocalIndication will be used.默认情况下,将使用来自LocalIndication的指示。

If you are using a MaterialTheme in your hierarchy, a Ripple will be used as the default Indication inside components such as androidx.compose.foundation.clickable and androidx.compose.foundation.indication .如果您在层次结构中使用MaterialThemeRipple将用作组件内部的默认Indication ,例如androidx.compose.foundation.clickableandroidx.compose.foundation.indication

Use this Modifier extension:使用此修饰符扩展:

inline fun Modifier.noRippleClickable(crossinline onClick: ()->Unit): Modifier = composed {
    clickable(indication = null,
        interactionSource = remember { MutableInteractionSource() }) {
        onClick()
    }
}

then simply replace Modifier.clickable {} with Modifier.noRippleClickable {}然后只需将Modifier.clickable {}替换为Modifier.noRippleClickable {}

Row(modifier = Modifier.noRippleClickable { 
  // action 
})

To disable the ripple effect, have to pass null to indication property of the modifier.要禁用波纹效果,必须将null传递给修饰符的indication属性。

More about indication on Jetpack Compose documentation有关Jetpack Compose 文档中的指示的更多信息

Code代码

Row(
    modifier = Modifier
        .clickable(
            indication = null, 
            interactionSource = remember { MutableInteractionSource() } // This is mandatory
        ) { 
            // action
        }
)

Modifier extension with other parameters:带有其他参数的修饰符扩展:

inline fun Modifier.noRippleClickable(
        enabled: Boolean = true,
        onClickLabel: String? = null,
        role: Role? = null,
        crossinline onClick: ()->Unit
    ): Modifier = composed {
        clickable(
            enabled = enabled,
            indication = null,
            onClickLabel = onClickLabel,
            role = role,
            interactionSource = remember { MutableInteractionSource() }) {
            onClick()
        }
    }

I used @Mahdi-Malv's answer and modify as below:我使用@Mahdi-Malv 的答案并修改如下:

  • remove inline and crossinline删除内联和交叉内联
  • modify according to my requirement根据我的要求修改
fun Modifier.noRippleClickable( interactionSource: MutableInteractionSource = remember { MutableInteractionSource() }, indication: Indication? = null, enabled: Boolean = true, onClickLabel: String? = null, role: Role? = null, onClick: () -> Unit, ) = clickable( interactionSource = interactionSource, indication = indication, enabled = enabled, onClickLabel = onClickLabel, role = role, onClick = onClick, )

You can handle it this way when working with Buttons.使用 Buttons 时可以这样处理。

Create a Ripple interactionSource class创建Ripple 交互源 class

class NoRippleInteractionSource : MutableInteractionSource {

override val interactions: Flow<Interaction> = emptyFlow()

override suspend fun emit(interaction: Interaction) {}

override fun tryEmit(interaction: Interaction) = true

}

In case of a button, you can handle it by passing the ripple interaction class as the interactionSource parameter ie:如果是按钮,您可以通过将涟漪交互 class 作为interactionSource参数传递来处理它,即:

 Button(
    onClick = { /*...*/ },
    interactionSource = NoRippleInteractionSource()
) {
    //..
}

This solution works with all compossables that accept a mutableInteractionSource as a parameter for example Button() , TextButton() , Switch() , etc该解决方案适用于接受 mutableInteractionSource 作为参数的所有可组合项,例如Button()TextButton()Switch()

 modifier = Modifier
        .clickable(
            enabled = enabled,
            onClick = { if (enabled) onClick() }
        )

if you are using modifier clickable attribute on Column or any other component then you can use enabled attribute along with onClick to disable ripple effect when needed.如果您在 Column 或任何其他组件上使用修饰符可单击属性,则可以在需要时使用 enabled 属性和 onClick 来禁用涟漪效应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM