https://kotlinlang.org logo
Title
k

KotlinLeaner

04/05/2023, 8:58 PM
Hi guys, I am refactoring my xml code to compose. I have a fragment layout and using
ComposeView
by the help of documentation. I have a list which is more than 50 items to display. So I am trying to use
LazyColumn
inside
setContent
but it giving me error
Vertically scrollable component was measured with an infinity maximum height constraints....
. I know this error when we doing nested
LazyColumn
, but I am not using any component related to compose to this screen. So what should I do in here?
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="<http://schemas.android.com/apk/res/android|http://schemas.android.com/apk/res/android>"
    xmlns:app="<http://schemas.android.com/apk/res-auto|http://schemas.android.com/apk/res-auto>"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


<androidx.core.widget.NestedScrollView
    android:id="@+id/nestedScrollView"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/consentBanneryz">


    <androidx.compose.ui.platform.ComposeView
        android:id="@+id/itemComposable"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         />

//// more item in here

      </androidx.core.widget.NestedScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
binding.itemComposable.apply {
    setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
    setContent {
        XyzTheme {
            ListView(list)
        }
    }
}
@Composable
fun ListView(itemList: List<Int>?
) {
    AnimatedVisibility(visible = !itemList.isNullOrEmpty()) {
        LazyColumn(
            modifier = Modifier
                .fillMaxSize()
        ) {
            item { Text() }
        }
    }
}
Error
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.
o

orangy

04/05/2023, 11:25 PM
#compose-android ?
l

Lucca Beurmann

04/06/2023, 4:18 AM
this happens because the lazy column is nested inside a scroll view (which is also scrollable) so the runtime can't measure the height of this lazy column