加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
2_swiper_hq.ets 2.87 KB
一键复制 编辑 原始数据 按行查看 历史
// 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 })
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化