9 Star 0 Fork 0

yanshuifeng/repeat_feature_demo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
2_list_sharedarr_simple.ets 5.95 KB
一键复制 编辑 原始数据 按行查看 历史
/**
* Dynamic Management of ClassA Objects in a Shared Array
*
* Author: Vidhya Pria Arunkumar <vidhya.pria.arunkumar@huawei.com>
*
* Description:
* This testcase demonstrates the usage of sharing a common array across two Repeat items
*
* Classes:
* - ClassA: Represents an object with an auto-incremented id and a counter.
* - Constructor initializes the ClassA instance with a counter value.
*
* Components:
* - RepeatComp: Main component managing the list operations on ClassA objects.
* - Scrollable view with a Column layout containing two separate Repeat lists sharing the same array.
* - Buttons for performing operations like adding, deleting, swapping, and updating items in the list.
* - Repeat components iterate over the arr array of ClassA objects, displaying each item's index and counter value
* with buttons to increment the counter.
*
* Usage:
* - Click various buttons to perform operations on the shared list of ClassA objects.
* - Scroll through both Repeat lists to observe items and their corresponding index and counter values.
* - Perform dynamic operations like adding items to the beginning or end, deleting, swapping, and updating items in the shared array.
*/
let nextId : number = 0;
@ObservedV2 class ClassA {
id : number = nextId++;
@Trace counter : number;
constructor(c : number) {
this.counter = c;
}
}
@Entry
@ComponentV2
struct RepeatComp {
@Local arr: number[] = [1, 2, 2];
@Local totalCount:number = this.arr.length;
scroller: Scroller = new Scroller();
build() {
Column({space:10}) {
Text('TWO REPEAT LISTS WITH SHARED ARRAY')
.fontSize(20)
Row() {
Column({space:10}) {
Text("REPEAT 1")
Text('-------')
List({ space: 5, scroller:this.scroller }) {
Repeat(this.arr)
.virtualScroll({
totalCount: this.totalCount
})
.templateId((item:number, index:number) => {
return index % 2 ? 'font-32' : 'something-wrong';
})
.template("font-32", (i) => {
ListItem() {
Row({space:5}) {
Text(` ${i.index}.`) // binding to index
Text(`- Item: ${i.item}`) // binding to object property of array item
Button("item +1")
.onClick(() => {
//this.arr[i.index]++;
i.item++;
})
}
}.border({width:1})
.backgroundColor(0xFFA000)
}, { cachedCount: 5 })
.each((i) => {
ListItem() {
Row({space:5}) {
Text(` ${i.index}.`) // binding to index
Text(`- Item: ${i.item}`) // binding to object property of array item
Button("item +1")
.onClick(() =>
{
//this.arr[i.index]++;
i.item++;
})
}
}.border({width:1})
.backgroundColor(0x00FFA0)
})
}.height('100%')
}.width('50%')
.height('70%')
Column({space:10}) {
Text("REPEAT 2")
Text('-------')
List({ space: 5, scroller:this.scroller }) {
Repeat(this.arr)
.virtualScroll({
totalCount: this.totalCount
})
.templateId((item:number, index:number) => {
return index % 2 ? 'font-32' : 'something-wrong';
})
.template("font-32", (i) => {
ListItem() {
Row({space:5}) {
Text(` ${i.index}.`) // binding to index
Text(`- Item: ${i.item}`) // binding to object property of array item
Button("item +1")
.onClick(() => {
//this.arr[i.index]++;
i.item++;
})
}
}.border({width:1})
.backgroundColor(0xFFA000)
}, { cachedCount: 5 })
.each((i) => {
ListItem() {
Row({space:5}) {
Text(` ${i.index}.`) // binding to index
Text(`- Item: ${i.item}`) // binding to object property of array item
Button("item +1")
.onClick(() => {
//this.arr[i.index]++;
i.item++;
})
}
}.border({width:1})
.backgroundColor(0x00FFA0)
})
}.height('100%')
}.width('50%')
.height('70%')
}.height('75%')
Row({space:10}) {
Button("Add arr")
.onClick(() => {
this.arr.push(this.arr.length);
this.totalCount = this.arr.length;
})
Button("Shift arr")
.onClick(() => {
this.arr.shift();
this.totalCount = this.arr.length;
})
Button("Swap arr1 & arr2")
.onClick(() => {
if(this.arr[1] && this.arr[2]) {
let temp = this.arr[1];
this.arr[1] = this.arr[2];
this.arr[2] = temp;
}
})
}
Row({space:10}) {
Button("Add 10 items in BEGIN")
.onClick(() => {
for (let i = 0; i < 10; i++) {
this.arr.unshift(this.arr.length);
}
this.totalCount = this.arr.length;
})
Button("Add 10 items in END")
.onClick(() => {
for (let i = 0; i < 10; i++) {
this.arr.push(this.arr.length);
}
this.totalCount = this.arr.length;
})
}
}
}
}
马建仓 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

搜索帮助