主要的属性是ListView 的 contentContainerStyle属性
用来设置ListView的样式
/**
* GridView
*/
import React, { Component } from 'react';
import {
Platform,
StyleSheet,
Text,
View,
Image,
ListView,
TouchableOpacity
} from 'react-native';
var Dimensions=require('Dimensions')
var width=Dimensions.get('window').width;
var rows=3;//有几列
var itemWidth=100;//每隔item的宽度
var mMarginRight=(width-itemWidth*rows)/(rows+1);//计算每个item的右边距
var mMarginBottom=10
var datas=require('./1.json');
export default class GridViewDemo extends Component<{}> {
constructor(props) {
super(props)
//1.设置数据源 固定写法
var ds=new ListView.DataSource({rowHasChanged:(r1,r2)=>r1!==r2})
//2.设置返回的数据 固定写法
this.state={
dataSource:ds.cloneWithRows(datas)
}
}
render() {
return (
<View style={styles.container}>
<ListView
contentContainerStyle={styles.listViewStyle}
dataSource={this.state.dataSource}
renderRow={this.renderRow}>
</ListView>
</View>
);
}
//设置每行的item
renderRow(rowData,sectionID,rowID,highlightRow){
return(
<TouchableOpacity activeOpacity={0.6}>
<View style={styles.ViewStyle}>
<Image source={{uri:rowData.instrument}} style={styles.imageStyle}/>
<Text style={styles.bottomStyle}>
{rowData.firstName}
</Text>
</View>
</TouchableOpacity>
)
}
}
const styles=StyleSheet.create({
container:{
flex:1, //设置全为1说明它占一份,
backgroundColor:'white',
paddingTop:5
},
imageStyle:{
width:80,
height:80
},
listViewStyle:{
flexDirection:'row',
flexWrap:'wrap'
},
ViewStyle:{
width:itemWidth,
height:itemWidth,
marginLeft:mMarginRight,
marginBottom:mMarginBottom,
alignItems:'center'
},
bottomStyle:{
marginTop:3
}
})