|
| 1 | +import React, { Component } from 'react' |
| 2 | +import { Alert, Dimensions, Platform, View } from 'react-native' |
| 3 | +import { Button, Header, Icon, Input, Item, Left, Right, Text } from 'native-base' |
| 4 | +import UltimateListView from 'react-native-ultimate-listview' |
| 5 | +// import { UltimateListView } from '../lib/index' |
| 6 | +import styles from './styles' |
| 7 | +import LoadingSpinner from './loadingSpinner' |
| 8 | +import ControlTab from './controlTab' |
| 9 | +import FlatListItem from './itemContainer/flatListItem' |
| 10 | +import FlatListGrid from './itemContainer/flatListGrid' |
| 11 | + |
| 12 | +const { width, height } = Dimensions.get('window') |
| 13 | +export default class Example extends Component { |
| 14 | + constructor(props) { |
| 15 | + super(props) |
| 16 | + this.state = { |
| 17 | + layout: 'list', |
| 18 | + text: '' |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + onFetch = async (page = 1, startFetch, abortFetch) => { |
| 23 | + try { |
| 24 | + // This is required to determinate whether the first loading list is all loaded. |
| 25 | + let pageLimit = 24 |
| 26 | + if (this.state.layout === 'grid') pageLimit = 60 |
| 27 | + const skip = (page - 1) * pageLimit |
| 28 | + |
| 29 | + // Generate dummy data |
| 30 | + let rowData = Array.from({ length: pageLimit }, (value, index) => `item -> ${index + skip}`) |
| 31 | + |
| 32 | + // Simulate the end of the list if there is no more data returned from the server |
| 33 | + if (page === 10) { |
| 34 | + rowData = [] |
| 35 | + } |
| 36 | + |
| 37 | + // Simulate the network loading in ES7 syntax (async/await) |
| 38 | + await this.sleep(2000) |
| 39 | + startFetch(rowData, pageLimit) |
| 40 | + } catch (err) { |
| 41 | + abortFetch() // manually stop the refresh or pagination if it encounters network error |
| 42 | + console.log(err) |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + onChangeLayout = (event) => { |
| 47 | + this.setState({ text: '' }) |
| 48 | + switch (event.nativeEvent.selectedSegmentIndex) { |
| 49 | + case 0: |
| 50 | + this.setState({ layout: 'list' }) |
| 51 | + break |
| 52 | + case 1: |
| 53 | + this.setState({ layout: 'grid' }) |
| 54 | + break |
| 55 | + default: |
| 56 | + break |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + onChangeScrollToIndex = (num) => { |
| 61 | + this.setState({ text: num }) |
| 62 | + let index = num |
| 63 | + if (this.state.layout === 'grid') { |
| 64 | + index = num / 3 |
| 65 | + } |
| 66 | + try { |
| 67 | + this.listView.scrollToIndex({ viewPosition: 0, index: Math.floor(index) }) |
| 68 | + } catch (err) { |
| 69 | + console.warn(err) |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + onPressItem = (type, index, item) => { |
| 74 | + Alert.alert(type, `You're pressing on ${item}`) |
| 75 | + } |
| 76 | + |
| 77 | + sleep = time => new Promise(resolve => setTimeout(() => resolve(), time)) |
| 78 | + |
| 79 | + renderItem = (item, index, separator) => { |
| 80 | + if (this.state.layout === 'list') { |
| 81 | + return ( |
| 82 | + <FlatListItem item={item} index={index} onPress={this.onPressItem} /> |
| 83 | + ) |
| 84 | + } else if (this.state.layout === 'grid') { |
| 85 | + return ( |
| 86 | + <FlatListGrid item={item} index={index} onPress={this.onPressItem} /> |
| 87 | + ) |
| 88 | + } |
| 89 | + return null |
| 90 | + } |
| 91 | + |
| 92 | + renderControlTab = () => ( |
| 93 | + <ControlTab |
| 94 | + layout={this.state.layout} |
| 95 | + onChangeLayout={this.onChangeLayout} |
| 96 | + /> |
| 97 | + ) |
| 98 | + |
| 99 | + renderHeader = () => ( |
| 100 | + <View> |
| 101 | + <View style={styles.header}> |
| 102 | + <Text style={{ textAlign: 'center' }}>I am the Header View, you can put some Instructions or Ads Banner here! |
| 103 | + </Text> |
| 104 | + </View> |
| 105 | + <View style={styles.headerSegment}> |
| 106 | + <Left style={{ flex: 0.15 }} /> |
| 107 | + {this.renderControlTab()} |
| 108 | + <Right style={{ flex: 0.15 }} /> |
| 109 | + </View> |
| 110 | + </View> |
| 111 | + ) |
| 112 | + |
| 113 | + renderPaginationFetchingView = () => ( |
| 114 | + <LoadingSpinner height={height * 0.2} text="loading..." /> |
| 115 | + ) |
| 116 | + |
| 117 | + render() { |
| 118 | + return ( |
| 119 | + <View style={styles.container}> |
| 120 | + <Header searchBar rounded> |
| 121 | + <Item style={{ backgroundColor: 'lightgray', borderRadius: 5 }}> |
| 122 | + <Icon name="ios-search" /> |
| 123 | + <Input placeholder="Search" onChangeText={this.onChangeScrollToIndex} value={this.state.text} /> |
| 124 | + </Item> |
| 125 | + </Header> |
| 126 | + <UltimateListView |
| 127 | + ref={ref => this.listView = ref} |
| 128 | + key={this.state.layout} // this is important to distinguish different FlatList, default is numColumns |
| 129 | + onFetch={this.onFetch} |
| 130 | + keyExtractor={(item, index) => `${index} - ${item}`} // this is required when you are using FlatList |
| 131 | + refreshableMode="advanced" // basic or advanced |
| 132 | + |
| 133 | + item={this.renderItem} // this takes three params (item, index, separator) |
| 134 | + numColumns={this.state.layout === 'list' ? 1 : 3} // to use grid layout, simply set gridColumn > 1 |
| 135 | + |
| 136 | + // ----Extra Config---- |
| 137 | + displayDate |
| 138 | + header={this.renderHeader} |
| 139 | + paginationFetchingView={this.renderPaginationFetchingView} |
| 140 | + // sectionHeaderView={this.renderSectionHeaderView} //not supported on FlatList |
| 141 | + // paginationFetchingView={this.renderPaginationFetchingView} |
| 142 | + // paginationAllLoadedView={this.renderPaginationAllLoadedView} |
| 143 | + // paginationWaitingView={this.renderPaginationWaitingView} |
| 144 | + // emptyView={this.renderEmptyView} |
| 145 | + // separator={this.renderSeparatorView} |
| 146 | + |
| 147 | + // new props on v3.2.0 |
| 148 | + arrowImageStyle={{ width: 20, height: 20, resizeMode: 'contain' }} |
| 149 | + dateStyle={{ color: 'lightgray' }} |
| 150 | + refreshViewStyle={Platform.OS === 'ios' ? { height: 80, top: -80 } : { height: 80 }} |
| 151 | + refreshViewHeight={80} |
| 152 | + /> |
| 153 | + </View> |
| 154 | + ) |
| 155 | + } |
| 156 | +} |
0 commit comments