react-native学习日记之ListView二 发表于 2017-02-08 用listView实现九宫格效果这次时照着es6的写法写的,es6和es5的写法有很多地方不一样123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100import 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; //九宫格纵向marginexport 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);