一起滚动多个 LazyRows - LazyHorizo​​ntalGrid 一样吗?

时间:2021-07-21 14:03:28

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

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

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

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

尝试在下面实施

sample horizontally scroll image

2 个答案:

答案 0 :(得分:0)

Column{
    val state = rememberLazyListState
    LazyRow(                                                        
        state = state,
       modifier = Modifier.fillMaxWidth()          
    ) {                                                              
        // My sublist1                                                           
    }
    LazyRow(    
        state = state                                                    
        modifier = Modifier.fillMaxWidth()                         
    ) {                                                              
        // My sublist2                                                          
    }
}

答案 1 :(得分:0)

我修改了 LazyVerticalGrid 类,使其仅适用于 import pandas as pd import numpy as np df = pd.DataFrame({ 'hands': [ np.array(['S2', 'S3', 'S4', 'S5', 'H7']), np.array(['S2', 'S3', 'S4', 'S5', 'C7']), ], 'value': [7.05432, 3.1415], }) find = ['S2', 'S3', 'S4', 'S5', 'H7'] #find = np.array(['S2', 'S3', 'S4', 'S5', 'H7']) def check(row): return all(row == find) mask = df.hands.apply(check) print( df[ mask ].value ) mask = df.hands.apply(lambda row:all(row == find)) print( df[ mask ].value ) #check = lambda row:all(row == find) #mask = df.hands.apply(check) #print( df[ mask ].value ) 水平网格。

这是完整的要点代码:LazyHorizontalGrid.kt

lazyhorizontalgrid

主要变化

GridCells.Fixed(n)

代码使用

@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))
                    }
                }
            }
        }
    }
}