HarmonyOS NEXT星河版之自定义List下拉革新与加载更多

打印 上一主题 下一主题

主题 512|帖子 512|积分 1536

一、加载更多

借助List的onReachEnd方法,实现加载更多功能,结果如下:


  1. @Component
  2. export struct HPList {
  3.   // 数据源
  4.   @Prop dataSource: object[] = []
  5.   // 加载更多是否ing
  6.   @State isLoadingMore: boolean = false
  7.   // 是否还有更多
  8.   @Prop hasMore: boolean = false
  9.   // 渲染Item
  10.   @BuilderParam renderItem: (item: object) => void
  11.   // 加载更多回调
  12.   onLoadMore: () => void = () => {
  13.   }
  14.   // 加载更多文案
  15.   loadingMoreText: string = '加载中...'
  16.   // 没有更多文案
  17.   noMoreDataText: string = '没有更多啦'
  18.   @Builder
  19.   getLoadMoreView() {
  20.     Row() {
  21.       if (!this.hasMore) {
  22.         Text(this.noMoreDataText)
  23.           .fontSize(14)
  24.           .fontColor($r('app.color.text_secondary'))
  25.       } else {
  26.         Row({ space: 8 }) {
  27.           LoadingProgress()
  28.             .width(30)
  29.             .height(30)
  30.             .color(Color.Orange)
  31.           Text(this.loadingMoreText)
  32.             .fontSize(14)
  33.             .fontColor($r('app.color.text_secondary'))
  34.         }
  35.       }
  36.     }
  37.     .height(60)
  38.     .width('100%')
  39.     .justifyContent(FlexAlign.Center)
  40.   }
  41.   build() {
  42.     List() {
  43.       ForEach(this.dataSource, (item: object) => {
  44.         ListItem() {
  45.           if (this.renderItem) {
  46.             this.renderItem(item)
  47.           }
  48.         }
  49.       })
  50.       // 加载更多view
  51.       ListItem() {
  52.         this.getLoadMoreView()
  53.       }
  54.     }
  55.     .onReachEnd(async () => {
  56.       if (!this.isLoadingMore && this.hasMore) {
  57.         this.isLoadingMore = true
  58.         await this.onLoadMore()
  59.         this.isLoadingMore = false
  60.       }
  61.     })
  62.   }
  63. }
复制代码
使用:
  1. import { HPList } from '@hp/basic'
  2. import { promptAction } from '@kit.ArkUI'
  3. @Preview
  4. @Component
  5. export struct Task {
  6.   // 模拟数据
  7.   @State dataList: Person[] = [{
  8.     name: '1'
  9.   }, {
  10.     name: '2'
  11.   }, {
  12.     name: '3'
  13.   }, {
  14.     name: '4'
  15.   }, {
  16.     name: '5'
  17.   }, {
  18.     name: '6'
  19.   }, {
  20.     name: '7'
  21.   }, {
  22.     name: '8'
  23.   }, {
  24.     name: '9'
  25.   }, {
  26.     name: '10'
  27.   }, {
  28.     name: '11'
  29.   }, {
  30.     name: '12'
  31.   }]
  32.   @State hasMore: boolean = true
  33.   isFirstIn: boolean = true
  34.   @Builder
  35.   renderItem(item: object) {
  36.     Row() {
  37.       Text(JSON.stringify(item))
  38.         .fontSize(16)
  39.         .textAlign(TextAlign.Center)
  40.     }
  41.     .width('100%')
  42.     .height(80)
  43.     .borderRadius(15)
  44.     .backgroundColor(Color.Pink)
  45.     .justifyContent(FlexAlign.Center)
  46.     .margin({ bottom: 10 })
  47.   }
  48.   /**
  49.    * 加载更多
  50.    */
  51.   async loadMore() {
  52.     // 首次渲染数据为空时,也会调loadMore
  53.     if (this.isFirstIn) {
  54.       this.isFirstIn = false
  55.       return
  56.     }
  57.     await new Promise<void>((resolve, reject) => {
  58.       setTimeout(() => {
  59.         // 模拟数据
  60.         this.dataList = this.dataList.concat(this.dataList.slice(5))
  61.         if (this.dataList.length > 30) {
  62.           this.hasMore = false
  63.         }
  64.         resolve()
  65.       }, 3000)
  66.     });
  67.   }
  68.   build() {
  69.     HPList({
  70.       dataSource: this.dataList,
  71.       hasMore: this.hasMore,
  72.       renderItem: (item: object) => {
  73.         this.renderItem(item)
  74.       },
  75.       onLoadMore: () => {
  76.         this.loadMore()
  77.       }
  78.     })
  79.   }
  80. }
  81. class Person {
  82.   name: string = ''
  83. }
