HarmonyOS Refresh支持swiper吗?

使用swiper做了一个上下滑动页面,需要有下拉刷新功能,貌似refrsh不支持swiper

阅读 553
1 个回答

参考demo:

@Entry
@Component
struct SwiperItemLeak {
  private swiperController: SwiperController = new SwiperController()
  private curSwiperIndex = 0;
  private list: number[] = []
  @State isStopSwiperSlide: boolean = false;
  @State positionY: number = 0;
  private isRefresh: boolean = false;

  aboutToAppear(): void {
    for (let i = 1; i <= 10; i++) {
      this.list.push(i);
    }
  }

  build() {
    Stack({ alignContent: Alignment.TopStart }) {
      Text(this.positionY.toString())
        .width('100%')
        .height('10%')
        .textAlign(TextAlign.Center)
        .fontSize(30)
      Swiper(this.swiperController) {
        ForEach(this.list, (item: number) => {
          Text(item.toString())
            .width('100%')
            .height('100%')
            .backgroundColor(0xAFEEEE * ((item + 1) / 0x0f))
            .textAlign(TextAlign.Center)
            .fontSize(30)
        })
      }
      .vertical(true)
      .width("100%")
      .height("100%")
      .cachedCount(3)
      .index(0)
      .autoPlay(false)
      .indicator(false)
      .effectMode(EdgeEffect.None)
      .loop(false)
      .duration(100)
      .disableSwipe(this.isStopSwiperSlide)
      .displayCount(1)
      .itemSpace(0)
      .curve(Curve.Linear)
      .backgroundColor(Color.Red)
      .position({ y: this.positionY })
      .onChange((index: number) => {
        console.info(index.toString())
        this.curSwiperIndex = index;
      })
      .parallelGesture(
        PanGesture()
          .onActionStart((event: GestureEvent) => {
          })
          .onActionUpdate((event: GestureEvent) => {
            if (event && this.curSwiperIndex == 0) {
              if (event.offsetY > 0) {
                this.positionY = event.offsetY;
                this.isStopSwiperSlide = true;
                this.isRefresh = true;
              }
              else {
                this.positionY = 0;
                this.isStopSwiperSlide = false;
                this.isRefresh = false;
              }
            }
          })
          .onActionEnd(() => {
            if (this.isRefresh) {
              setTimeout(() => {
                this.isStopSwiperSlide = false;
                this.positionY = 0;
                this.isRefresh = false;
              }, 1000);
            }
          })
      )
    }.width('100%').height("100%").backgroundColor(Color.Pink)
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进