英文:
Nested Scrolling in Android compose
问题
我有一个列(Column),在列内有一个具有特定大小的间隔(Spacer)和一个懒加载列(LazyColumn)。
现在发生的情况是,当我想要滚动时,首先懒加载列(LazyColumn)开始滚动,当它滚动到底部时,列(Column)开始滚动到底部。
我想要的是首先列(Column)开始滚动,当它滚动到底部时,我的懒加载列(LazyColumn)开始滚动。
这是我的示例代码:
Column(
modifier = Modifier
.fillMaxHeight()
.verticalScroll(scrollState),
) {
Spacer(modifier = Modifier.height(300.dp))
LazyColumn(
modifier = Modifier
.height(LocalConfiguration.current.screenHeightDp.dp)
.fillMaxSize(),
) {
items(50) { item ->
Text(modifier = Modifier.height(40.dp), text = "ITEM NUMBER: $item")
}
}
}
英文:
I have a Column and inside the Column, I have a Spacer with a specific size and a LazyColumn.
What is happening now is that when I want to scroll, First LazyColumn starts to scrolling, and when it reaches the end, the Column starts scrolling to the end.
What I want is that first the Column starts to scroll and when it reaches the end, My LazyColumn starts to scroll.
This is my sample code
Column(
modifier = Modifier
.fillMaxHeight()
.verticalScroll(scrollState),
) {
Spacer(modifier = Modifier.height(300.dp))
LazyColumn(
modifier = Modifier
.height(LocalConfiguration.current.screenHeightDp.dp)
.fillMaxSize(),
) {
items(50) { item ->
Text(modifier = Modifier.height(40.dp), text = "ITEM NUMBER: $item")
}
}
}
答案1
得分: 1
在 LazyColumn 中将你的 Spacer 代码作为其中一个 item 添加,并移除 Column。
LazyColumn(modifier = Modifier.fillMaxSize()) {
item {
Spacer(modifier = Modifier.height(300.dp))
}
items(50) { item ->
Text(modifier = Modifier.height(40.dp), text = "项目编号:$item")
}
}
英文:
Add your Spacer code inside the LazyColumn as one of the item and remove Column.
LazyColumn(modifier = Modifier.fillMaxSize()) {
item {
Spacer(modifier = Modifier.height(300.dp))
}
items(50) { item ->
Text(modifier = Modifier.height(40.dp), text = "ITEM NUMBER: $item")
}
}
答案2
得分: 0
我不确定你的最终目标是什么,是否会在列中添加一些额外内容,但对于这个特定示例,我认为不需要嵌套滚动
下面的代码应该按照你想要的方式运行。它将滚动内容,包括现在由content padding
替换的间隔。希望对你有帮助!
LazyColumn(
modifier = Modifier.fillMaxSize(),
contentPadding = PaddingValues(top = 300.dp),
) {
items(50) { item ->
Text(
modifier = Modifier.height(40.dp),
text = "ITEM NUMBER: $item"
)
}
}
英文:
I am not sure, what your ultimate goal is, if you are going to have some additional content in the column or not but for this particular example, I don't think nested scrolling is needed
The code below should behave as you wanted. It will scroll the content including the spacer which is now replaced by the content padding
. Hope this helps!
LazyColumn(
modifier = Modifier.fillMaxSize(),
contentPadding = PaddingValues(top = 300.dp),
) {
items(50) { item ->
Text(
modifier = Modifier.height(40.dp),
text = "ITEM NUMBER: $item"
)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论