复制代码
二、下拉革新

通过Refresh实现下拉革新,并自定义头部,结果如下:



代码如下:
  1. import { promptAction } from '@kit.ArkUI'
  2. @Component
  3. export struct HPList {
  4.   // 数据源
  5.   @Prop dataSource: object[] = []
  6.   // 加载更多是否ing
  7.   @State isLoadingMore: boolean = false
  8.   // 是否还有更多
  9.   @Prop hasMore: boolean = false
  10.   // 渲染Item
  11.   @BuilderParam renderItem: (item: object) => void
  12.   // 加载更多回调
  13.   onLoadMore: () => void = () => {
  14.   }
  15.   // 加载更多文案
  16.   loadingMoreText: string = '加载中...'
  17.   // 没有更多文案
  18.   noMoreDataText: string = '没有更多啦'
  19.   // 是否下拉刷新ing
  20.   @State isRefreshing: boolean = false
  21.   // 下拉刷新回调
  22.   onRefresh: () => void = () => {
  23.   }
  24.   @State refreshState: RefreshStatus = RefreshStatus.Inactive
  25.   // 获取下拉文本
  26.   getStatusText() {
  27.     switch (this.refreshState) {
  28.       case RefreshStatus.Inactive:
  29.         return ""
  30.       case RefreshStatus.Drag:
  31.         return "继续下拉"
  32.       case RefreshStatus.OverDrag:
  33.         return "松开刷新"
  34.       case RefreshStatus.Refresh:
  35.         return "加载中"
  36.     }
  37.     return ""
  38.   }
  39.   @Builder
  40.   getRefreshView() {
  41.     Row({ space: 10 }) {
  42.       LoadingProgress()
  43.         .color($r('app.color.primary'))
  44.         .width(40)
  45.         .height(40)
  46.       Text(this.getStatusText())
  47.         .fontColor($r('app.color.text_secondary'))
  48.         .fontSize(14)
  49.     }
  50.     .justifyContent(FlexAlign.Center)
  51.     .height(50)
  52.     .width('100%')
  53.   }
  54.   @Builder
  55.   getLoadMoreView() {
  56.     Row() {
  57.       if (!this.hasMore) {
  58.         Text(this.noMoreDataText)
  59.           .fontSize(14)
  60.           .fontColor($r('app.color.text_secondary'))
  61.       } else {
  62.         Row({ space: 8 }) {
  63.           LoadingProgress()
  64.             .width(30)
  65.             .height(30)
  66.             .color(Color.Orange)
  67.           Text(this.loadingMoreText)
  68.             .fontSize(14)
  69.             .fontColor($r('app.color.text_secondary'))
  70.         }
  71.       }
  72.     }
  73.     .height(60)
  74.     .width('100%')
  75.     .justifyContent(FlexAlign.Center)
  76.   }
  77.   build() {
  78.     Refresh({ refreshing: $$this.isRefreshing, builder: this.getRefreshView() }) {
  79.       List() {
  80.         ForEach(this.dataSource, (item: object) => {
  81.           ListItem() {
  82.             if (this.renderItem) {
  83.               this.renderItem(item)
  84.             }
  85.           }
  86.         })
  87.         // 加载更多view
  88.         ListItem() {
  89.           this.getLoadMoreView()
  90.         }
  91.       }
  92.       .onReachEnd(async () => {
  93.         if (!this.isLoadingMore && this.hasMore) {
  94.           this.isLoadingMore = true
  95.           await this.onLoadMore()
  96.           this.isLoadingMore = false
  97.         }
  98.       })
  99.     }
  100.     .onStateChange(async (state) => {
  101.       this.refreshState = state
  102.       if (state === RefreshStatus.Refresh) {
  103.         await this.onRefresh()
  104.         this.isRefreshing = false
  105.       }
  106.     })
  107.   }
  108. }
