-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGridView.js
71 lines (65 loc) · 1.85 KB
/
GridView.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
import React, {Component} from 'react';
import {
StyleSheet,
Text,
View,
ListView,
TouchableWithoutFeedback
} from 'react-native';
const styles = StyleSheet.create({
contentContainerStyle: {
flexDirection: 'row',
flexWrap: 'wrap',
alignItems: 'flex-start',
justifyContent: 'space-between'
},
});
export default class GridView extends Component {
constructor(props) {
super(props);
let dividerHorizontal = this.props.dividerHorizontal ? this.props.dividerHorizontal : 0;
let column = this.props.column ? this.props.column : 2;
this.state = {
column: column,
viewWidth: 0,
dividerHorizontal: dividerHorizontal
};
}
_renderItem(data) {
let viewWidth = this.state.viewWidth;
let column = this.state.column;
let dividerHorizontal = this.state.dividerHorizontal;
let itemWidth = (viewWidth - (dividerHorizontal * column - dividerHorizontal)) / column;
let renderItem = this.props.renderItem;
return (
<View style={{width: itemWidth}}>
{renderItem && renderItem(data)}
</View>
);
}
render() {
let refreshControl = this.props.refreshControl ? this.props.refreshControl : null;
return (
<View
style={{flex: 1}}
onLayout={(event) => {
let width = event.nativeEvent.layout.width;
if (!width || width === this.state.viewWidth) {
return;
} else {
this.setState({
viewWidth: width
});
}
}}>
<ListView
style={{flex: 1}}
contentContainerStyle={styles.contentContainerStyle}
dataSource={this.props.dataSource}
renderRow={this._renderItem.bind(this)}
refreshControl={refreshControl}
/>
</View>
);
}
}