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(惰性加载、性能好)。

相关推荐
小画家~6 分钟前
vue3 里面的 itemList.reduce
前端·数据结构·算法
锋行天下22 分钟前
langgraph 循环节点的静态和动态实现
前端·后端
keyipatience40 分钟前
JS学习笔记(2)数组,函数,作用域,对象
前端·javascript·笔记·学习·html
2603_9651481142 分钟前
商品价格监控 API 接口:从原理到实战的完整指南
java·前端·数据库·python·生活
酷虎软件1 小时前
视频加字幕怎么制作
java·前端·音视频
Geoffwo1 小时前
robotjs0.6.0鼠标中轴滚动失效修复方案
前端·javascript
彭亚川Allen1 小时前
从零手搓 RAG 之文档解析技术
前端·后端·架构
oicola2 小时前
Ant Design v5 vs Tailwind CSS:组件库与样式框架的终极对决
前端·css
溪语流沙2 小时前
【Web全栈进阶】Vue Router + Pinia:SPA路由与全局状态
前端·javascript·vue.js
ManageEngineITSM2 小时前
工单优先级怎么定?影响×紧急度矩阵的设计与落地方法
java·大数据·运维·前端·数据库·变更管理