【问题标题】:Jetpack Compose: How to put a LazyVerticalGrid inside a scrollable Column?Jetpack Compose:如何将 LazyVerticalGrid 放入可滚动列中?
【发布时间】:2021-06-10 10:53:08
【问题描述】:

当尝试将 LazyVerticalGrid 放入 可滚动 Column 中时,我收到以下错误:

java.lang.IllegalStateException: 在同一个嵌套可滚动 方向布局,如 LazyColumn 和 不允许使用 Column(Modifier.verticalScroll())。如果你想添加一个 项目列表前的标题请查看 LazyColumn 具有 DSL api 的组件,允许首先通过 item() 函数,然后通过 items() 获取项目列表。

我没有制作传统的列表,我只是有很多元素太大而无法在屏幕上显示。因此,我希望列滚动,以便我可以看到所有元素。这是我的代码:

@ExperimentalFoundationApi
@Composable
fun ProfileComposable(id: String?) {
    val viewModel: ProfileViewModel = viewModel()
    if (id != null) {
        viewModel.getProfile(id)
        val profile = viewModel.profile.value
        val scrollState = rememberScrollState()
        if (profile != null) {
            Column(modifier = Modifier
                .fillMaxWidth()
                .fillMaxHeight()
                .verticalScroll(scrollState)) {
                Row() {
                    ProfilePic(profile.getImgUrl(), profile.name)
                    Column(Modifier.padding(16.dp)) {
                        ProfileName(profile.name)
                        Stats(profile.stats) //      <--------------- the offending composable
                    }
                }
                Sprites(sprites = profile.sprites)
                TextStat(profile.id.toString(), "Pokemon Number")
                TextStat(profile.species.name, "Species")
                TextStat(profile.types.joinToString { it.type.name }, "Types")
                TextStat(profile.weight.toString(), "Weight")
                TextStat(profile.forms.joinToString { it.name }, "Forms")
            }
        } else {
            Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
                CircularProgressIndicator()
            }
        }
    } else {
        Text("Error")
    }
} 

Stats() 可组合包含导致错误的LazyVerticalGrid

@ExperimentalFoundationApi
@Composable
fun Stats(stats: List<Stat>) {
    LazyVerticalGrid(cells = GridCells.Fixed(2)) {
        itemsIndexed(stats) { index, item ->
            StatBox(stat = item)
        }
    }
}

我不想让网格滚动,我只想在可滚动的列中显示一个网格。

【问题讨论】:

  • 你能检查一下这个问题是否对你有帮助吗? stackoverflow.com/questions/66908737/…
  • @nglauber 不幸的是没有。用item{}LazyColumn 包裹它不起作用,我得到同样的错误。
  • @nglauber 是的,它不起作用,必须使用列和行组合。
  • @KesWalker 找到任何解决方案了吗?
  • @ArtemGarkusha 不。

标签: android kotlin android-jetpack-compose


【解决方案1】:

我自己也遇到了这个问题。正如@gaohomway 所说,您最好的选择是使用 Google 的 Accompanist library 中的实验性 FlowRow()。

这里是一个工作代码 sn-p 作为示例:

@Composable
fun ProfileScreen2() {
    LazyColumn(
        modifier = Modifier
            .fillMaxSize()
    ) {
        item {
            Box(modifier = Modifier.fillMaxWidth().height(200.dp).background(color = Red))
        }

        item {
            Box(modifier = Modifier.fillMaxWidth().height(200.dp).background(color = Gray))
        }

        item {
            FlowRow() {
                SampleContent()
            }
        }
    }
}

@Composable
internal fun SampleContent() {
    repeat(60) {
        Box(
            modifier = Modifier
                .size(64.dp)
                .background(Blue)
                .border(width = 1.dp, color = DarkGray),
            contentAlignment = Alignment.Center,
        ) {
            Text(it.toString())
        }
    }
}

显示这个可滚动页面(不要介意底部的导航栏):

【讨论】:

    【解决方案2】:

    原因

    不允许在相同方向的布局中嵌套可滚动,如 LazyColumn 和 Column(Modifier.verticalScroll())。

    暂时找不到LazyVerticalGrid禁止滚动

    替代品

    来自Android官方Jetpack Compose Flow Layouts的替代库

    FlowRow {
        // row contents
    }
    
    FlowColumn {
        // column contents
    }
    
    

    【讨论】:

      【解决方案3】:

      我有一个类似的用例,目标是设计一个个人资料屏幕,顶部有一堆信息和统计数据,然后在屏幕底部以网格的形式显示帖子。

      我最终对整个列表使用了 LazyVerticalGrid,并为需要填满整个屏幕的项目设置了全跨度:

      LazyVerticalGrid(cells = GridCells.Fixed(3)) {
          item(span = { GridItemSpan(3) }) { TopInfo() }
          item(span = { GridItemSpan(3) }) { SomeOtherInfo() }
          item(span = { GridItemSpan(3) }) { BottomInfo() }
          items(gridItems) { GridItemView(it) }
      }
      

      【讨论】:

      • 如果项目位于项目下方,这将最小化项目的内容
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-26
      • 1970-01-01
      • 2022-08-17
      • 1970-01-01
      • 2023-01-07
      • 2022-12-15
      相关资源
      最近更新 更多