android - Jetpack 在 LazyCloumn 中编写 LazyColumn

标签 android android-jetpack-compose lazycolumn

在我的主页中,我有一个lazycolumn,其中一项是水平寻呼机。在每个水平寻呼机中都有一些页面,我也需要在其中包含lazyColumn。错误是您不允许在同一方向使用嵌套滚动。我应该如何实现这个ui?

@Composable
fun Home() {
    LazyColumn {
        item { }
        item { }
        item { }

        item {
            TabRow(selectedTabIndex =) {

            }
        }
        item {
            HorizontalPager(count =) { page ->
                
                when {
                    page == 0 -> {
                        LazyColumn {
                            items(list) {
                                
                            }
                        }
                        
                    }
                }
            }
        }
    }
}

最佳答案

我创建了一个示例。对我来说,它按预期工作。请看一下

class ComposeActivity8 : ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            ComposeTutorialTheme {
                Home()
            }
        }
    }

    @Composable
    fun VItem(text: String) {
        Text(modifier = Modifier.padding(40.dp), text = text)
        Divider(color = Color.Black)
    }

    @Composable
    fun HItem(content: @Composable BoxScope.() -> Unit) {
        Box {
            content()
            Divider(
                color = Color.Red, modifier = Modifier
                    .align(Alignment.CenterEnd)
                    .fillMaxHeight()
                    .width(1.dp)
            )
        }
    }

    @Composable
    fun CreateLazyColumn(pos: String, countItem: Int) {
        LazyColumn {
            items(count = countItem, itemContent = { index ->
                VItem("$pos.$index")
            })
        }
    }

    @Composable
    fun Home() {
        LazyColumn {
            item { VItem("Vertical item 1") }
            item { VItem("Vertical item 2") }
            item { VItem("Vertical item 3") }
            item { VItem("Vertical item 4") }
            item {
                LazyRow(modifier = Modifier.height(150.dp)) {
                    item {
                        HItem {
                            CreateLazyColumn("Horizontal item 5.1", 6)
                        }
                    }
                    item {
                        HItem {
                            CreateLazyColumn("Horizontal item 5.2", 10)
                        }
                    }
                    item {
                        HItem {
                            Text(
                                modifier = Modifier.padding(40.dp),
                                text = "Horizontal item 5.3"
                            )
                        }
                    }
                    item {
                        HItem {
                            Text(
                                modifier = Modifier.padding(40.dp),
                                text = "Horizontal item 5.4"
                            )
                        }
                    }
                    item {
                        HItem {
                            CreateLazyColumn("Horizontal 5.5", 6)
                        }
                    }
                    item {
                        HItem {
                            Text(
                                modifier = Modifier.padding(40.dp),
                                text = "Horizontal item 5.6"
                            )
                        }
                    }
                    item {
                        HItem {
                            Text(
                                modifier = Modifier.padding(40.dp),
                                text = "Horizontal item 5.7"
                            )
                        }
                    }
                    item {
                        HItem {
                            Text(
                                modifier = Modifier.padding(40.dp),
                                text = "Horizontal item 5.8"
                            )
                        }
                    }
                }
                Divider(color = Color.Black)
            }
            item { VItem("Vertical item 6") }
            item { VItem("Vertical item 7") }
            item { VItem("Vertical item 8") }
            item { VItem("Vertical item 9") }
            item { VItem("Vertical item 10") }
            item { VItem("Vertical item 11") }
            item { VItem("Vertical item 12") }
        }
    }
}
视频
enter image description here

关于android - Jetpack 在 LazyCloumn 中编写 LazyColumn,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70401570/

相关文章:

android - 在compose中,为什么修改List元素的属性,LazyColumn不刷新

android - 我如何访问内部 Android 设备信息?

android - 发现GPU。供应商 ID 10de 设备(如果 0x1f07 检查错误的 AMD Vulkan 驱动程序版本)

android - Jetpack 中的权重组合

android - 如何在 CircularProgressIndicator 处于 Activity 状态时禁用交互?

kotlin - Alertdialog 中的 LazyColumn 在 Jetpack Compose 中显示错误

android - parse.com对Android的安全问题

java - 使用特定大小时如何使单行EditText正确绘制自身

android-jetpack-compose - 行内的 fillMaxHeight()

android - 当 LazyColumn 在后台时,撰写对话框关闭键盘外观