9 Star 0 Fork 0

yanshuifeng/repeat_feature_demo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
3_repeat_objects.ets 1.67 KB
一键复制 编辑 原始数据 按行查看 历史
denis.pikalov 提交于 2024-06-10 10:24 . Add tests
@ObservedV2 Item {
private static nextId = 0;
id : number;
@Trace s : string;
constructor() {
this.id = Item.nextNumber++;
this.s = this.id.toString(); // in reality more complex
}
}
@Entry
@ComponentV2
struct Index {
// next item to add to arr, ensure items are unique.
@Local arr : Array<Item> = [ new Item(), new Item(), new Item(), new Item(), new Item(), new Item() ]
@Local bgColor : Array<string> = [ Color.White, Color.Gray ];
build() {
Column() {
Text('3_repeat_objects.ets')
List() {
Repeat(this.arr)
.virtualScroll()
.every(( repeatItem ) => {
ListItem() {
Button("Replace") {
Text(repeatItem.item.s)
}
.onClick(() => {
// update object property without marking array arr as changed
repeatItem.item.s = "A" + repeatItem.item.s;
})
.height(100)
}
// 2 visible items at a time
// for simplicity assume no offscreen items
.width("100%").height(200)
// can add to explain with index
.backgroundColor(this.bgColor[repeatItem.index % 2])
})
.key(item => item.id.toString())
} // List
Button("Append new item")
.onClick(() => { this.arr.push( new Item()); })
Button("Delete last item")
.onClick(() => { this.Array.pop(); })
Button("Update item index #1")
.onClick(() => { this.arr[1]= new Item(); })
Button("Replace item index #1 with two new")
.onClick(() => { this.arr.splice(1, 1, new Item(), new Item() ); })
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/yan-shuifeng/repeat_feature_demo.git
git@gitee.com:yan-shuifeng/repeat_feature_demo.git
yan-shuifeng
repeat_feature_demo
repeat_feature_demo
master

搜索帮助