复制代码
使用:
  1. import { HPList } from '@hp/basic'
  2. import { promptAction } from '@kit.ArkUI'
  3. @Preview
  4. @Component
  5. export struct Task {
  6.   // 模拟数据
  7.   @State dataList: Person[] = [{
  8.     name: '1'
  9.   }, {
  10.     name: '2'
  11.   }, {
  12.     name: '3'
  13.   }, {
  14.     name: '4'
  15.   }, {
  16.     name: '5'
  17.   }, {
  18.     name: '6'
  19.   }, {
  20.     name: '7'
  21.   }, {
  22.     name: '8'
  23.   }, {
  24.     name: '9'
  25.   }, {
  26.     name: '10'
  27.   }, {
  28.     name: '11'
  29.   }, {
  30.     name: '12'
  31.   }]
  32.   @State hasMore: boolean = true
  33.   isFirstIn: boolean = true
  34.   @Builder
  35.   renderItem(item: object) {
  36.     Row() {
  37.       Text(JSON.stringify(item))
  38.         .fontSize(16)
  39.         .textAlign(TextAlign.Center)
  40.     }
  41.     .width('100%')
  42.     .height(80)
  43.     .borderRadius(15)
  44.     .backgroundColor(Color.Pink)
  45.     .justifyContent(FlexAlign.Center)
  46.     .margin({ bottom: 10 })
  47.   }
  48.   async onRefresh() {
  49.     await new Promise<void>((resolve, reject) => {
  50.       setTimeout(() => {
  51.         this.dataList = [{
  52.           name: '旺财'
  53.         },
  54.           {
  55.             name: '张三'
  56.           }, {
  57.             name: '李四'
  58.           }, {
  59.             name: '王五'
  60.           },
  61.           {
  62.             name: '张三1'
  63.           }, {
  64.             name: '李四1'
  65.           }, {
  66.             name: '王五1'
  67.           },
  68.           {
  69.             name: '张三2'
  70.           }, {
  71.             name: '李四2'
  72.           }, {
  73.             name: '王五2'
  74.           }]
  75.         resolve()
  76.       }, 3000)
  77.     });
  78.   }
  79.   /**
  80.    * 加载更多
  81.    */
  82.   async loadMore() {
  83.     // 首次渲染数据为空时,也会调loadMore
  84.     if (this.isFirstIn) {
  85.       this.isFirstIn = false
  86.       return
  87.     }
  88.     promptAction.showToast({ message: 'opppp' })
  89.     await new Promise<void>((resolve, reject) => {
  90.       setTimeout(() => {
  91.         // 模拟数据
  92.         this.dataList = this.dataList.concat(this.dataList.slice(5))
  93.         if (this.dataList.length > 30) {
  94.           this.hasMore = false
  95.         }
  96.         resolve()
  97.       }, 3000)
  98.     });
  99.   }
  100.   build() {
  101.     HPList({
  102.       dataSource: this.dataList,
  103.       hasMore: this.hasMore,
  104.       renderItem: (item: object) => {
  105.         this.renderItem(item)
  106.       },
  107.       onLoadMore:async  () => {
  108.         await this.loadMore()
  109.       },
  110.       onRefresh: async () => {
  111.         await this.onRefresh()
  112.       }
  113.     })
  114.   }
  115. }
  116. class Person {
  117.   name: string = ''
  118. }
复制代码
三、小结



  • 下拉革新Refresh及自定义组件
  • 加载更多及自定义组件

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

风雨同行

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表