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

相关推荐
雾岛听风来几秒前
Android开发中常用高效数据结构
前端·javascript·后端
IT_陈寒几秒前
Vue 3性能优化实战:这5个Composition API技巧让你的应用快30%
前端·人工智能·后端
IT_陈寒8 分钟前
Vue3性能翻倍的5个秘密:从Composition API到Tree Shaking实战指南
前端·人工智能·后端
IT_陈寒22 分钟前
JavaScript 性能优化:3个V8引擎隐藏技巧让你的代码提速50%
前端·人工智能·后端
沐怡旸37 分钟前
【技术选型】前端框架:Vue vs React - 组合式API与Hooks的哲学之争
前端·面试
charlie1145141911 小时前
HTML 理论系统笔记2
前端·笔记·学习·html·基础·1024程序员节·原生
2501_938780281 小时前
Ionic + Angular 跨端实战:用 Capacitor 实现相机拍照功能并适配移动端
前端·数码相机·angular.js
m0_64880493_江哥1 小时前
Python实现随机选播视频的示例代码
前端·python·音视频
_大学牲1 小时前
禁止复制内容 🤔 那点事~
前端·chrome·程序员
申阳1 小时前
Day 1:耗时2小时,梳理并分享我作为全栈开发最依赖的“兵器库”
前端·后端·程序员