下拉刷新也是一个很常用的功能,比如我们在浏览新闻时可以下拉刷新最新资讯等,ArkUI开发框架给我们提供了下拉刷新组件:Refresh
,它的用法很简单,给需要下拉刷新的组件外包上 Refresh
就可以了。
interface RefreshInterface {
(value: { refreshing: boolean; offset?: number | string; friction?: number | string }): RefreshAttribute;
}
简单样例如下所示:
@Entry @Component struct Index {
@State refreshing: boolean = false;
build() {
Column({space: 10}) {
Refresh({ refreshing: this.refreshing }) {
Text(this.refreshing ? "Refreshing" : "Refreshed")
.width('100%')
.fontSize(26)
.height(100)
.textAlign(TextAlign.Center)
.backgroundColor('#aabbcc')
}
.width('100%')
.height(100)
Row({space: 10}) {
Button('Start')
.onClick(() => {
this.refreshing = true;
})
Button('Stop')
.onClick(() => {
this.refreshing = false;
})
}
}
.width('100%')
.height('100%')
}
}
declare class RefreshAttribute extends CommonMethod<RefreshAttribute> {
onStateChange(callback: (state: RefreshStatus) => void): RefreshAttribute;
onRefreshing(callback: () => void): RefreshAttribute;
}
RefreshStatus
定义了以下 5 种状态:Refresh
状态后对外的回调,一般在该回调里进行网络请求等操作。简单样例如下所示:
@Entry @Component struct Index {
@State refreshing: boolean = false;
build() {
Column({space: 10}) {
Refresh({ refreshing: this.refreshing }) {
Text(this.refreshing ? "Refreshing" : "Refreshed")
.width('100%')
.fontSize(26)
.height(100)
.textAlign(TextAlign.Center)
.backgroundColor('#aabbcc')
}
.width('100%')
.height(100)
.onRefreshing(() => {
this.refreshing = true; // 进入刷新状态
setTimeout(() => { // 模拟网络请求,3500毫秒后结束刷新
this.refreshing = false;
}, 3500)
})
}
.width('100%')
.height('100%')
}
}
如果你觉得这篇内容对你还蛮有帮助,我想邀请你帮我三个小忙:
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。