简体   繁体   English

如何在 jetpackCompose 中对齐 LazyColumn 内的不同项目

[英]How can I align different items inside LazyColumn in jetpackCompose

I am building a chat app with firebase and I need to align the chat bubbles in the end when I write the message and in the start when I receive, like in whatsapp.我正在用 firebase 构建一个聊天应用程序,我需要在最后写消息时和在开始时对齐聊天气泡,就像在 whatsapp 中一样。 If I use the horizontalArrangement in the lazyColumn it affects all the items.如果我在lazyColumn 中使用horizo​​ntalArrangement,它会影响所有项目。 I tried using the modifier.align in the chat bubbles but nothing happens.我尝试在聊天气泡中使用 modifier.align 但没有任何反应。 How can I do this?我怎样才能做到这一点?

below is my lazyColumn下面是我的lazyColumn

LazyColumn(
           modifier = Modifier.fillMaxWidth(),
            ) {
                                if (list != null && list.isNotEmpty()) {
                                    items(items = list) {


                                        if (it.user1id == args.userId) {
                                            ChatCard(
                                                message = it,
                                                color = Color.Magenta,
                                                modifier = Modifier
                                                    .align(Alignment.CenterHorizontally)
                                                    .padding(
                                                    start = 32.dp,
                                                    end = 4.dp,
                                                    top = 4.dp
                                                )
                                            )
                                        } else {
                                            ChatCard(
                                                message = it,
                                                color = Color.White,
                                                Modifier.padding(
                                                    start = 4.dp,
                                                    end = 32.dp,
                                                    top = 4.dp
                                                )
                                            )
                                        }

                                    }
                                }
                            }
@Composable
fun ChatCard(message: Message, color: Color, modifier: Modifier = Modifier){
    Card(
        modifier = modifier,
        backgroundColor = color,
        shape = RoundedCornerShape(10.dp)
    ) {
        Row(
            modifier = Modifier.padding(4.dp),
            horizontalArrangement = Arrangement.SpaceBetween
        ) {
            Text(
                modifier = Modifier
                    .padding(4.dp)
                    .widthIn(0.dp, 280.dp),
                text = message.message
            )
            Text(
                modifier = Modifier.padding(4.dp),
                text = message.time,
                style = TextStyle(
                    fontSize = 12.sp,
                    color = Color.LightGray
                )
            )
        }

    }
}

You can add a Row for each item applying a different horizontalArrangement removing the Modifier.align in your Card .您可以为应用不同的horizontalArrangement每个项目添加Row ,从而删除CardModifier.align

Something like:就像是:

   items(items = itemsList) {

            Row(Modifier.fillMaxWidth(),
              horizontalArrangement = if (it == ....)
                Arrangement.Start else
                    Arrangement.End) {

                if (it == ....) {
                    ChatCard(
                        color = Color.Magenta,
                        modifier = Modifier
                            .padding(
                                start = 32.dp,
                                end = 4.dp,
                                top = 4.dp
                            )
                    )
                } else {
                    ChatCard(
                        color = Color.White,
                        Modifier.padding(
                            start = 4.dp,
                            end = 32.dp,
                            top = 4.dp
                        )
                    )
                }
            }

在此处输入图片说明

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

相关问题 如何在 JetpackCompose 的 LazyColumn 中 select 多个项目 - How to select multiple items in LazyColumn in JetpackCompose 我如何在项目中传递 2 个列表(LazyColumn) - How I can pass 2 lists in items (LazyColumn) 如何在 Jetpack Compose 的 LazyColumn 中使用 LazyColumn? - How can I use LazyColumn in LazyColumn in jetpack compose? 我怎么知道可以导航“LazyColumn”? - How do I know a `LazyColumn` can be navigated? 如何让我的 Lazyrow 在 JetpackCompose 中自动滚动? - How can i make my Lazyrow to scroll automatically in JetpackCompose? 根据 android 中 LazyColumn 内的项目执行不同的操作,不同的 onClick 事件 - Perform different actions based upon items inside LazyColumn in android compose, different onClick events 如何在 jetpack compose 中向 LazyVerticalGrid 添加一个 stickyHeader,例如 LazyColumn? - How can I add a stickyHeader to LazyVerticalGrid like LazyColumn in jetpack compose? 重新启动动画时,如何重置 JetpackCompose AnimatedFloat 的初始值? - How can I reset the initial value of JetpackCompose AnimatedFloat, when I restart the animation? 使用 JetpackCompose LazyColumn,Compose-Espresso 链接变为空闲超时 - Compose-Espresso link to become idle timed out With JetpackCompose LazyColumn 我可以在 Android 的自定义视图中包装 JetpackCompose 吗? - Can I wrap a JetpackCompose in Android's Custom View?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM