参考示例:// xxx.ets class MyDataSource implements IDataSource { private list: number[] = [] constructor(list: number[]) { this.list = list } totalCount(): number { return this.list.length } getData(index: number): number { return this.list[index] } registerDataChangeListener(listener: DataChangeListener): void { } unregisterDataChangeListener() { } } @Entry @Component struct SwiperExample { private swiperController: SwiperController = new SwiperController() private data: MyDataSource = new MyDataSource([]) private panOptionLeft: PanGestureOptions = new PanGestureOptions({ direction: PanDirection.Left }) private panOptionRight: PanGestureOptions = new PanGestureOptions({ direction: PanDirection.Right }) @State directionValue:string = 'right' @State isShow:boolean = false aboutToAppear(): void { let list: number[] = [] for (let i = 1; i <= 10; i++) { list.push(i); } this.data = new MyDataSource(list) } build() { Column({ space: 5 }) { Swiper(this.swiperController) { LazyForEach(this.data, (item: string) => { Text(item.toString()) .width('90%') .height(160) .backgroundColor(0xAFEEEE) .textAlign(TextAlign.Center) .fontSize(30) .parallelGesture( PanGesture(this.directionValue ==='left'?this.panOptionLeft:this.panOptionRight) ) }, (item: string) => item) } .cachedCount(2) .index(1) .autoPlay(false) .interval(4000) .indicator(Indicator.digit() .top(200) .fontColor(Color.Gray) .selectedFontColor(Color.Gray) .digitFont({ size: 20, weight: FontWeight.Bold }) .selectedDigitFont({ size: 20, weight: FontWeight.Normal })) .loop(true) .duration(1000) .itemSpace(0) .displayArrow(true, false) Row({ space: 12 }) { Button('只能上一个') .onClick(() => { this.directionValue = 'left' }) Button('只能下一个') .onClick(() => { this.directionValue = 'right' }) }.margin(5) } .width('100%') .margin({ top: 5 }) } }
参考示例: