I am learning react native and in all the tutorials i see ListView has been used with only 1 items per row. I have not used ListView, though. I have only 6 items that has to be shown as flat grid with 2 items per row and should be responsive. I know its a basic question, but i have tried from my side too which can be seen in the image
This is my code
JavaScript
x
34
34
1
renderDeviceEventList() {
2
return _.map(this.props.deviceEventOptions, deviceEventOption => (
3
<View key={deviceEventOption.id}>
4
<Icon
5
name={deviceEventOption.icon_name}
6
color="#ddd"
7
size={30}
8
onPress={() =>
9
this.props.selectDeviceEvent(deviceEventOption)
10
}
11
/>
12
<Text style={{ color: "#ff4c4c" }}>
13
{deviceEventOption.icon_name}
14
</Text>
15
</View>
16
));
17
}
18
render() {
19
return (
20
<View
21
style={{
22
flex: 1,
23
top: 60,
24
flexDirection: "row",
25
justifyContent: "space-around",
26
flexWrap: "wrap",
27
marginBottom: 10
28
}}
29
>
30
{this.renderDeviceEventList()}
31
</View>
32
);
33
}
34
Advertisement
Answer
To make a 2 row grid using ListView
you could use this code as an example:
JavaScript
1
54
54
1
renderGridItem( item ){
2
return (<TouchableOpacity style={styles.gridItem}>
3
<View style={[styles.gridItemImage, justifyContent:'center', alignItems:'center'}]}>
4
<Text style={{fontSize:25, color:'white'}}>
5
{item.fields.name.charAt(0).toUpperCase()}
6
</Text>
7
</View>
8
<Text style={styles.gridItemText}>{item.fields.name}</Text>
9
</TouchableOpacity>
10
);
11
}
12
13
renderCategories(){
14
15
var listItems = this.dsinit.cloneWithRows(this.state.dataSource);
16
17
return (
18
<ScrollView style={{backgroundColor: '#E8E8E8', flex: 1}} >
19
<ListView
20
contentContainerStyle={styles.grid}
21
dataSource={listItems}
22
renderRow={(item) => this.renderGridItem(item)}
23
/>
24
</ScrollView>
25
);
26
}
27
28
const styles = StyleSheet.create({
29
grid: {
30
justifyContent: 'center',
31
flexDirection: 'row',
32
flexWrap: 'wrap',
33
flex: 1,
34
},
35
gridItem: {
36
margin:5,
37
width: 150,
38
height: 150,
39
justifyContent: 'center',
40
alignItems: 'center',
41
},
42
gridItemImage: {
43
width: 100,
44
height: 100,
45
borderWidth: 1.5,
46
borderColor: 'white',
47
borderRadius: 50,
48
},
49
gridItemText: {
50
marginTop: 5,
51
textAlign:'center',
52
},
53
});
54
Change styles to choose how many rows you want to see on screen. This code is responsive.