HarmonyOS swiper修改指示器问题

HarmonyOS
2024-12-18 15:55:42
924浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zbw_apple

当前swiper自有的属性不支持设置指示器的间距,如果要设置间距,需要自定义UI来实现指示器,示例代码如下:

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([])
  @State widthLength: number = 0
  @State heightLength: number = 0
  @State currentIndex: number = 0
  // 实现导航点自定义动画

  private swiperWidth: number = 0

  private getTextInfo(index: number): Record<string, number> {
    let strJson = getInspectorByKey(index.toString())
    try {
      let obj: Record<string, string> = JSON.parse(strJson)
      let rectInfo: number[][] = JSON.parse('[' + obj.$rect + ']')
      return { 'left': px2vp(rectInfo[0][0]), 'width': px2vp(rectInfo[1][0] - rectInfo[0][0]) }
    } catch (error) {
      return { 'left': 0, 'width': 0 }
    }
  }

  private getCurrentIndicatorInfo(index: number, event: TabsAnimationEvent): Record<string, number> {
    let nextIndex = index
    if (index > 0 && event.currentOffset > 0) {
      nextIndex--
    } else if (index < 3 && event.currentOffset < 0) {
      nextIndex++
    }
    let indexInfo = this.getTextInfo(index)
    let nextIndexInfo = this.getTextInfo(nextIndex)
    let swipeRatio = Math.abs(event.currentOffset / this.swiperWidth)
    let currentIndex = swipeRatio > 0.5 ? nextIndex : index // 页面滑动超过一半,swiper切换到下一页。
    let currentLeft = indexInfo.left + (nextIndexInfo.left - indexInfo.left) * swipeRatio
    let currentWidth = indexInfo.width + (nextIndexInfo.width - indexInfo.width) * swipeRatio
    return { 'index': currentIndex, 'left': currentLeft, 'width': currentWidth }
  }

  aboutToAppear(): void {
    let list: number[] = []
    for (let i = 1; i <= 6; i++) {
      list.push(i);
    }
    this.data = new MyDataSource(list)
  }

  build() {
    Column({ space: 5 }) {
      Stack({ alignContent: Alignment.Bottom }) {
        Swiper(this.swiperController) {
          // LazyForEach(this.data, (item: string) => {
          // Text(item.toString())
          // .width('90%')
          // .height(160)
          // .backgroundColor(0xAFEEEE)
          // .textAlign(TextAlign.Center)
          // .fontSize(30)
          // }, (item: string) => item)
          Image($r('app.media.background'))
            .backgroundColor(Color.Red)
          Image($r('app.media.startIcon'))
            .backgroundColor(Color.Red)
          Image($r('app.media.background'))
            .backgroundColor(Color.Red)
          Image($r('app.media.startIcon'))
            .backgroundColor(Color.Red)
          Image($r('app.media.background'))
            .backgroundColor(Color.Red)
          Image($r('app.media.startIcon'))
            .backgroundColor(Color.Red)
        }
        .width('100%')
        .height('100%')
        .cachedCount(2)
        .index(0)
        .autoPlay(false)
        .interval(4000)
        .loop(true)
        .duration(1000)
        .itemSpace(0)
        .indicator(false)
        .curve(Curve.Linear)
        .onChange((index: number) => {
          console.info(index.toString())
          this.currentIndex = index
        })
        .onGestureSwipe((index: number, extraInfo: SwiperAnimationEvent) => {
          console.info("index: " + index)
          console.info("current offset: " + extraInfo.currentOffset)


          // 在页面跟手滑动过程中,逐帧触发该回调。
          let currentIndicatorInfo = this.getCurrentIndicatorInfo(index, extraInfo)
          this.currentIndex = currentIndicatorInfo.index
        })
        .onAnimationStart((index: number, targetIndex: number, extraInfo: SwiperAnimationEvent) => {
          console.info("index: " + index)
          console.info("targetIndex: " + targetIndex)
          console.info("current offset: " + extraInfo.currentOffset)
          console.info("target offset: " + extraInfo.targetOffset)
          console.info("velocity: " + extraInfo.velocity)
          // 切换动画开始时触发该回调。下划线跟着页面一起滑动,同时宽度渐变。
          this.currentIndex = targetIndex

        })
        .onAnimationEnd((index: number, extraInfo: SwiperAnimationEvent) => {
          console.info("index: " + index)
          console.info("current offset: " + extraInfo.currentOffset)
        })


        Row() {
          LazyForEach(this.data, (item: string, index: number) => {
            Column()
              .width(this.currentIndex === index ? 15 : 15)
              .height(5)// 实现设置指示器的间距
              .margin(0)
              .borderRadius(5)
              .backgroundColor(this.currentIndex === index ? Color.Red : Color.Gray)
          }, (item: string) => item)
        }
        .margin({ bottom: 4 })
      }
    }.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.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
分享
微博
QQ
微信
回复
2024-12-18 19:13:08


相关问题
HarmonyOS 关于Swiper指示器问题
1152浏览 • 1回复 待解决
HarmonyOS Swiper指示器
698浏览 • 0回复 待解决
HarmonyOS Swiper指示器
637浏览 • 1回复 待解决
HarmonyOS Swiper指示器显示错误
768浏览 • 1回复 待解决
如何设置swiper指示器不显示
1551浏览 • 1回复 待解决
HarmonyOS如何自定义Swiper指示器样式?
712浏览 • 0回复 待解决
HarmonyOS Swiper支持动态修改数据吗
1242浏览 • 1回复 待解决
HarmonyOS Swiper循环问题
1035浏览 • 1回复 待解决
HarmonyOS swiper + LazyForEach使用问题
1189浏览 • 1回复 待解决
HarmonyOS Swiper的disableSwipe问题
530浏览 • 1回复 待解决