forked from FaridSafi/react-native-gifted-listview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_advanced.js
334 lines (301 loc) · 7.91 KB
/
example_advanced.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
/**
* Sample React Native App
* https://github.com/facebook/react-native
*/
'use strict';
var React = require('react');
var {
StyleSheet,
Text,
View,
TouchableHighlight,
Platform
} = require('react-native');
var GiftedListView = require('react-native-gifted-listview');
var GiftedSpinner = require('react-native-gifted-spinner');
var Example = React.createClass({
/**
* Will be called when refreshing
* Should be replaced by your own logic
* @param {number} page Requested page to fetch
* @param {function} callback Should pass the rows
* @param {object} options Inform if first load
*/
_onFetch(page = 1, callback, options) {
setTimeout(() => {
var header = 'Header '+page;
var rows = {};
rows[header] = ['row '+((page - 1) * 3 + 1), 'row '+((page - 1) * 3 + 2), 'row '+((page - 1) * 3 + 3)];
if (page === 5) {
callback(rows, {
allLoaded: true, // the end of the list is reached
});
} else {
callback(rows);
}
}, 1000); // simulating network fetching
},
/**
* When a row is touched
* @param {object} rowData Row data
*/
_onPress(rowData) {
console.log(rowData+' pressed');
},
/**
* Render a row
* @param {object} rowData Row data
*/
_renderRowView(rowData) {
return (
<TouchableHighlight
style={customStyles.row}
underlayColor='#c8c7cc'
onPress={() => this._onPress(rowData)}
>
<Text>{rowData}</Text>
</TouchableHighlight>
);
},
/**
* Render a row
* @param {object} rowData Row data
*/
_renderSectionHeaderView(sectionData, sectionID) {
return (
<View style={customStyles.header}>
<Text style={customStyles.headerTitle}>
{sectionID}
</Text>
</View>
);
},
/**
* Render the refreshable view when waiting for refresh
* On Android, the view should be touchable to trigger the refreshCallback
* @param {function} refreshCallback The function to call to refresh the listview
*/
_renderRefreshableWaitingView(refreshCallback) {
if (Platform.OS !== 'android') {
return (
<View style={customStyles.refreshableView}>
<Text style={customStyles.actionsLabel}>
↓
</Text>
</View>
);
} else {
return (
<TouchableHighlight
underlayColor='#c8c7cc'
onPress={refreshCallback}
style={customStyles.refreshableView}
>
<Text style={customStyles.actionsLabel}>
↻
</Text>
</TouchableHighlight>
);
}
},
/**
* Render the refreshable view when the pull to refresh has been activated
* @platform ios
*/
_renderRefreshableWillRefreshView() {
return (
<View style={customStyles.refreshableView}>
<Text style={customStyles.actionsLabel}>
↻
</Text>
</View>
);
},
/**
* Render the refreshable view when fetching
*/
_renderRefreshableFetchingView() {
return (
<View style={customStyles.refreshableView}>
<GiftedSpinner />
</View>
);
},
/**
* Render the pagination view when waiting for touch
* @param {function} paginateCallback The function to call to load more rows
*/
_renderPaginationWaitingView(paginateCallback) {
return (
<TouchableHighlight
underlayColor='#c8c7cc'
onPress={paginateCallback}
style={customStyles.paginationView}
>
<Text style={[customStyles.actionsLabel, {fontSize: 13}]}>
Load more
</Text>
</TouchableHighlight>
);
},
/**
* Render the pagination view when fetching
*/
_renderPaginationFetchigView() {
return (
<View style={customStyles.paginationView}>
<GiftedSpinner />
</View>
);
},
/**
* Render the pagination view when end of list is reached
*/
_renderPaginationAllLoadedView() {
return (
<View style={customStyles.paginationView}>
<Text style={customStyles.actionsLabel}>
~
</Text>
</View>
);
},
/**
* Render a view when there is no row to display at the first fetch
* @param {function} refreshCallback The function to call to refresh the listview
*/
_renderEmptyView(refreshCallback) {
return (
<View style={customStyles.defaultView}>
<Text style={customStyles.defaultViewTitle}>
Sorry, there is no content to display
</Text>
<TouchableHighlight
underlayColor='#c8c7cc'
onPress={refreshCallback}
>
<Text>
↻
</Text>
</TouchableHighlight>
</View>
);
},
/**
* Render a separator between rows
*/
_renderSeparatorView() {
return (
<View style={customStyles.separator} />
);
},
render() {
return (
<View style={screenStyles.container}>
<View style={screenStyles.navBar}>
<Text style={screenStyles.navBarTitle}>Gifted ListView</Text>
</View>
<GiftedListView
rowView={this._renderRowView}
onFetch={this._onFetch}
initialListSize={12} // the maximum number of rows displayable without scrolling (height of the listview / height of row)
firstLoader={true} // display a loader for the first fetching
pagination={true} // enable infinite scrolling using touch to load more
paginationFetchigView={this._renderPaginationFetchigView}
paginationAllLoadedView={this._renderPaginationAllLoadedView}
paginationWaitingView={this._renderPaginationWaitingView}
refreshable={true} // enable pull-to-refresh for iOS and touch-to-refresh for Android
refreshableViewHeight={50} // correct height is mandatory
refreshableDistance={40} // the distance to trigger the pull-to-refresh - better to have it lower than refreshableViewHeight
refreshableFetchingView={this._renderRefreshableFetchingView}
refreshableWillRefreshView={this._renderRefreshableWillRefreshView}
refreshableWaitingView={this._renderRefreshableWaitingView}
emptyView={this._renderEmptyView}
renderSeparator={this._renderSeparatorView}
withSections={true} // enable sections
sectionHeaderView={this._renderSectionHeaderView}
PullToRefreshViewAndroidProps={{
colors: ['#fff'],
progressBackgroundColor: '#003e82',
}}
rowHasChanged={(r1,r2)=>{
r1.id !== r2.id
}}
distinctRows={(rows)=>{
var indentitis = {};
var newRows = [];
for(var i = 0;i<rows.length; i++){
if(indentitis[rows[i].id]){
}else{
indentitis[rows[i].id]=true;
newRows.push(rows[i]);
}
}
return newRows;
}}
/>
</View>
);
}
});
var customStyles = {
separator: {
height: 1,
backgroundColor: '#CCC'
},
refreshableView: {
height: 50,
backgroundColor: '#fff',
justifyContent: 'center',
alignItems: 'center',
},
actionsLabel: {
fontSize: 20,
color: '#007aff',
},
paginationView: {
height: 44,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#FFF',
},
defaultView: {
justifyContent: 'center',
alignItems: 'center',
padding: 20,
},
defaultViewTitle: {
fontSize: 16,
fontWeight: 'bold',
marginBottom: 15,
},
row: {
padding: 10,
height: 44,
},
header: {
backgroundColor: '#50a4ff',
padding: 10,
},
headerTitle: {
color: '#fff',
},
};
var screenStyles = {
container: {
flex: 1,
backgroundColor: '#FFF',
},
navBar: {
height: 64,
backgroundColor: '#007aff',
justifyContent: 'center',
alignItems: 'center',
},
navBarTitle: {
color: '#fff',
fontSize: 16,
marginTop: 12,
}
};
module.exports = Example;