Hi Folks, I have a ComposeView (with LazyRow cont...
# compose-android
d
Hi Folks, I have a ComposeView (with LazyRow content) embedded in a LinearLayout with a weight. And I get this error
Copy code
Horizontally scrollable component was measured with an infinity maximum width constraints, which is disallowed. One of the common reasons is nesting layouts like LazyRow and Row(Modifier.horizontalScroll()). 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 LazyRow scope. There are could be other reasons for this to happen: your ComposeView was added into a LinearLayout with some weight, you applied Modifier.wrapContentSize(unbounded = true) or wrote a custom layout. Please try to remove the source of infinite constraints in the hierarchy above the scrolling container.
I'm surprised this is an invalid combination. 🤔 My understanding is there are two passes to weighted children of linearLayouts, the first one passes 0 and then the second passes the appropriate width by sharing the available space with the weighted children However, on debugging (using BoxWithConstraints) it seems that on the initial pass the maxWidth passed to the ComposeView is Infinity. Which causes the error above. Is there any recommended approach for having Scrollable content embedded in linearLayouts with width? I have a hack which is using
Copy code
Modifier.widthIn(0.dp, 0.dp)
Which works because it is a more restrictive constraint than the (0, Infinity) constraint passed on the first measure phase of the linearlayout But seems weird that this is not supported by default.
r
the first one passes 0
In Views, measurement is done with a MeasureSpec, and it’s true LinearLayout will use 0 as the width, but it uses the spec called
UNSPECIFIED
Which basically means “tell me how big you want to be given no constraints”
d
Ah I see. That does makes sense. But ScrollableViews work well with weights. Should LazyRow by default interpret Infinity max constraints on main axis as 0.dp or is this expected to be handled by developers?
r
I don’t know, but
UNSPECIFIED
never meant
0
. Note also that Compose was designed to not have double measurement passes, so it might be related to that
The problem — I think, you’d have to ask the engineers on my team — is that a
LazyRow
in an infinite container would technically require to measure all possible items to figure out how big it wants to be (which of course goes against the concept of
LazyRow
in the first place)
Here’s something you can try though
Turn off baseline alignement on the
LinearLayout
That should bypass the infinity spec
d
The problem — I think, you’d have to ask the engineers on my team — is that a
LazyRow
in an infinite container would technically require to measure all possible items to figure out how big it wants to be (which of course goes against the concept of
LazyRow
in the first place)
Yeah. I wouldn’t expect this too. I’d more expect that it doesn’t compose any item at all when it receives infinite constraints.
Thanks. I’d try out your suggestion and revert.
Turn off baseline alignement on the
LinearLayout
Thanks @romainguy . This Worked
154 Views