代码拉取完成,页面将自动刷新
// Not used by Repeat
class MyDataSource implements IDataSource {
private list: number[] = [];
private listener: DataChangeListener | undefined;
constructor(list: number[]) {
this.list = list
}
totalCount(): number {
return this.list.length
}
getData(index: number): number {
return this.list[index]
}
registerDataChangeListener(listener: DataChangeListener): void {
this.listener = listener
}
unregisterDataChangeListener() {
}
}
@Entry
@ComponentV2
struct Index {
@Local arr: Array<string> = Array(5).fill('').map((v,i) => `${i}`);
@Local totalCount:number = this.arr.length;
private data: MyDataSource = new MyDataSource([]);
private swiperController: SwiperController = new SwiperController()
aboutToAppear(): void {
let list = []
for (let i = 1; i <= 10; i++) {
list.push(i.toString() as never);
}
this.data = new MyDataSource(list)
}
shift() {
this.arr.shift();
this.totalCount = this.arr.length;
}
unshift() {
// TBD need better logic
let min = Math.min(...this.arr.map(v => (+v || 0)));
if (min === Infinity) min = 0;
this.arr.unshift(`${min - 1}`);
this.totalCount = this.arr.length;
}
update(index: number) {
// TBD need better logic
this.arr[index] = `${-this.arr[index] || 0}`;
this.totalCount = this.arr.length;
}
build() {
Column({ space: 5 }) {
Text('4_swiper_hq.ets')
Row({space: 3}) {
Button('shift').onClick(() => this.shift())
Button('unshift').onClick(() => this.unshift())
Button('update #0').onClick(() => this.update(0))
}.padding(10)
Swiper(this.swiperController) {
Repeat(this.arr)
// @ts-ignore
.virtualScroll({
totalCount: this.totalCount
})
.each((ri) => {
Text(`${ri.item}`).width('90%').height(160).backgroundColor(0xAFEEEE).textAlign(TextAlign.Center).fontSize(30)
})
.key((item:string) => item)
/* LazyForEach analog
LazyForEach(this.data, (item: string) => {
Text(item).width('90%').height(160).backgroundColor(0xAFEEEE).textAlign(TextAlign.Center).fontSize(30)
}, (item:string) => item)
*/
}
.onChange((index) => {
console.log('onChange', index);
})
.cachedCount(2)
.index(1)
.autoPlay(true)
.interval(4000)
.indicator(true)
.loop(true)
.duration(100)
.itemSpace(0)
.curve(Curve.Linear)
.onChange((index: number) => {
console.info(index.toString())
})
Row({ space: 12 }) {
Button('showNext')
.onClick(() => {
this.swiperController.showNext()
})
Button('showPrevious')
.onClick(() => {
this.swiperController.showPrevious()
})
}.margin(5)
}.width('100%')
.margin({ top: 5 })
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。