微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Jetpack Compose onClick 涟漪不是以圆周运动传播?

如何解决Jetpack Compose onClick 涟漪不是以圆周运动传播?

如gif所示

enter image description here

当包含 Text、Spacer 和 ColumnLazyRowForIndexed 被触摸时,波纹不会以圆周运动传播。甚至在水平列表被触摸时也有触摸效果

@Composable
fun Chip(modifier: Modifier = Modifier,text: String) {
    Card(
        modifier = modifier,border = Borderstroke(color = Color.Black,width = Dp.Hairline),shape = RoundedCornerShape(8.dp)
    ) {
        Row(
            modifier = Modifier.padding(start = 8.dp,top = 4.dp,end = 8.dp,bottom = 4.dp),verticalAlignment = Alignment.CenterVertically
        ) {
            Box(
                modifier = Modifier.preferredSize(16.dp,16.dp)
                    .background(color = MaterialTheme.colors.secondary)
            )
            Spacer(Modifier.preferredWidth(4.dp))
            Text(text = text)
        }
    }
}

@Composable
fun TutorialSectionCard(model: TutorialSectionModel) {

    Column(
        modifier = Modifier
            .padding(top = 8.dp)
            .clickable(onClick = { /* Ignoring onClick */ })
            .padding(16.dp)
    ) {

        Text(text = model.title,fontWeight = FontWeight.Bold,style = MaterialTheme.typography.h6)
        Spacer(Modifier.preferredHeight(8.dp))
        Providers(AmbientContentAlpha provides ContentAlpha.medium) {
            Text(model.description,style = MaterialTheme.typography.body2)
        }
        Spacer(Modifier.preferredHeight(16.dp))
        LazyRowForIndexed(items = model.tags) { _: Int,item: String ->
            Chip(text = item)
            Spacer(Modifier.preferredWidth(4.dp))
        }
    }
}

@Preview
@Composable
fun TutorialSectionCardPreview() {

    val model = TutorialSectionModel(
        clazz = MainActivity::class.java,title = "1-1 Column/Row Basics",description = "Create Rows and Columns that adds elements in vertical order",tags = listof("Jetpack","Compose","Rows","Columns","Layouts","Text","Modifier")

    )

    Column {
        TutorialSectionCard(model)
        TutorialSectionCard(model)
        TutorialSectionCard(model)
    }
}

应该怎么做才能产生循环效果,而不是在触摸或滚动列表本身或列表中的项目时?

解决方法

你必须为你的组合应用一个主题,它反过来提供一个默认的涟漪工厂,或者你必须明确地设置涟漪:

@Preview
@Composable
fun TutorialSectionCardPreview() {
    MaterialTheme() {
        Column {
            TutorialSectionCard
            ...
        }
    }
}

Column(
        modifier = Modifier
            .padding(top = 8.dp)
            .clickable(
                onClick = { /* Ignoring onClick */ },indication = rememberRipple(bounded = true)
            )
            .padding(16.dp)
    ) {
      // content
    }

(从 compose 版本 1.0.0-alpha09 开始,似乎无法防止滚动内容时出现波纹)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。