Column,rememberScrollState,记住滚动位置

滚动效果:

scss 复制代码
@Composable
fun VerticalScrollableComponent(personList: List<Person>) {
    // We create a ScrollState that's "remember"ed  to add proper support for a scrollable component.
    // This allows us to also control the scroll position and other scroll related properties.

    // remember calculates the value passed to it only during the first composition. It then
    // returns the same value for every subsequent composition. More details are available in the
    // comments below.
    val scrollState = rememberScrollState()
    // Column is a composable that places its children in a vertical sequence.
    Column(
        modifier = Modifier.scrollable(
            state = scrollState,
            orientation = Orientation.Vertical
        )
    ) {
        for ((index, person) in personList.withIndex()) {
            // Row is a composable that places its children in a horizontal sequence. You
            // can think of it similar to a LinearLayout with the horizontal orientation.
            // In addition, we pass a modifier to the Row composable. You can think of
            // Modifiers as implementations of the decorators pattern that  are used to
            // modify the composable that its applied to. In this example, we configure the
            // Row to occupify the entire available width using Modifier.fillMaxWidth() and also
            // give it a padding of 16dp.
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(vertical = 16.dp, horizontal = 16.dp)
            ) {
                // Card composable is a predefined composable that is meant to represent the card surface as
                // specified by the Material Design specification. We also configure it to have rounded
                // corners and apply a modifier.
                Card(
                    shape = RoundedCornerShape(4.dp),
                    backgroundColor = colors[index % colors.size],
                    modifier = Modifier.fillMaxWidth()
                ) {
                    // Text is a predefined composable that does exactly what you'd expect it to
                    // display text on the screen. It allows you to customize its appearance
                    // using the style property.
                    Text(
                        person.name, style = TextStyle(
                            color = Color.Black,
                            fontSize = 20.sp,
                            textAlign = TextAlign.Center
                        ), modifier = Modifier.padding(vertical = 16.dp)
                    )
                }
            }
        }
    }
}

val scrollState = rememberScrollState()

  • 创建并记忆 一个 ScrollState(保存当前滚动位置、最大可滚范围等)。
  • remember 的原因是:在重组(recomposition)时复用同一个状态,避免每次重组滚动位置被重置。
  • 想在进程被回收后恢复滚动位置,可用 rememberSaveable()
ini 复制代码
val scrollState = rememberScrollState()
// Column is a composable that places its children in a vertical sequence.
Column(
    modifier = Modifier.scrollable(
        state = scrollState,
        orientation = Orientation.Vertical
    )
){}

滚动 vs 列表Column + verticalScroll 适合内容较少 、高度固定的简单页面;如果是长列表/大量数据 ,用 LazyColumn(惰性加载、性能好)。

相关推荐
by__csdn2 小时前
大前端:定义、演进与实践全景解析
前端·javascript·vue.js·react.js·typescript·ecmascript·动画
JS_GGbond2 小时前
前端工具链:从“厨房设备”到“开箱即用”的轻松之旅
前端
7***37452 小时前
前端体验的隐性力量:微交互、认知负担与情绪设计的技术实践思维
前端·交互
eason_fan2 小时前
一次 React 项目 lock 文件冲突修复:从 Hook 报错到 Vite 配置优化
前端·vite·前端工程化
彭于晏爱编程2 小时前
👊👊👊领导让我从vue转到react,我敲泥*
前端
毕设源码-钟学长2 小时前
【开题答辩全过程】以 基于Echarts的电商用户数据可视化平台设计与实现- -为例,包含答辩的问题和答案
前端·信息可视化·echarts
在黎明的反思2 小时前
c++20协程
java·前端·c++20
百罹鸟2 小时前
现如今的AI IDE:提示词策略与MCP Server使用感悟
前端·人工智能·mcp
徐同保2 小时前
Electron创建demo项目和打包
前端·javascript·electron
用户12039112947262 小时前
从原生 JS 到 Vue3 Composition API:手把手教你用现代 Vue 写一个优雅的 Todos 任务清单
前端·vue.js·面试