DevEco Studio 版本:DevEco Studio NEXT Developer Preview2
HarmonyOS API 版本:4.1.0(11)
PullToRefresh 版本:"@ohos/pulltorefresh": "2.0.5"
下拉刷新控件 PullToRefresh 使用
- 支持自定义 header,footer
- 没有更多了布局
具体代码如下:
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
| import { HomeVM } from '../vm/HomeVM'; import { PullToRefresh } from '@ohos/pulltorefresh';
@Component export struct ListAreaComponent { @State data?: ListDataWrapper[] | null = null private vm: HomeVM = new HomeVM() private scroller: Scroller = new Scroller();
aboutToAppear(): void { this.vm.requestData().then((data: ListDataWrapper[]) => { this.data = data; }) }
build() { PullToRefresh({ data: this.data, scroller: this.scroller, customList: () => { this.getListView() }, onRefresh: () => { return new Promise<string>((resolve) => { this.vm.requestData().then((data: ListDataWrapper[]) => { this.data = data resolve('') }).catch((error: Error) => { resolve('') }); }); }, onLoadMore: () => { return new Promise<string>((resolve) => { resolve('') }); }, customLoad: commonNoMore, customRefresh: commonLoading, }) .width('100%') .height('100%') }
@Builder getListView() { List({ space: 12, scroller: this.scroller }) { ForEach(this.data, (item: ListDataWrapper, index: number) => { ListItem() { }; }, (item: ListDataWrapper, index?: number) => index + JSON.stringify(item)); } .width('100%') .height('100%') .scrollBar(BarState.Off) .edgeEffect(EdgeEffect.None) } }
@Builder export function commonLoading(): void { Stack() { Stack() { Canvas(new CanvasRenderingContext2D(new RenderingContextSettings(true))) .width('100%') .height('100%') .onReady(() => { }) LoadingProgress() .color('#FF00A3FF') .width(32) .height(32) } .width('100%') .height('100%') } .width('100%') .height('100%') .clip(true) }
@Builder export function commonNoMore(): void { Stack() { Text('已经到底了~') .textAlign(TextAlign.Center) .fontColor('#FF85888F') .fontSize(14) } .width('100%') .height('100%') .clip(true) }
@Builder export function commonEmpty(): void { Stack() .width('100%') .height('100%') }
|
注意,列表组件需要设置为无边缘效果:List().edgeEffect(EdgeEffect.None)
。
参考
https://ohpm.openharmony.cn/#/cn/detail/@ohos%2Fpulltorefresh