文档开发者中心
我的
|

HarmonyOS 5

List如何实现上拉取下一页数据

列表数据分页获取,我们可以通过Refresh组件实现List下拉刷新列表,取第一页数据,滚动列表到底部,然后上拉到一定距离之后,需要取下一页数据,如何实现
点赞
收藏
回复
3
分享
举报
浏览249 发布于2024-10-18 02:15广东
全部评论
最多点赞
最新发布
最早发布

参考二楼回复

4楼回复于2024-10-18 08:52 来自广东
有一个专门的触发事件,貌似叫onReachEnd
3楼回复于2024-10-18 02:25 未知归属地
收起
自动换行
深色代码主题
复制
@Entry
@Component
struct RefreshExample {
  @State isRefreshing: boolean = false
  @State arr: String[] = ['0', '1', '2', '3', '4','5','6']
  @State arr2: String[] = ['0', '1', '2', '3', '4','5','6','7','8','9','10']
  @State isSearch:boolean = false
  build() {
    Column() {
      Refresh({ refreshing: $$this.isRefreshing}) {
        Column(){
          List() {
            ForEach(this.arr, (item: string) => {
              ListItem() {
                Text('' + item)
                  .width('100%').height(100).fontSize(16)
                  .textAlign(TextAlign.Center).borderRadius(10).backgroundColor(0xFFFFFF)
                  .margin({top:10,bottom:10})
              }
            })
          }
          .onReachEnd(() => {
            console.info("触发加载")
          })
          .width('100%')
          .height('48%')
          .layoutWeight(1)
          List() {
            ForEach(this.arr2, (item: string) => {
              ListItem() {
                Text('' + item)
                  .width('100%').height(100).fontSize(16)
                  .textAlign(TextAlign.Center).borderRadius(10).backgroundColor(0xFFFFFF)
                  .margin({top:10,bottom:10})
              }
            })
            if (this.isSearch){
              ListItem(){
                Flex({alignItems:ItemAlign.Center,justifyContent:FlexAlign.Center}){
                  Text('正在搜索...')
                }.width('100%').margin(10)
              }
            }
          }
          .onReachEnd(() => {
            console.info("触发加载:",this.isSearch)
            if(!this.isSearch){
              this.isSearch = true
              setTimeout(()=>{
                this.isSearch = false
              },2000)
            }
          })
          .border({width:1})
          .width('100%')
          .height('50%')
        }
        .height("100%")
        .width('100%')
      }
      .onStateChange((refreshStatus: RefreshStatus) => {
        console.info('状态变化' + refreshStatus)
      })
      .onRefreshing(() => {
        setTimeout(() => {
          this.isRefreshing = false
        }, 2000)
        console.log('进入刷新')
      })
      .backgroundColor(0x89CFF0)
    }
  }
}

分页加载可参考onReachEnd事件,链接如下:

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-service-widget-container-list-V5

无痕加载请参考lazyforeach方法,链接如下:

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-rendering-control-lazyforeach-V5

下拉刷新可参考refresh组件,链接如下:

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-components-container-refresh-V5

如果要使用三方库,参考如下链接:

https://gitee.com/openharmony-sig/PullToRefresh

展开全部
2楼回复于2024-10-18 02:17 来自广东
写回答
新增插入模板功能
一键使用模板,快速填写内容,轻松发帖~
知道了
  • 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
  • 如您发布的内容为转载内容,请注明内容来源。

我要提问题

了解社区公约,与您携手共创和谐专业的开发者社区。