如何使用Swiper组件实现下拉刷新

Swiper下拉刷新方案

HarmonyOS
2024-06-11 23:23:47
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
rhlee

利用parallelGesture和PanGesture绑定拖动触发事件,实现下拉刷新效果。

代码示例:

@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) 
  } 
}
分享
微博
QQ
微信
回复
2024-06-12 23:40:48
相关问题
Refresh结合lottie实现下拉刷新动画
451浏览 • 1回复 待解决
使用swiper组件实现viewPager效果
456浏览 • 1回复 待解决
有谁知道web组件如何下拉刷新
532浏览 • 1回复 待解决
Swiper 组件嵌套图片刷新数据会闪烁
292浏览 • 1回复 待解决
如何实现下载断点续传
501浏览 • 0回复 待解决
Swiper组件如何设置导航点位置
518浏览 • 1回复 待解决
如何获取组件刷新时间
600浏览 • 1回复 待解决
Swiper是否支持组件复用
355浏览 • 1回复 待解决
下拉刷新和上拉加载的API为9的sdk
1343浏览 • 1回复 待解决