在 OpenHarmony(ArkUI) 中,ListView(API 9+ 一般用 List 组件)本身没有内建“列表项动画”属性,但可以通过 ArkUI 的动画能力 + 数据驱动 来实现常见动画效果。下面按常见场景给你讲做法和示例。
@State 管理列表数据animateTo 或组件动画@Animatable / animation 控制显隐和位移@Entry
@Component
struct ListAnimExample {
@State listData: string[] = ['A', 'B', 'C']
build() {
Column() {
Button('添加')
.onClick(() => {
animateTo({ duration: 300 }, () => {
this.listData.push(`Item ${this.listData.length}`)
})
})
List() {
ForEach(this.listData, (item: string) => {
ListItem() {
Text(item)
.fontSize(20)
.padding(10)
}
.animation({ duration: 300 })
}, (item: string) => item)
}
}
}
}
✅ 效果:
removeItem(index: number) {
animateTo({ duration: 300 }, () => {
this.listData.splice(index, 1)
})
}
配合:
ListItem()
.animation({ duration: 300 })
@Component
struct ExpandItem {
@State expanded: boolean = false
build() {
Column() {
Text('点击展开')
.onClick(() => {
animateTo({ duration: 300 }, () => {
this.expanded = !this.expanded
})
})
if (this.expanded) {
Text('详细信息')
.height(50)
.animation({ duration: 300 })
}
}
}
}
translatex: 100onAppear 中归位ListItem()
.onAppear(() => {
animateTo({ duration: 300 }, () => {
this.offsetX = 0
})
})
.translate({ x: this.offsetX })
✅ 使用 List + LazyForEach + animation
✅ 避免频繁 ForEach 全量刷新
❌ 不要在 ForEach 里用 index 作为 key
✅ 用唯一 id
ForEach(this.listData, item => item.id)
❌ 不要直接操作 DOM
✅ 一切通过 @State 驱动
OpenHarmony 目前:
DiffUtil 自动动画animateTo| 动画类型 | 实现方式 |
|---|---|
| 新增/删除 | animateTo + ListItem.animation |
| 展开 | height + animateTo |
| 滑动 | translate + onAppear |
| 复杂动画 | custom animation |
如果你告诉我:
我可以直接给你可运行完整 Demo。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。