在 OpenHarmony(ArkUI)里,“ListView” 通常指 List + ForEach/LazyForEach 的列表场景。性能调优的核心目标是:减少渲染节点、避免重复构建、按需加载、降低主线程压力。下面从「必做项 → 进阶项 → 常见坑」系统说明。
List() {
ForEach(this.dataList, (item) => {
ListItem() {
MyItem({ item })
}
})
}
List() {
LazyForEach(this.dataSource, (item) => {
ListItem() {
MyItem({ item })
}
}, item => item.id)
}
要点:
LazyForEach 只渲染可视区域 + 少量缓存dataSource 要实现 IDataSourceList() {
...
}
.cachedCount(5)
❌ 避免:
.onScroll(() => {
this.scrollOffset = ...
})
✅ 如果必须:
onScrollIndexonReachEnd 做分页如果 item 高度不固定:
✅ 推荐:
ListItem() {
...
}
.height(80)
或使用:
List() {
...
}
.lanes(1)
❌ 不推荐:
Column() {
Row() {
Column() {
Text()
}
}
}
✅ 扁平结构:
Row() {
Text()
Text()
}
@Builder
itemBuilder(item: Item) {
Row() {
Text(item.name)
}
}
LazyForEach(this.dataSource, (item) => {
ListItem() {
this.itemBuilder(item)
}
})
Image 的 .objectFit()cachedCount + LazyForEachImage(item.url)
.width(60)
.height(60)
.objectFit(ImageFit.Cover)
✅ 最好用 pixelMap / 缩略图
❌ 错误:
@State dataList: Item[] = []
改一个字段 → 整个 List 重绘
✅ 使用:
@Observed + @ObjectLink@Stateclass Item {
name: string = ''
}
@Observed
class ItemModel {
list: Item[] = []
}
❌
Text(this.computeHeavy())
✅
aboutToAppear() {
this.result = this.computeHeavy()
}
@Animatableopacity 频繁变化.onReachEnd(() => {
this.loadMore()
})
✅ 使用 LazyForEach
✅ 稳定 key
✅ 固定 item 高度
✅ 控制 cachedCount
✅ 扁平组件结构
✅ 图片压缩
✅ 局部状态更新
✅ 分页加载
如果你愿意,可以:
我可以直接帮你做针对性优化方案。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。