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

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

HarmonyOS
2024-12-24 17:16:49
浏览
收藏 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 })
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
分享
微博
QQ
微信
回复
2024-12-24 20:04:59


相关问题
HarmonyOS Swiper滑动相关
684浏览 • 1回复 待解决
HarmonyOS @State和@Prop单向传输场景
362浏览 • 1回复 待解决
HarmonyOS Swiper滑动性能优化
393浏览 • 1回复 待解决
HarmonyOS 如何禁止Swipe向某个index滑动
266浏览 • 1回复 待解决