简体   繁体   English

有没有办法在 Jetpack Compose 的垂直可滚动视图中嵌套 LazyVerticalGrid

[英]Is there a way to nest a LazyVerticalGrid iniside a vertical scrollable view in Jetpack Compose

I'm trying to show a LazyGrid of items within a main screen that has a scrollable content.我正在尝试在具有可滚动内容的主屏幕中显示项目的 LazyGrid。

I tried to set up the screen as follows:我尝试按如下方式设置屏幕:

@Composable
fun MainScreen(){

    ConstraintLayout(Modifier.verticalScroll(remeberScrollState())) {
    
    val (view, rowView, image, gridView) = createRefs()

    Row(){
    //view
}
    Row (){
    //rowView (a lazy row)
}
    Row() {
    //image
}
    Row() {
    //gridView
   }
 }


}

Compose had no issue with the other views but once I included the grid view, I had this error: Compose 对其他视图没有问题,但是一旦包含网格视图,我就遇到了这个错误:

java.lang.IllegalStateException: Vertically scrollable component was measured with an infinity maximum height constraints, which is disallowed. java.lang.IllegalStateException:垂直滚动组件是用无限大的最大高度限制测量的,这是不允许的。 One of the common reasons is nesting layouts like LazyColumn and Column(Modifier.verticalScroll()).常见原因之一是嵌套布局,如 LazyColumn 和 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.如果您想在项目列表之前添加 header,请在 LazyColumn scope 内的主要项目()之前添加一个 header 作为单独的项目()。发生这种情况可能还有其他原因:您的 ComposeView 已添加到具有一定权重的 LinearLayout,您应用了 Modifier.wrapContentSize(unbounded = true) 或编写了自定义布局。 Please try to remove the source of infinite constraints in the hierarchy above the scrolling container.请尝试删除滚动容器上方层次结构中无限约束的来源。

The LazyVerticalGrid is set up like so: LazyVerticalGrid 的设置如下:

list : List<Object>

LazyVerticalGrid( cells = GridCells.Fixed(2), content = {

items(list.size) { index ->

Card(){...}

}
})

Is there a way to nest a LazyVerticalGrid within any layout that is vertically scrollable?有没有办法在任何可垂直滚动的布局中嵌套 LazyVerticalGrid?

simple example:简单的例子:

ConstraintLayout(Modifier.verticalScroll(rememberScrollState())){
    LazyVerticalGrid( cells = GridCells.Fixed(2),Modifier.height(150.dp)){
        items(5){
            Text(text = "hello")
        }
    }
}

When a list is nested, the internal list needs to be fixed in height嵌套列表时,内部列表需要固定高度

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM