https://kotlinlang.org logo
#compose
Title
# compose
e

eygraber

03/03/2023, 4:53 AM
Is there any way to ignore certain
LazyColumn
items when applying a
verticalArrangement
? More details in 🧵
My use case is that I have a
LazyColumn
that has 1 item at the top that manages the spacing between it and the next item. I want all items after that to be spaced by
16.dp
. I tried creating a custom
Arrangement.Vertical
that in
arrange
manually handles the 1st item, and delegates to an
Arrangement.spacedBy(16.dp)
for the other items. The problem is that
LazyColumn
only calls
Arrangement.arrange
when there is "spare space" (i.e. when the items don't extend past its viewport). In order to get the proper spacing, I have to also delegate
spacing
to the
Arrangement.spacedBy(16.dp)
, but that applies to my first as well.
I could solve this by nesting the
LazyColumn
inside a vertically scrolling
Column
and put the first item in that
Column
but that comes with its own challenges, specifically the issue where a vertically scrollable component has an infinite maximum height. The exception even calls out this case explicitly:
Copy code
Vertically scrollable component was measured with an infinity maximum height constraints, which is disallowed. One of the common reasons is nesting layouts like LazyColumn and Column(Modifier.verticalScroll()). If you want to add a header before the list of items please add a header as a separate item() before the main items() inside the LazyColumn scope
s

shikasd

03/04/2023, 2:10 PM
I believe the canonical solution for this is:
Copy code
item {
    // First item with specific padding
    Spacer(Modifier.size(firstItemPadding)
}

items {
    // The rest of the list
}
You can also try and arrange items with Spacer/padding modifier without the arrangement
e

eygraber

03/05/2023, 1:35 AM
There's still an issue with horizontal content padding. I get the feeling that content padding and arrangements work well in a narrow set of cases, albeit that narrow set is probably the overwhelming majority of cases
s

shikasd

03/05/2023, 2:40 AM
Yeah, most built-in components are like this, e.g. there are cases when it is easier to build a custom Row instead of using the default one. Our north star is to make building custom Lazy layouts as easy as that, but we have a really long way to go because of sheer complexity they have
e

eygraber

03/05/2023, 3:35 AM
Luckily most of these things have acceptable workarounds (other than the spacedBy arrangement). In my particular use case these issues are arising at the intersection of infinite height LazyColumn nested in a scrollable Column. If it weren't for that, I would "hoist" the header into the scrollable column and it would solve all of my problems.
s

shikasd

03/06/2023, 12:12 PM
Making potentially infinite scrollable container work inside infinite constraints solves way less problems than it creates, unfortunately :D E.g. fling and whatever limited scrollbar support will be broken straight away
128 Views