-
Notifications
You must be signed in to change notification settings - Fork 9
/
RefreshFlatList.android.js
465 lines (432 loc) · 14.5 KB
/
RefreshFlatList.android.js
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/*
* @Author: aran.hu
* @Date: 2017-04-14 14:29:04
* @Last Modified by: aran.hu
* @Last Modified time: 2017-12-27 14:07:14
*/
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import {
AppRegistry,
StyleSheet,
Text,
View,
FlatList,
VirtualizedList,
Dimensions,
Animated,
PanResponder,
Easing,
ScrollView,
TouchableOpacity
} from 'react-native';
import Util from './util'
import Item from './Item'
const { height, width } = Dimensions.get('window');
import AndroidSwipeRefreshLayout from './AndroidSwipeRefreshLayout'
const AnimatedFlatList = Animated.createAnimatedComponent(FlatList);
const AnimatedVirtualizedList = Animated.createAnimatedComponent(VirtualizedList);
// 0: 未刷新; 1: 到达刷新点; 2: 刷新中; 3: 刷新完成
export const RefreshState = {
pullToRefresh: 0,
releaseToRefresh: 1,
refreshing: 2,
refreshdown: 3,
}
export const RefreshText = {
pullToRefresh: 'pull to refresh',
releaseToRefresh: 'release to refresh ',
refreshing: 'refreshing...',
refreshdown: 'refresh complete!'
}
export const FooterText = {
pushToRefresh: 'load more',
loading: 'loading...'
}
export const ViewType = {
ListView: 'ListView',
ScrollView: 'ScrollView'
}
export default class FlatListTest extends Component {
static defaultProps = {
isRefresh: false,
viewType: 'ScrollView',
};
static propTypes = {
customRefreshView: PropTypes.func,
isRefresh: PropTypes.bool,
onRefreshFun: PropTypes.func,
onEndReached: PropTypes.func,
viewType: PropTypes.oneOf(['ListView', 'ScrollView'])
};
constructor() {
super()
this.state = {
_data: Util.makeData(),
rotation: new Animated.Value(0),
rotationNomal: new Animated.Value(0),
refreshState: RefreshState.pullToRefresh,
refreshText: RefreshText.pullToRefresh,
percent: 0,
footerMsg: FooterText.pushToRefresh,
toRenderItem: true
}
this._marginTop = new Animated.Value()
this._scrollEndY = 0
this.headerHeight = 60 // Default refreshView height
this.isAnimating = false // Controls the same animation not many times during the sliding process
this.beforeRefreshState = RefreshState.pullToRefresh
}
componentWillMount() {
const { customRefreshView } = this.props
if (customRefreshView) {
const { height } = customRefreshView(RefreshState.pullToRefresh).props.style
this.headerHeight = height
}
this._marginTop.setValue(-this.headerHeight)
this._marginTop.addListener((v) => {
let p = parseInt(((this.headerHeight + v.value) / (this.headerHeight)) * 100)
if (this.state.refreshState !== RefreshState.refreshdown)
this.setState({ percent: (p > 100 ? 100 : p) + '%' })
})
}
componentDidMount() {
this.initAnimated()
}
componentWillReceiveProps(nextProps, nextState) {
this.setRefreshState(nextProps.isRefresh)
}
shouldComponentUpdate(nextProps, nextState) {
if (this.state.refreshState != RefreshState.refreshing
&& nextState.refreshState == RefreshState.refreshing) {
this.initAnimated()
}
return true
}
componentWillUnmount() {
this.t && clearTimeout(this.t);
this.timer1 && clearTimeout(this.timer1)
this.timer2 && clearTimeout(this.timer2)
}
initAnimated() {
this.state.rotation.setValue(0)
Animated.timing(this.state.rotation, {
toValue: 1,
duration: 1000,
easing: Easing.linear,
}).start((r) => {
if (this.state.refreshState == RefreshState.refreshing) {
this.initAnimated()
}
})
}
// test onRefreshFun
_onRefreshFun = () => {
this.setRefreshState(true)
this.timer1 = setTimeout(() => {
this.setRefreshState(false)
}, 2000)
}
setRefreshState(refreshing) {
const { onRefreshFun } = this.props
if (refreshing) {
this.beforeRefreshState = RefreshState.refreshing
this.updateRefreshViewState(RefreshState.refreshing)
} else {
if (this.beforeRefreshState == RefreshState.refreshing) {
this.beforeRefreshState = RefreshState.pullToRefresh
this.updateRefreshViewState(RefreshState.refreshdown)
} else {
// this.updateRefreshViewState(RefreshState.pullToRefresh)
}
}
}
updateRefreshViewState(refreshState = RefreshState.pullToRefresh) {
switch (refreshState) {
case RefreshState.pullToRefresh:
this.setState({ refreshState: RefreshState.pullToRefresh, refreshText: RefreshText.pullToRefresh }, () => {
Animated.timing(
this._marginTop,
{
toValue: -this.headerHeight,
duration: 200,
easing: Easing.linear
}).start(() => {
this.updateItemRenderState()
})
})
break;
case RefreshState.releaseToRefresh:
this.setState({ refreshState: RefreshState.releaseToRefresh, refreshText: RefreshText.releaseToRefresh })
break;
case RefreshState.refreshing:
this.setState({ refreshState: RefreshState.refreshing, refreshText: RefreshText.refreshing }, () => {
Animated.timing(
this._marginTop,
{
toValue: 0,
duration: 200,
easing: Easing.linear
}).start()
})
break;
case RefreshState.refreshdown:
this.setState({ refreshState: RefreshState.refreshdown, refreshText: RefreshText.refreshdown, toRenderItem: true }, () => {
// This delay is shown in order to show the refresh time to complete the refresh
this.setState({ toRenderItem: false })
this.t = setTimeout(() => {
// 当刷新完成时,先回到初始状态保持100%, 然后在更新组件状态
Animated.timing(
this._marginTop,
{
toValue: -this.headerHeight,
duration: 200,
easing: Easing.linear
}).start(() => {
this.updateRefreshViewState(RefreshState.pullToRefresh)
})
}, 500)
})
break
default:
}
}
// 滑动列表结束后,设置item可以render, 用来接收新的state
updateItemRenderState() {
if (!this.state.toRenderItem) {
this.setState({ toRenderItem: true })
}
}
_onSwipe = (movement) => {
/**
* If you are in the refresh or refresh the completion of the state will not trigger the refresh
*/
// this.setState({toRenderItem: false})
if (this.state.refreshState >= RefreshState.refreshing) return
this._scrollEndY = movement
if(movement >= 0) this._marginTop.setValue( movement - this.headerHeight )
if(movement >= this.headerHeight) {
this.updateRefreshViewState(RefreshState.releaseToRefresh)
} else if (movement < this.headerHeight) {
if (this.state.refreshState === RefreshState.releaseToRefresh)
this.updateRefreshViewState(RefreshState.pullToRefresh)
}
}
_onRefresh = () => {
if (this.state.refreshState >= RefreshState.refreshing) return
if (this._scrollEndY >= this.headerHeight) {
const { onRefreshFun } = this.props
this.setRefreshState(true)
onRefreshFun ? onRefreshFun() : this._onRefreshFun()
} else {
//下拉距离不够自动收回
this.setState({ toRenderItem: true }, () => {
Animated.timing(
this._marginTop,
{
toValue: -this.headerHeight,
duration: 200,
easing: Easing.linear
}).start(() => {
this.updateItemRenderState()
})
})
}
this._scrollEndY = 0
}
_onEndReached = () => {
const { onEndReached } = this.props
if (onEndReached) {
return onEndReached()
}
this.setState({ footerMsg: 'loading' })
this.timer2 = setTimeout(() => {
this.setState({ footerMsg: 'load more' })
}, 1000)
}
// _onTouchStart = () => {
// if(this.state.toRenderItem)
// this.setState({toRenderItem: false})
// }
_onTouchEnd = () => {
if (!this.state.toRenderItem)
this.setState({ toRenderItem: true })
}
_onMomentumScrollEnd = () => {
if (!this.state.toRenderItem)
this.setState({ toRenderItem: true })
}
_onScrollBeginDrag = () => {
if (this.state.toRenderItem)
this.setState({ toRenderItem: false })
}
_onScrollEndDrag = () => {
if (!this.state.toRenderItem)
this.setState({ toRenderItem: true })
}
_isTop = () => {
return this._scrollEndY == 0 ? true : false
}
_renderItem = (data) => {
return <Item
isTriggerPressFn={this._isTop}
{...this.props} data={data}
toRenderItem={this.state.toRenderItem} />
}
customRefreshView = () => {
const { customRefreshView } = this.props
const { refreshState, refreshText, percent } = this.state
if (customRefreshView) return customRefreshView(refreshState, percent)
switch (refreshState) {
case RefreshState.pullToRefresh:
if (!this.isAnimating) {
this.isAnimating = true
Animated.timing(this.state.rotationNomal, {
toValue: 0,
duration: 200,
easing: Easing.linear,
}).start(() => { this.isAnimating = false })
}
return (
<Animated.View style={{ flexDirection: 'row', height: this.headerHeight, justifyContent: 'center', alignItems: 'center', backgroundColor: 'pink', }}>
<Animated.Image
style={{
width: 30, height: 30,
transform: [{
rotateZ: this.state.rotationNomal.interpolate({
inputRange: [0, 1],
outputRange: ['0deg', '180deg']
})
}]
}}
source={require('./img/load-down.png')}
/>
<Text>{refreshText + ' ' + percent}</Text>
</Animated.View>
)
case RefreshState.releaseToRefresh:
if (!this.isAnimating) {
this.isAnimating = true
Animated.timing(this.state.rotationNomal, {
toValue: 1,
duration: 200,
easing: Easing.linear,
}).start(() => { this.isAnimating = false })
}
return (
<Animated.View style={{ flexDirection: 'row', height: this.headerHeight, justifyContent: 'center', alignItems: 'center', backgroundColor: 'pink', }}>
<Animated.Image
style={{
width: 30, height: 30,
transform: [{
rotateZ: this.state.rotationNomal.interpolate({
inputRange: [0, 1],
outputRange: ['0deg', '180deg']
})
}]
}}
source={require('./img/load-down.png')}
/>
<Text>{refreshText + ' ' + percent}</Text>
</Animated.View>
)
case RefreshState.releaseToRefresh:
return (
<Animated.View style={{ justifyContent: 'center', alignItems: 'center', width: width, height: this.headerHeight, backgroundColor: 'pink' }} >
</Animated.View>
)
case RefreshState.refreshing:
return (
<Animated.View style={{ flexDirection: 'row', height: this.headerHeight, justifyContent: 'center', alignItems: 'center', backgroundColor: 'pink', }}>
<Animated.Image
style={{
width: 20, height: 20,
transform: [{
rotateZ: this.state.rotation.interpolate({
inputRange: [0, 1],
outputRange: ['0deg', '360deg']
})
}]
}}
source={require('./img/loading.png')}
/>
<Text>{refreshText}</Text>
</Animated.View>
)
case RefreshState.refreshdown:
return (
<Animated.View style={{ flexDirection: 'row', height: this.headerHeight, justifyContent: 'center', alignItems: 'center', backgroundColor: 'pink', }}>
<Text>{refreshText + ' ' + percent}</Text>
</Animated.View>
)
default:
}
}
_ListFooterComponent = () => {
const { footerMsg } = this.state
const { listFooterComponent } = this.props
if (listFooterComponent) return listFooterComponent()
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center', width: width, height: 30, backgroundColor: 'pink' }} >
<Text style={{ textAlign: 'center', }}> {footerMsg} </Text>
</View>
)
}
render() {
const { viewType, data } = this.props
return (
<AndroidSwipeRefreshLayout
ref={component => this._swipeRefreshLayout = component}
enabledPullUp={true}
enabledPullDown={true}
onSwipe={this._onSwipe}
onRefresh={this._onRefresh}>
{
viewType == ViewType.ScrollView ?
<AnimatedFlatList
ref={flatList => { this._flatList = flatList }}
{...this.props}
data={['1']}
renderItem={this._renderItem}
keyExtractor={(v, i) => i}
ListHeaderComponent={this.customRefreshView}
onTouchEnd={this._onTouchEnd}
onScrollBeginDrag={this._onScrollBeginDrag}
onScrollEndDrag={this._onScrollEndDrag}
onMomentumScrollEnd={this._onMomentumScrollEnd}
style={[{ ...this.props.style },
{
marginTop: this._marginTop.interpolate({
inputRange: [-this.headerHeight, 0, 500],
outputRange: [-this.headerHeight, 0, 150]
})
}]}
/>
:
<AnimatedFlatList
ref={flatList => { this._flatList = flatList }}
{...this.props}
data={data || this.state._data}
keyExtractor={(v, i) => i}
renderItem={this._renderItem}
ListHeaderComponent={this.customRefreshView}
ListFooterComponent={this._ListFooterComponent}
onEndReached={this._onEndReached}
onEndReachedThreshold={10}
onTouchEnd={this._onTouchEnd}
onScrollBeginDrag={this._onScrollBeginDrag}
onScrollEndDrag={this._onScrollEndDrag}
onMomentumScrollEnd={this._onMomentumScrollEnd}
style={[{ ...this.props.style },
{
marginTop: this._marginTop.interpolate({
inputRange: [-this.headerHeight, 0, 500],
outputRange: [-this.headerHeight, 0, 150]
})
}]}
/>
}
</AndroidSwipeRefreshLayout>
);
}
}