垂直可滚动组件的测量具有无限大的最大高度限制,这是不允许的

Viv*_*odi 25 android kotlin android-jetpack android-jetpack-compose lazycolumn

我在 recyclerview 项目布局中使用 ComposeView 来与 jetpack compose 一起使用。当我打开屏幕时遇到奇怪的问题

错误

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.
Run Code Online (Sandbox Code Playgroud)

我尝试跟踪这个堆栈溢出,但没有成功

main_activity.xml

      <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="parent" />
Run Code Online (Sandbox Code Playgroud)

item_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <androidx.compose.ui.platform.ComposeView
        android:id="@+id/itemComposable"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

</androidx.constraintlayout.widget.ConstraintLayout>

  
Run Code Online (Sandbox Code Playgroud)

视图持有者.kt

class OptionsViewHolder(val binding: ItemLayoutBinding) : Recyclerview.ViewHolder(binding.root) {

    private val context = binding.root.context

    companion object {
        fun from(parent: ViewGroup): OptionsViewHolder {
            return OptionsViewHolder(
                ItemLayoutBinding.inflate(
                    LayoutInflater.from(parent.context),
                    parent,
                    false
                )
            )
        }
    }

    fun bindChoice() {
        binding.itemComposable.setContent {
            BoxWithConstraints {
                LazyColumn(
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(this@BoxWithConstraints.maxHeight)
                        .verticalScroll(rememberScrollState())
                ) {
                    items(getOptions()) { option ->
                        Text(text = option)
                    }
                }
            }
        }
    }

    private fun getOptions() = mutableListOf(
        context.getString(R.string.monitor),
        context.getString(R.string.pressure),
        context.getString(R.string.not_sure)
    )
}
Run Code Online (Sandbox Code Playgroud)

Cod*_*oet 10

您正在LazyColumna 中使用 a RecyclerView,这是不允许的。ALazyColumn相当于RecyclerViewCompose 中的 a。所以你正在嵌套RecyclerViews或LazyColumns。

  • 我认为这里的问题是 BoxWithConstraints 返回 Constraints.Infinity 作为 maxHeight。检查从 BoxWithConstraints 获得的高度。允许使用具有固定高度的 LazyColumn,但不允许使用无限约束。我同意 RecyclerView 和 LazyColumn 在一起是多余的 (3认同)
  • 所以我该怎么做 ? (2认同)