如何使用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) 
  } 
}
  • 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.
分享
微博
QQ
微信
回复
2024-06-12 23:40:48


相关问题
HarmonyOS Webview如何实现下拉刷新效果
431浏览 • 1回复 待解决
HarmonyOS ArkWeb如何实现下拉刷新功能
1103浏览 • 1回复 待解决
Refresh结合lottie实现下拉刷新动画
1589浏览 • 1回复 待解决
HarmonyOS 怎么实现下拉分类列表?
634浏览 • 1回复 待解决
HarmonyOS 下拉刷新如何实现
236浏览 • 1回复 待解决
上拉加载,下拉刷新组件
863浏览 • 1回复 待解决
使用swiper组件实现viewPager效果
1998浏览 • 1回复 待解决
HarmonyOS 组件下拉刷新问题
973浏览 • 1回复 待解决
HarmonyOS使用Refresh下拉刷新问题
1292浏览 • 1回复 待解决
有谁知道web组件如何下拉刷新
2166浏览 • 1回复 待解决
HarmonyOS rn组件FlatList下拉刷新异常
552浏览 • 1回复 待解决