NoticeList.ets
5.2 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { AxiosResponse } from '@ohos/axios'
import { getNoticeList, noticeTest, noticeParams, noticeRow } from '../api/notice'
import { router } from '@kit.ArkUI'
@Entry
@Component
struct NoticeList {
@State params: noticeParams = {
pageNum: 1,
pageSize: 10,
noticeTitle: ''
}
@State totalAll: number = 0
@State noticeList: noticeRow[] = []
@State refreshing: boolean = false;
@State refreshOffset: number = 0;
@State canLoad: boolean = false;
@State refreshState: RefreshStatus = RefreshStatus.Inactive; // 刷新状态
@State isLoading: boolean = false; // 是否在加载
@State loadingText: string = ''; // 加载是文字
getList = async () => {
const result: AxiosResponse<noticeTest> = await getNoticeList(this.params)
this.noticeList = result.data.rows
this.totalAll = result.data.total
}
async aboutToAppear(){
await this.getList()
}
// 自定义刷新布局
@Builder
refreshBuilder() {
Stack({ alignContent: Alignment.Bottom }) {
// 可以通过刷新状态控制是否存在Progress组件
// 当刷新状态处于下拉中或刷新中状态时Progress组件才存在
if (this.refreshState != RefreshStatus.Inactive && this.refreshState != RefreshStatus.Done) {
Row() {
LoadingProgress().height(32)
Text("Refreshing...").fontSize(16).margin({ left: 20 })
}
.alignItems(VerticalAlign.Center)
}
}
.clip(true)
.height("100%")
.width("100%")
}
// 自定义加载布局
@Builder
footer() {
Row() {
LoadingProgress().height(32).width(48)
.visibility(this.isLoading ? Visibility.Visible : Visibility.None)
Text(this.loadingText)
}.width("100%")
.height(64)
.justifyContent(FlexAlign.Center)
}
// 刷新测试数据
private async refreshData(){
this.noticeList = []
this.params = {
pageNum: 1,
pageSize: 10,
}
await this.getList()
this.refreshing = false;
}
// 加载更多测试数据
private async loadMoreData(){
if(this.params.pageSize >= this.totalAll) {
this.isLoading = false
this.loadingText = '已加载全部数据'
}else {
this.params.pageSize += 10
this.loadingText = '加载中'
await this.getList()
this.isLoading = false
}
}
build() {
Column(){
// 顶部搜索
// 顶部搜索
Row(){
Search({ value: $$this.params.noticeTitle, placeholder: '请输入公告标题' })
.searchButton('搜索', {
fontSize: 12
})
.width('95%')
.height(30)
.backgroundColor('#fff')
.placeholderColor(Color.Grey)
.placeholderFont({ size: 12, weight: 400 })
.textFont({ size: 12, weight: 400 })
.layoutWeight(1)
.onSubmit(async () => {
this.noticeList = []
this.params.pageSize = 10
await this.getList()
})
}.padding({left: 5, right: 5})
Refresh({ refreshing: $$this.refreshing, builder: this.refreshBuilder() }) {
List({space: 10}) {
ForEach(this.noticeList, (item: noticeRow, index: number) => {
ListItem(){
Row(){
Column(){
Text(item.noticeTitle).fontSize(14).fontWeight(600).lineHeight(19).margin({bottom: 5})
.fontColor('#3d3d3d').textOverflow({overflow: TextOverflow.Ellipsis}).maxLines(1)
Text(item.createTime).fontColor('#999').fontSize(12).lineHeight(16)
}.alignItems(HorizontalAlign.Start).layoutWeight(1)
Image($r('app.media.right_3')).width(12)
}.width('100%').justifyContent(FlexAlign.SpaceBetween)
.border({width: {bottom: this.noticeList.length == index + 1 ? 0 : 1}, color: '#eee'}).padding({top: 15, bottom: 15})
.onClick(() => {
router.pushUrl({
url: 'pages/NoticeDetail',
params: {
id: item.noticeId
}
})
})
}.padding({left: 10, right: 10}).backgroundColor(Color.White)
})
ListItem() {
this.footer();
}
}
.onScrollIndex((start: number, end: number) => {
// 当达到列表末尾时,触发新数据加载
if (this.canLoad && end >= this.noticeList.length - 1) {
this.canLoad = false;
this.isLoading = true;
this.loadMoreData()
}
})
.onScrollFrameBegin((offset: number) => {
// 只有当向上滑动时触发新数据加载
if (offset > 5 && !this.isLoading) {
this.canLoad = true;
}
return { offsetRemain: offset };
})
.scrollBar(BarState.Off)
// 开启边缘滑动效果
.edgeEffect(EdgeEffect.Spring, { alwaysEnabled: true })
}
.width('100%')
.height('100%')
.backgroundColor('#f2f3f7')
.padding(10)
.onOffsetChange((offset: number) => {
this.refreshOffset = offset;
})
.onStateChange((state: RefreshStatus) => {
this.refreshState = state;
})
.onRefreshing(() => {
this.refreshData()
})
}.height('100%')
}
}