【问题标题】:Scroll Multiple LazyRows together - LazyHorizontalGrid alike?一起滚动多个惰性行 - 类似于惰性水平网格?
【发布时间】:2021-07-21 14:03:28
【问题描述】:

如何给两个LazyRows分配相同的滚动状态,让两行一起滚动?

Jetpack compose lists 目前没有 LazyHorizo​​ntalGrid,那么有什么替代解决方案吗?

Column{
    LazyRow(                                                        
        modifier = Modifier.fillMaxWidth()          
    ) {                                                              
        // My sublist1                                                           
    }
    LazyRow(                                                        
        modifier = Modifier.fillMaxWidth()                         
    ) {                                                              
        // My sublist2                                                          
    }
}                                                               

尝试在下面实现

【问题讨论】:

  • 为什么不使用单个LazyRow? ://
  • @nglauber 这是设计指南,必须以同样的方式进行。
  • 也许你可以使用LazyVerticalGrid
  • @SandroKakhetelidze 不,我需要水平滚动网格。应该有一些方法可以使这项工作。

标签: android android-jetpack-compose android-jetpack-compose-list


【解决方案1】:

我修改了 LazyVerticalGrid 类,使其仅适用于 GridCells.Fixed(n) 水平网格。

这里是完整的 gist 代码:LazyHorizontalGrid.kt

主要变化

@Composable
@ExperimentalFoundationApi
private fun FixedLazyGrid(
    nRows: Int,
    modifier: Modifier = Modifier,
    state: LazyListState = rememberLazyListState(),
    contentPadding: PaddingValues = PaddingValues(0.dp),
    scope: LazyGridScopeImpl
) {
    val columns = (scope.totalSize + nRows - 1) / nRows
    LazyRow(
        modifier = modifier,
        state = state,
        contentPadding = contentPadding,
    ) {
        items(columns) { columnIndex ->
            Column {
                for (rowIndex in 0 until nRows) {
                    val itemIndex = columnIndex * nRows + rowIndex
                    if (itemIndex < scope.totalSize) {
                        Box(
                            modifier = Modifier.wrapContentSize(),
                            propagateMinConstraints = true
                        ) {
                            scope.contentFor(itemIndex, this@items).invoke()
                        }
                    } else {
                        Spacer(Modifier.weight(1f, fill = true))
                    }
                }
            }
        }
    }
}

代码用法

LazyHorizontalGrid(
    cells = GridCells.Fixed(2)
) {
    items(items = restaurantsList){
        RestaurantItem(r = it, modifier = Modifier.fillParentMaxWidth(0.8f))
    }
}

【讨论】:

  • 您可以参考here。会更简单
  • @MARSK 不错,但有动画延迟。
  • 不,它不在我的设备上。它工作得非常顺利。你在用模拟器吗?
  • 我建议,因为修改是特定于LazyVerticalGrid,而链接提供了全方位的定制。
  • 好的。是的,它在模拟器上。如果它在模拟器上运行,它也可以确保在低端设备上运行良好。
猜你喜欢
  • 2022-12-15
  • 1970-01-01
  • 2015-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-17
  • 1970-01-01
  • 2013-11-28
相关资源
最近更新 更多