爱凉拌菜真是太好了

react-native学习日记之ListView二

用listView实现九宫格效果

这次时照着es6的写法写的,es6和es5的写法有很多地方不一样
效果图

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
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
ListView,
TouchableOpacity,
Image
} from 'react-native';
import Dimensions from 'Dimensions';
const listData = [
{
src:'a', desc:'第一张'
},
{
src:'b', desc:'第二张'
},
{
src:'c', desc:'第三张'
},
{
src:'d', desc:'第四张'
},
{
src:'e', desc:'第五张'
},
{
src:'b', desc:'第二张'
},
{
src:'c', desc:'第三张'
},
{
src:'d', desc:'第四张'
},
{
src:'e', desc:'第五张'
}
];
// 获取屏幕宽高
var {width,height} = Dimensions.get('window');
var cols = 3; //3列
var cellWidth = 100,cellHeight = 100;
var vMargin = (width-cols*cellWidth)/(cols+1); //九宫格横向margin算法
var hMargin = 20; //九宫格纵向margin
export default class ListViewjiugongge extends Component {
constructor(props) {
super(props);
const ds = new ListView.DataSource({rowHasChanged:(r1,r2)=>r1!==r2});
this.state = {
dataSource: ds.cloneWithRows(listData)
}
}
render() {
return (
<ListView
dataSource={this.state.dataSource}
renderRow={this.renderRow}
// renderSeparator={(sectionID,rowID,adjacentRowHighlighted)=>{
// console.log(sectionID,rowID,adjacentRowHighlighted);
// return(
// <View key={`${sectionID}-${rowID}`}
// style={{height:90,width:1,backgroundColor:adjacentRowHighlighted?'blue':'red'}}
// ></View>
// )
// }}
contentContainerStyle={styles.listViewStyle}
/>
);
}
renderRow(owData,sectionID,rowID,highlightRow) {
return(
<TouchableOpacity activeOpacity={0.5} onPress={()=>{highlightRow(sectionID,rowID)}}>
<View style={styles.cellViewStyle}>
{/*图片src*/}
<Image source={{uri:owData.src}} style={{width:100,height:100,borderRadius:50}}/>
</View>
</TouchableOpacity>
)
}
}
const styles = StyleSheet.create({
listViewStyle: {
flexDirection:'row',
flexWrap:'wrap',
alignItems:'flex-start'
},
cellViewStyle: {
marginLeft:vMargin,
marginTop: hMargin
},
descStyle: {
// flexDirection:'row',
// alignItems:'center'
}
});
AppRegistry.registerComponent('ListViewjiugongge', () => ListViewjiugongge);