HarmonyOS 如何能让swiper在满足某个条件的时候,无法单向滑动

如何能让swiper 在满足某个条件的时候,无法单向滑动。举个例子,有5个页面,当滑动到3个时候,触发了某个条件,此时可以继续向4滑动,但是无法向2滑动

HarmonyOS
2天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
put_get

参考示例:

// 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 })
  }
}
分享
微博
QQ
微信
回复
2天前
相关问题
HarmonyOS Swiper滑动相关
39浏览 • 1回复 待解决
Swiper组件设置不跟随手势滑动
655浏览 • 1回复 待解决