React Native 基础篇之 ListView 九宫格实现

前端之家收集整理的这篇文章主要介绍了React Native 基础篇之 ListView 九宫格实现前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. /**
  2. * Sample React Native App
  3. * https://github.com/facebook/react-native
  4. * @flow
  5. */
  6.  
  7. import React,{ Component } from 'react';
  8. import {
  9. AppRegistry,StyleSheet,TextInput,ListView,TouchableOpacity,Image,ScrollView,Text,Alert,View
  10. } from 'react-native';
  11. //导入数据
  12. import ShareData from "./shareData.json";
  13. //获取屏幕宽度
  14. let Dimensions = require("Dimensions");
  15. let {width} = Dimensions.get('window');
  16. //常量设置
  17. let cols = 3;
  18. let cellWH =100;
  19. let vMargin = (width-cellWH*cols)/(cols+1);
  20. let hMargin = 20;
  21.  
  22.  
  23. export default class listViewSpeedDial extends Component {
  24. constructor(props){
  25. super(props);
  26. //1.设置数据源
  27. let ds = new ListView.DataSource({rowHasChanged: (r1,r2) => r1 !== r2});
  28. //2.设置返回数据
  29. this.state = {dataSource:ds.cloneWithRows(ShareData.data)};
  30. thiz = this;
  31. }
  32.  
  33. render() {
  34. return (
  35. <ListView
  36. dataSource={this.state.dataSource}
  37. renderRow={this._renderRow}
  38. contentContainerStyle={styles.listViewStyle}
  39. />
  40. );
  41. }
  42.  
  43. _renderRow(rowData,sectionID,rowID,highlightRow){
  44. return(
  45. <TouchableOpacity activeOpacity={0.5} onPress={()=>{thiz._onPress(rowData.title)}}>
  46. <View style={styles.innerViewStyle}>
  47. <Image source={{uri:rowData.icon}} style={styles.iconStyle}/>
  48. <Text>{rowData.title}</Text>
  49. </View>
  50. </TouchableOpacity>
  51. );
  52. }
  53.  
  54. _onPress(e) {
  55.  
  56. alert(">>>点击 "+e);
  57. }
  58.  
  59. }
  60.  
  61.  
  62. const styles = StyleSheet.create({
  63. listViewStyle:{
  64. flexDirection:'row',flexWrap:'wrap',
  65. },iconStyle:{
  66. width:80,height:80,},innerViewStyle:{
  67. width:cellWH,height:cellWH,marginLeft:vMargin,marginTop:hMargin,alignItems:'center',}
  68. });


代码 链接:http://pan.baidu.com/s/1eSpCz4I 密码:18z4

猜你在找的React相关文章