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

相关推荐
晴殇i10 小时前
用户登录后,Token 到底该存哪里?从懵圈到精通的全方位解析
前端·面试
零一科技10 小时前
Vue3学习第七课:(Vuex 替代方案)Pinia 状态管理 5 分钟上手
前端·vue.js
吃饺子不吃馅10 小时前
受够了 create-xxx?我写了一个聚合主流框架的脚手架
前端·面试·架构
Achieve前端实验室11 小时前
深度解析 JavaScript 作用域与作用域链
前端·javascript·面试
讨厌下雨的天空11 小时前
缓冲区io
linux·服务器·前端
xhxxx11 小时前
《深入理解 JavaScript 对象:属性命名、访问与遍历的那些细节》
前端·javascript
BBB努力学习程序设计11 小时前
Web App开发基础知识:从零构建现代化Web应用
前端·html
BBB努力学习程序设计11 小时前
使用Bootstrap框架搭建简单页面:快速构建现代化网站
前端·html
西洼工作室11 小时前
CSS响应式布局全攻略
前端·css·响应式布局·栅格系统
用户938169125536011 小时前
VUE3项目配置
前端