简体   繁体   English

如何使用 for 循环使行可滚动 | Jetpack 撰写

[英]How to make Row Scrollable with for loop | Jetpack Compose

I am making a project with Jetpack Compose.我正在使用 Jetpack Compose 制作一个项目。 I want to display comments like there are in Instagram.我想像 Instagram 一样显示评论。 There is an array that contains comments.有一个包含评论的数组。

This is a code used to display comments:这是用于显示评论的代码:

            val i : Int
            for(i in 1..user.count) {
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(10.dp),
                    horizontalArrangement = Arrangement.SpaceBetween
                ) {
                    Row(
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        Image1(
                            painter = painterResource(id = user.pp),
                            contentDescription = "PP",
                            modifier = Modifier
                                .clip(CircleShape)
                                .size(50.dp)

                        )
                        Spacer(modifier = Modifier.width(10.dp))
                        Column() {
                            Row() {
                                Text(text = user.name, color = Color.Black, fontSize = 20.sp)
                                Spacer(modifier = Modifier.width(10.dp))
                            }
                            Spacer(modifier = Modifier.width(10.dp))
                            Text(text = "Public", color = Color.DarkGray, fontSize = 13.sp)
                        }

                    }
                    IconButton(onClick = { /*TODO*/ }) {
                        Icon(
                            painter = painterResource(id = R.drawable.ic_baseline_more_vert_24),
                            contentDescription = "More"
                        )
                    }

                }
                Row() {
                    Spacer(modifier = Modifier.width(10.dp))
                    Text(
                        text = user.c[i-1],
                        color = Color.Black,
                        fontSize = 16.sp,
                        modifier = Modifier.padding(end = 10.dp)
                    )
                }
                Spacer(modifier = Modifier.height(10.dp))
                Row() {
                    var isClicked by remember {
                        mutableStateOf(false)
                    }
                    Spacer(modifier = Modifier.width(10.dp))
                    Icon(
                        painter = painterResource(
                            id =
                            if (!isClicked) R.drawable.like_in_comments else R.drawable.like
                        ),
                        contentDescription = "Like",
                        tint = Color.Blue,
                        modifier = Modifier
                            .size(25.dp)
                            .clickable { isClicked = !isClicked }
                    )
                    Spacer(modifier = Modifier.width(10.dp))
                    Text(
                        text = "Like",
                        color = Color.DarkGray,
                        fontSize = 16.sp,
                    )
                }
                Spacer(modifier = Modifier.height(10.dp))
                Divider()
            }

I want to make it scrollable.我想让它滚动。 I can use LazyRow.我可以使用 LazyRow。 When I use it, I get some errors.当我使用它时,我会遇到一些错误。 How do I implement it?我该如何实施? Please help.请帮忙。

You can make an ordinary Row scrollable by supplying its Modifier.horizontalScroll() with a scroll state like the following您可以通过为其Modifier.horizontalScroll()提供滚动条 state 来使普通Row可滚动,如下所示

val scrollState = rememberScrollState()

Row (modifier = Modifier.horizontalScroll(scrollState)) {
         ... 
}

But for a simple LazyRow without having a unique key , you don't need to iterate each item via a loop construct (eg a for-loop ), you just have to simply call items and pass the list.但是对于没有唯一key的简单LazyRow ,您不需要通过循环结构(例如for-loop )迭代每个项目,您只需简单地调用items并传递列表。

val itemList = listOf("Item1", "Item2")
    
LazyRow {
    items(itemList) { item ->
        // your composable here
    }
}

For a LazyRow with a unique key (assuming you have a class with an " id " attribute)对于具有唯一键的LazyRow (假设您有一个带有“ id ”属性的 class)

val people = listOf(Person(1, "person1"), Person(2, "person2"))

LazyRow {
    items(items = people, key = { item -> person.id }) { person->
        // your composable here
    }
}

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

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