kotlin 填充LazyVerticalGrid项目中的剩余空间

dsf9zpds  于 2023-08-06  发布在  Kotlin
关注(0)|答案(1)|浏览(216)

我正在使用LazyVerticalGrid来显示包含动态内容的项目,但我希望无论项目大小如何,都始终具有相同的高度。如何实现这一点?现在,即使列包含fillMaxHeight修饰符,结果也是一样的。

  1. @Composable
  2. fun VerticalGrid() {
  3. val products = listOf(
  4. Product("Product 11111111111111", "100"),
  5. Product("Product 2222222222222222", "200"),
  6. Product("Product 3", "300"),
  7. Product("Product 4", "400"),
  8. Product("Product 5", "500"),
  9. Product("Product 6", "600"),
  10. Product("Product 7", "700"),
  11. Product("Product 8", "800")
  12. )
  13. LazyVerticalGrid(
  14. cells = GridCells.Fixed(2),
  15. verticalArrangement = Arrangement.spacedBy(dimensionResource(R.dimen.size_small)),
  16. horizontalArrangement = Arrangement.spacedBy(dimensionResource(R.dimen.size_small)),
  17. modifier = Modifier.background(colorResource(id = R.color.light_gray_background))
  18. ) {
  19. itemsIndexed(products) { _, item ->
  20. Column(
  21. modifier = Modifier
  22. .background(Color.White)
  23. .padding(16.dp)
  24. .fillMaxHeight()
  25. ) {
  26. Text(text = item.title)
  27. Text(text = item.price)
  28. }
  29. }
  30. }
  31. }

字符串


的数据

am46iovg

am46iovg1#

将LazyVerticalGrid更改为LazyVerticalStaggeredGrid
https://developer.android.com/jetpack/compose/lists#lazy-staggered-grid

  1. LazyVerticalStaggeredGrid(
  2. columns = StaggeredGridCells.Adaptive(200.dp),
  3. verticalItemSpacing = 4.dp,
  4. horizontalArrangement = Arrangement.spacedBy(4.dp),
  5. content = {
  6. items(randomSizedPhotos) { photo ->
  7. AsyncImage(
  8. model = photo,
  9. contentScale = ContentScale.Crop,
  10. contentDescription = null,
  11. modifier = Modifier.fillMaxWidth().wrapContentHeight()
  12. )
  13. }
  14. },
  15. modifier = Modifier.fillMaxSize()

字符串

展开查看全部

相关问题