加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 07:24:46 所属栏目:百科 来源:网络整理
导读:/** * Sample React Native App * https://github.com/facebook/react-native * @flow */import React,{ Component } from 'react';import { AppRegistry,StyleSheet,TextInput,ListView,TouchableOpacity,Image,ScrollView,Text,Alert,View} from 'react-na
/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React,{ Component } from 'react';
import {
  AppRegistry,StyleSheet,TextInput,ListView,TouchableOpacity,Image,ScrollView,Text,Alert,View
} from 'react-native';
//导入数据
import ShareData from "./shareData.json";
//获取屏幕宽度
let Dimensions = require("Dimensions");
let {width} = Dimensions.get('window');
//常量设置
let cols = 3;
let cellWH =100;
let vMargin = (width-cellWH*cols)/(cols+1);
let hMargin = 20;


export default class listViewSpeedDial extends Component {
 
 constructor(props){
  super(props);
  //1.设置数据源
  let ds = new ListView.DataSource({rowHasChanged: (r1,r2) => r1 !== r2});
  //2.设置返回数据
  this.state = {dataSource:ds.cloneWithRows(ShareData.data)};
  thiz = this;
 }

  render() {
    return (
     <ListView
      dataSource={this.state.dataSource}
      renderRow={this._renderRow}
      contentContainerStyle={styles.listViewStyle}
     />
    );
  }

  _renderRow(rowData,sectionID,rowID,highlightRow){
     return(
    <TouchableOpacity activeOpacity={0.5} onPress={()=>{thiz._onPress(rowData.title)}}>
    <View style={styles.innerViewStyle}>
      <Image source={{uri:rowData.icon}} style={styles.iconStyle}/>
      <Text>{rowData.title}</Text>
    </View>
    </TouchableOpacity>
    );
  }

  _onPress(e) {

    alert(">>>点击 "+e);
  }

}


const styles = StyleSheet.create({
  listViewStyle:{
    flexDirection:'row',flexWrap:'wrap',
  },iconStyle:{
    width:80,height:80,},innerViewStyle:{
    width:cellWH,height:cellWH,marginLeft:vMargin,marginTop:hMargin,alignItems:'center',}
});


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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读