Nested Scrolling in Android compose

huangapple go评论50阅读模式
英文:

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

我不确定你的最终目标是什么,是否会在列中添加一些额外内容,但对于这个特定示例,我认为不需要嵌套滚动 Nested Scrolling in Android compose

下面的代码应该按照你想要的方式运行。它将滚动内容,包括现在由content padding替换的间隔。希望对你有帮助! Nested Scrolling in Android compose

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 Nested Scrolling in Android compose

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! Nested Scrolling in Android compose

LazyColumn(
    modifier = Modifier.fillMaxSize(),
    contentPadding = PaddingValues(top = 300.dp),
) {
    items(50) { item ->
        Text(
            modifier = Modifier.height(40.dp),
            text = "ITEM NUMBER: $item"
        )
    }
}

huangapple
  • 本文由 发表于 2023年5月14日 23:28:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76248274.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定