HarmonyOS鸿蒙Next中List如何实现上拉取下一页数据
HarmonyOS鸿蒙Next中List如何实现上拉取下一页数据 列表数据分页获取,我们可以通过Refresh组件实现List下拉刷新列表,取第一页数据,滚动列表到底部,然后上拉到一定距离之后,需要取下一页数据,如何实现
有一个专门的触发事件,貌似叫onReachEnd,
@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事件,链接如下:
无痕加载请参考lazyforeach方法,链接如下:
下拉刷新可参考refresh组件,链接如下:
如果要使用三方库,参考如下链接:
https://gitee.com/openharmony-sig/PullToRefresh
在HarmonyOS鸿蒙Next中,实现上拉加载下一页数据可以通过ListContainer
和PageSlider
组件结合使用。首先,监听ListContainer
的滚动事件,当用户滚动到底部时触发加载更多数据的逻辑。可以通过OnScrollListener
接口的onScrollEnd
方法来检测滚动结束。在加载数据时,使用PageSlider
来分页请求数据,并将新数据追加到现有列表的末尾。最后,调用ListContainer
的notifyDataChanged
方法刷新列表显示。