HarmonyOS 页面往上滑动自动加载新数据的方法?

HarmonyOS 页面往上滑动自动加载新数据的方法?

HarmonyOS
2024-11-26 09:23:19
304浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
shlp

​下拉刷新demo实例:​https://developer.huawei.com/consumer/cn/forum/topic/0204111882771519339?fid=0102683795438680754

上滑动到顶端后,上拉刷新数据的demo实现如下:​

@Entry 
@Component 
struct WeiboPersonPage { 
  @State currentTitle: string = '有态度' 
  @State data: Array<string> = []; 
  @State data2: Array<string> = []; 
  mainScroller: Scroller = new Scroller(); 
  tags: Array<string> = ['动态0', '照片1', '视频2', '相册3']; 
  tagScrollers: Array<Scroller> = []; //每个scroller对应一个标签 
  @State hideNavBar: boolean = true; 
  @State hideLoadingView: boolean = true; 
  @State mainListCanScroll: boolean = true; 
  @State private currentPageHeight: number = 0; 
  @State private currentPageWidth: number = 0; 
  @State private progress: number = 0; //0-10 
  @State isRefreshing: boolean = false; 
  private panOption: PanGestureOptions = new PanGestureOptions({ direction: PanDirection.Up | PanDirection.Down }) 
  @State isLoadingFromBottom: boolean = false; 
  aboutToAppear() { 
    for (let i = 0; i < this.tags.length; i++) { 
      let tagScroller = new Scroller(); 
      this.tagScrollers.push(tagScroller); 
    } 
    for (let i = 0; i < 2; i++) { 
      this.data.push(`自定义header内容`) 
    } 
    for (let i = 0; i < 50; i++) { 
      this.data2.push(`Hello ${i}`) 
    } 
  } 
  build() { 
    Column() { 
      Navigation() { 
        Stack({ alignContent: Alignment.TopStart }) { 
          this.mainListView(this.mainScroller, this.tagScrollers) 
          Progress({ value: this.progress, total: 10, type: ProgressType.Ring }) 
            .width(20) 
            .color(Color.Orange) 
            .style({ 
              strokeWidth: 20, 
              status: this.isRefreshing == true ? ProgressStatus.LOADING : ProgressStatus.PROGRESSING, 
              enableSmoothEffect: true, 
              enableScanEffect: true 
            }) 
            .backgroundColor(Color.Yellow) 
            .visibility((this.hideLoadingView == false || this.isRefreshing) ? Visibility.Visible : Visibility.Hidden) 
            .margin({ left: this.currentPageWidth - 23 - 20 }) 
          //1.下拉超过80,则进入刷新状态 
          Button({ type: ButtonType.Normal, stateEffect: true }) { 
            Image($r('app.media.back2')).width(40).height(40) 
          } 
          .margin({ left: 23 }) 
          .backgroundColor(Color.Blue) 
          .onClick(() => { 
          }) 
          .visibility(this.hideNavBar == false ? Visibility.Hidden : Visibility.Visible) 
        } 
      } 
      .backgroundColor(Color.White) 
      .title(this.NavigationTitle) 
      .margin({ 
        top: $r('app.float.navigation_margin_top') 
      }) 
      .width('100%') 
      .titleMode(NavigationTitleMode.Mini) 
      .hideBackButton(false) 
      .zIndex(1) 
      .mode(NavigationMode.Auto) 
      .hideTitleBar(this.hideNavBar) 
      .parallelGesture( 
        PanGesture(this.panOption) 
          .onActionStart((event?: GestureEvent) => { 
          }) 
          .onActionUpdate((event?: GestureEvent) => { 
          }) 
          .onActionEnd(() => { 
            if (this.progress >= 10) { 
              //并且手指离开,执行刷新 
              this.isRefreshing = true; 
              //正在刷新的时候 
              //需要一直显示 
              console.info('开始刷新========1') 
              //在这里进行回调函数,执行若干秒 
              //举例,执行若干秒 
              setTimeout(() => { 
                this.isRefreshing = false; 
                this.data2.unshift('1111'); 
              }, 2000); 
            } 
            else { 
              this.isRefreshing = false; 
              console.info('取消刷新========1') 
            } 
          }) 
      ) 
    } 
    .width('100%') 
    .backgroundColor($r('app.color.common_background')) 
    .height('100%') 
    .onAreaChange((oldValue: Area, newValue: Area) => { 
      console.info('onAreaChange:' + newValue.height.toString()); 
      this.currentPageHeight = Math.round(newValue.height as number); 
      this.currentPageWidth = Math.round(newValue.width as number); 
    }) 
  } 
  //自定义导航 
  @Builder 
  NavigationTitle() { 
    Column() { 
      Text(this.currentTitle) 
        .fontColor('#182431') 
        .fontSize(18) 
        .fontWeight(600) 
        .backgroundColor(Color.Yellow) 
        .height(56) 
    } 
    .width('67%') 
    .height(56) 
    .backgroundColor(Color.Grey) 
  } 
  @Builder 
  listView(scroller: Scroller) { 
    List({ space: 0, scroller: scroller }) { 
      ForEach(this.data2, (item: string, index?: number) => { 
        ListItem() { 
          if (this.isLoadingFromBottom) { 
            if (index == this.data2.length - 1) { 
              Row() { 
                Text('加载中...').fontSize(50) 
                Progress({ value: 0, total: 20, type: ProgressType.Ring }) 
                  .width(20) 
                  .color(Color.Orange) 
                  .style({ strokeWidth: 20, status: ProgressStatus.LOADING, 
                    enableSmoothEffect: true, enableScanEffect: true }) 
                  .backgroundColor(Color.Red) 
              }.margin({ left: 10, right: 10 }) 
            } 
            else { 
              Row() { 
                Text(item).fontSize(50).width('100%') 
              }.margin({ left: 10, right: 10 }) 
            } 
          } 
          else { 
            Row() { 
              Text(item).fontSize(50).width('100%') 
            }.margin({ left: 10, right: 10 }) 
          } 
        } 
        .backgroundColor(Color.White) 
      }, (item: string) => item) 
    } 
    .cachedCount(5) 
    .divider({ strokeWidth: 1, color: 0x222222 }) 
    // 必须设置列表为滑动到边缘无效果,必须的!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 
    .edgeEffect(EdgeEffect.None) 
    .enableScrollInteraction(this.mainListCanScroll ? false : true) 
    .onScroll((scrollOffset: number, scrollState: ScrollState) => { 
      //如果下侧列表划到了最上侧,则主list可以向下滑动,不可以向上滑动; 
      //如果下侧列表划到了最上侧,非主list可以向上滑动,不可以向下滑动 
      if (scroller.currentOffset().yOffset == 0) { 
        this.mainListCanScroll = true 
      } 
      else { 
        this.mainListCanScroll = false 
      } 
    }) 
    .onReachEnd(() => { 
      //每次只允许执行一次,执行完后在执行下一个 
      if (this.isLoadingFromBottom == true) { 
        return; 
      } 
      //当划到末尾的时候,加一行数据,用来显示加载效果 
      this.isLoadingFromBottom = true; 
      this.data2.push('占位数据'); 
      //请求操作 
      //举例是一个延时操作 
      setTimeout(() => { 
        console.info('加载结束=====1') 
        this.isLoadingFromBottom = false; 
        this.data2.pop(); 
        this.data2.push('新的数据' + this.data2.length); 
        this.data2.push('新的数据' + this.data2.length); 
        this.data2.push('新的数据' + this.data2.length); 
        this.data2.push('新的数据' + this.data2.length); 
        this.data2.push('新的数据' + this.data2.length); 
        this.data2.push('新的数据' + this.data2.length); 
        this.data2.push('新的数据' + this.data2.length); 
        this.data2.push('新的数据' + this.data2.length); 
        this.data2.push('新的数据' + this.data2.length); 
        this.data2.push('新的数据' + this.data2.length); 
        this.data2.push('新的数据' + this.data2.length); 
        this.data2.push('新的数据' + this.data2.length); 
        this.data2.push('新的数据' + this.data2.length); 
        this.data2.push('新的数据' + this.data2.length); 
        this.data2.push('新的数据' + this.data2.length); 
        this.data2.push('新的数据' + this.data2.length); 
        this.data2.push('新的数据' + this.data2.length); 
        this.data2.push('新的数据' + this.data2.length); 
        this.data2.push('新的数据' + this.data2.length); 
      }, 3000); 
    }) 
  } 
  @Builder 
  mainListView(mainScroller: Scroller, tagScrollers: Array<Scroller>) { 
    List({ space: 0, scroller: mainScroller }) { 
      ForEach(this.data, (item: string, index?: number) => { 
        if (index == 0) { 
          ListItem() { 
            Row() { 
              Text(item).fontSize(50).width('100%') 
            }.margin({ left: 10, right: 10 }) 
          } 
          .height(400) // 
          .backgroundColor(Color.Red) 
        } 
        else { 
          ListItem() { 
            Tabs({ barPosition: BarPosition.Start }) { 
              TabContent() { 
                this.listView(tagScrollers[0]) 
              } 
              .tabBar('首页') 
              TabContent() { 
                this.listView(tagScrollers[1]) 
              } 
              .tabBar('推荐') 
              TabContent() { 
                this.listView(tagScrollers[2]) 
              } 
              .tabBar('发现') 
              TabContent() { 
                this.listView(tagScrollers[3]) 
              } 
              .tabBar("我的") 
            } 
          } 
          .height(this.currentPageHeight - 56 - 40) //这里要设置一个全屏减去导航栏56减去标签栏40的高度 
          .backgroundColor(Color.Green) 
        } 
      }, (item: string) => item) 
    } 
    .cachedCount(2) 
    .backgroundColor(Color.White) 
    .divider({ 
      strokeWidth: 1, 
      startMargin: '3.5%', 
      endMargin: '2%', 
      color: '#f2f2f2' 
    }) 
    .edgeEffect(EdgeEffect.None) // 必须设置列表为滑动到边缘无效果 
    .divider({ strokeWidth: 1, color: 0x222222 }) 
    // 必须设置列表为滑动到边缘无效果 
    .edgeEffect(EdgeEffect.Spring) 
    .scrollBar(BarState.Off) 
    .enableScrollInteraction(this.mainListCanScroll) 
    .onScroll((scrollOffset: number, scrollState: ScrollState) => { 
      console.info('offset1:' + this.mainScroller.currentOffset().yOffset); 
      let currentOffsetY = Math.round(this.mainScroller.currentOffset().yOffset); 
      //导航栏 
      if (currentOffsetY > 100) { 
        this.hideNavBar = false 
      } 
      else { 
        this.hideNavBar = true 
      } 
      //主list和分list滑动控制 
      if (currentOffsetY == 0) { 
        this.mainListCanScroll = true 
      } 
      else { 
        if (currentOffsetY >= 400) { 
          this.mainListCanScroll = false 
        } 
        else { 
          this.mainListCanScroll = true 
        } 
      } 
      //需要控制下拉和上拉后的刷新 
      //下拉超过80,松手后检测,如果松手后的偏移量超过80,则刷新 
      //如果松手后的偏移量不超过,则不执行刷新操作 
      if (currentOffsetY >= 0) { 
        this.hideLoadingView = true 
      } 
      else { 
        this.hideLoadingView = false 
      } 
      this.progress = currentOffsetY / 80 * (-10); 
    }) 
    .onReachStart(() => { 
      console.info('到最上面了========1') 
    }) 
    .onReachEnd(() => { 
      console.info('到最底部了========1') 
    }) 
  } 
}
  • 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.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.
  • 181.
  • 182.
  • 183.
  • 184.
  • 185.
  • 186.
  • 187.
  • 188.
  • 189.
  • 190.
  • 191.
  • 192.
  • 193.
  • 194.
  • 195.
  • 196.
  • 197.
  • 198.
  • 199.
  • 200.
  • 201.
  • 202.
  • 203.
  • 204.
  • 205.
  • 206.
  • 207.
  • 208.
  • 209.
  • 210.
  • 211.
  • 212.
  • 213.
  • 214.
  • 215.
  • 216.
  • 217.
  • 218.
  • 219.
  • 220.
  • 221.
  • 222.
  • 223.
  • 224.
  • 225.
  • 226.
  • 227.
  • 228.
  • 229.
  • 230.
  • 231.
  • 232.
  • 233.
  • 234.
  • 235.
  • 236.
  • 237.
  • 238.
  • 239.
  • 240.
  • 241.
  • 242.
  • 243.
  • 244.
  • 245.
  • 246.
  • 247.
  • 248.
  • 249.
  • 250.
  • 251.
  • 252.
  • 253.
  • 254.
  • 255.
  • 256.
  • 257.
  • 258.
  • 259.
  • 260.
  • 261.
  • 262.
  • 263.
  • 264.
  • 265.
  • 266.
  • 267.
  • 268.
  • 269.
  • 270.
  • 271.
  • 272.
  • 273.
  • 274.
  • 275.
  • 276.
  • 277.
  • 278.
  • 279.
  • 280.
  • 281.
  • 282.
  • 283.
  • 284.
  • 285.
  • 286.
  • 287.
  • 288.
  • 289.
  • 290.
  • 291.
  • 292.
  • 293.
  • 294.
  • 295.
  • 296.
  • 297.
分享
微博
QQ
微信
回复
2024-11-26 15:05:13


相关问题
如何实现页面加载前从接口获取数据
3026浏览 • 1回复 待解决
HarmonyOS 页面滑动卡顿
546浏览 • 1回复 待解决