I've read through similar topics but I couldn't find satisfactory result:
- What is the equivalent of NestedScrollView + RecyclerView or Nested RecyclerView (Recycler inside another recycler) in Jetpack compose
- Jetpack Compose: How to put a LazyVerticalGrid inside a scrollable Column?
- Use lazyColum inside the column has an error in the Jetpack Compose
- Nested LazyVerticalGrid with Jetpack Compose
My use-case is: to create a comments' list (hundreds of items) with possibility to show replies to each comment (hundreds of items for each item).
Currently it's not possible to do a nested LazyColumn
inside another LazyColumn
because Compose will throw an exception:
java.lang.IllegalStateException: 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. 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.
The solutions provided by links above (and others that came to my mind) are:
- Using fixed height for internal
LazyColumn
- I cannot use it as each item can have different heights (for example: single vs multiline comment). - Using normal
Column
s (not lazy) insideLazyColumn
- performance-wise it's inferior to lazy ones, when using Android Studio's Profiler and list of 500 elements, normalColumn
would use 350MB of RAM in my app comparing to 220-240MB using lazyComposables
. So it will not recycle properly. - Using
FlowColumn
from Accompanist - I don't see any performance difference between this one and normalColumn
so see above. - Flatten the list's data source (show both comments and replies as "main" comments and only make UI changes to distinguish between them) - this is what I was currently using but when I was adding more complexity to this feature it prevents some of new feature requests to be implemented.
- Disable internal
LazyColumn
's scrolling using newly added in Compose 1.2.0userScrollEnabled
parameter - unfortunately it throws the same error and it's an intended behaviour (see here). - Using other ways to block scrolling (also to block it programatically) - same error.
- Using other
LazyColumn
's.height()
parameters likewrapContentHeight()
or usingIntrinsicSize.Min
- same error.
Any other ideas how to solve this?
item
/items
. Also this answer may be useful for building a dynamic items tree. – TowillforEachIndexed
with manual control ofitem
anditems
instead ofitemsIndexed
that I was using. However, it breaks pagination implemented like here asitemsIndexed
has different (user-aware) index value comparing toforEachIndexed
one). At the end, it seems I managed to mix both "mindsets" to useitemsIndexed
for comments andforEach
for replies. – Hilliekey
parameter ofitem
/items
to specify a unique identifier for each item, alsoitemContent
can improve performance if you specify different types depending on cell type (comment/reply) – TowillColumn
for the inner branches. – Cope