Ali Albaali
11/10/2021, 5:59 PMLazyColumn but it's prohibited, so now I'm using a Column and loop thru items but that's not the most optimal solution. Any ideas?Bradleycorn
11/10/2021, 6:27 PM/** The amount of indentation space that replies should use **/
private val REPLY_INDENTING = 16.dp
@Composable
fun Comment(text: String, modifier: Modifier = Modifier) {
// Include whatever you need here to render a comment.
// For this basic example, just show the comment text.
Text(text, modifier = modifier)
}
@Composable
fun Reply(replyText: String, depth: Int = 1, modifier: Modifier = Modifier) {
// we want replies to be "indented".
// This basic setup will just use padding to achieve the indenting.
// We'll multiply the reply depth by the indenting amount to get the
// proper amount of indentation for this item.
val indentation = REPLY_INDENTING * depth
Text(replyText, modifier = modifier.padding(start = indentation))
}
Then, I’d build a function scoped to a LazyList that will render a list of replies with indenting. It’ll be recursive so it can show replies to replies. This function does NOT need to be a Composable
fun LazyListScope.Replies(
replies: List<Reply>,
depth: Int = 1, // Used as a multiplier to calculate indenting
modifier: Modifier = modifier) {
replies.forEach { reply ->
item {
Reply(
reply.text,
depth = depth,
modifier = modifier)
}
Replies(
replies = reply.replies,
depth = depth + 1,
modifier = modifier)
}
}
Finally, I can build my main comments (lazy) column:
@Composable
fun CommentList(comments: List<Comment>, modifier: Modifier = Modifier) {
LazyColumn(modifier = modifier) {
comments.forEach { comment ->
item {
Comment(comment.text, modifier = Modifier.fillMaxWidth())
}
Replies(
replies = comment.replies,
depth = 1,
modifier = Modifier.fillMaxWidth())
}
}
}
I’m no expert, and YMMV ….Ali Albaali
11/10/2021, 6:31 PMLazyColumn .Bradleycorn
11/10/2021, 6:32 PM@Composable annotation on the LazyListScope.Replies functionBradleycorn
11/10/2021, 6:33 PMBradleycorn
11/10/2021, 6:34 PMitem method takes a lambda, and THAT is a @Composable where things get renderedAli Albaali
11/10/2021, 6:34 PMAli Albaali
11/10/2021, 6:36 PMitems extension function?Bradleycorn
11/10/2021, 6:38 PMitems instead of a forEach -> itemBradleycorn
11/10/2021, 6:41 PMitems method, then each comment AND it’s replies are an “item” and will be treated as a unit … With the approach i took, the comment is an “item”, and each reply is another “item”. Each is independent and stands on it’s own. So that will affect behavior a little bit. I don’t know that either one is “more correct”. It just depends on how you want it to behave.Bradleycorn
11/10/2021, 6:49 PMAli Albaali
11/10/2021, 7:09 PMitems(comments) { comment ->
CommentItem(comment)
this@comment.replies(comment.replies)
}
fun LazyListScope.replies(replies: List<Comment>) {
items(replies) { reply ->
CommentItem(reply)
this@replies.replies(reply.replies)
}
}
Do you have any idea why?Ali Albaali
11/10/2021, 7:21 PMAli Albaali
11/14/2021, 4:08 AMBradleycorn
11/14/2021, 1:37 PMDivider composable that does just that.
Divider is a vertical separator (it draws a horizontal line). If you want a vertical line, I’m not sure of anything “out of the box” that does that. But, you could write your own VerticalDivider pretty easily. If you look at the code for Divider it’s just a Box with some modifiers to set it’s size and background color. You could do the same thing, just invert the sizing modifiers (instead of .fillMaxWidth.height(..), do fillMaxHeight().width(..))Ali Albaali
11/14/2021, 2:53 PMDivider composable. However I'm asking about how to add lines to replies so they look like a thread, similar to padding for items.Bradleycorn
11/15/2021, 2:55 PMDivider and write my own VerticalLine composable to do it. Then make each reply a Row with a VerticalLine and the reply content.Ali Albaali
11/15/2021, 7:37 PM