在 OpenHarmony(ArkUI) 中,列表组件(如 List、Grid)的“刷新”本质上就是 数据变化 → UI 自动更新。下面按常见场景给你说明。
@State 驱动列表刷新只要列表数据源是 被 @State 装饰的状态变量,数据变化就会自动刷新。
@Component
struct MyList {
@State items: string[] = ['A', 'B', 'C']
build() {
Column() {
Button('刷新')
.onClick(() => {
// 修改数据,List 自动刷新
this.items = ['A', 'B', 'C', 'D', 'E']
})
List() {
ForEach(this.items, (item) => {
ListItem() {
Text(item)
}
})
}
}
}
}
✅ 关键点
ForEach 依赖数据源刷新this.items.push('F')
this.items = [...this.items]
this.items.splice(1, 1)
this.items = [...this.items]
⚠️ 直接
push有时不会触发刷新,建议重新赋值数组
OpenHarmony 没有内置下拉刷新组件,需要自己实现或使用组件。
List() {
// 下拉刷新区域
}
.onScrollIndex((start) => {
if (start === 0) {
// 触发刷新
}
})
或监听滑动:
.onTouch((e) => {
if (e.type === TouchType.Down) {
// 判断滑动位置
}
})
@Observed + @ObjectLink(对象列表)如果列表是对象数组:
@Observed
class Item {
name: string
constructor(name: string) {
this.name = name
}
}
@Component
struct ItemView {
@ObjectLink item: Item
build() {
Text(this.item.name)
}
}
@Entry
@Component
struct Page {
@State list: Item[] = [new Item('A'), new Item('B')]
build() {
List() {
ForEach(this.list, (item) => {
ListItem() {
ItemView({ item })
}
})
}
}
}
这样修改对象属性也能刷新。
LazyForEach(大数据量刷新)class MyDataSource extends BasicDataSource {
// 实现 getData / totalCount 等
}
List() {
LazyForEach(this.dataSource, (item) => {
ListItem() {
Text(item)
}
})
}
刷新时调用:
this.dataSource.notifyDataReload()
✅ 会刷新
@State 数组重新赋值notifyDataReload()@ObjectLink 对象属性变化❌ 不会刷新
如果你愿意,可以告诉我:
我可以给你 完整可直接运行的示例。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。