【问题标题】:Vertically scrollable component was measured with an infinity maximum height constraints, which is disallowed垂直滚动组件是用无限大的最大高度限制测量的,这是不允许的
【发布时间】:2023-02-22 05:15:12
【问题描述】:

我在我的 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.

我试图关注这个stack overflow,但没有成功

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" />

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>

  

viewholder.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)
    )
}

【问题讨论】:

    标签: android kotlin android-jetpack-compose android-jetpack lazycolumn


    【解决方案1】:

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

    【讨论】:

    • 所以我该怎么做 ?
    • 将 LazyColumn 替换为 Column。
    • 那我怎样才能制作嵌套的物品呢?
    • 列没有item 属性?
    • 我认为这里的问题是 BoxWithConstraints 将 Constraints.Infinity 作为 maxHeight 返回。检查从 BoxWithConstraints 获得的高度。允许使用具有固定高度的 LazyColumn,但不能使用无限约束。我同意 RecyclerView 和 LazyColumn 在一起是多余的
    【解决方案2】:

    在 Jetpack Compose 中制作嵌套的垂直列表。

    嘿,这很简单,我想通了..说我们有一个数据结构 (在科特林中)

    val allTransactions = List<SurfaceControl.Transaction>
    
    val monthlyTransaction = SurfaceControl.Transaction(monthName: String, monthlyTransactions: List< MonthlyTransactions>)
    
    val monthlyTransaction = MonthlyTransaction(amount: String, date: String)
    

    首先有一个 LazyColumn

    @Composable
    fun mainFunction (){
        LazyColumn {
            if (allTransactions.isNotEmpty()) {
                items(allTransactions.size) { index ->
                    // pass each model 
                    TransactionComposable(model = allTransactions[index])
                }
            }
        }
    }
    
    @Composable
    fun TransactionComposable(transaction: Transaction) {
        
        // make the title outside the column so it will be on top of the list
        Text(text = transaction.monthName)  
    
        Column {
            // make a for each loop inside the column
            transaction.monthlyTransactions.forEach { monthlyTransaction ->
                    // for each of your iteration, call your inner list item inside a a row
                    Row {
                        TransactionDetailComponent (monthlyTransaction)
                    }
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-28
      • 2023-03-13
      • 1970-01-01
      • 2020-10-12
      • 1970-01-01
      • 2016-05-07
      相关资源
      最近更新 更多