React Native商城项目实战12 - 首页头部内容
来源:互联网 发布:好的彩票数据分析软件 编辑:程序博客网 时间:2024/04/30 21:40
上图说明:HomeTopView为首页头部内容,HomeTopListView为HomeTopView子视图。
1.HomeTopView.js
/** * Sample React Native App * https://github.com/facebook/react-native * @flow */import React, { Component } from 'react';import { AppRegistry, StyleSheet, Text, View, ScrollView, Image, ListView} from 'react-native';var Dimensions = require('Dimensions');var screenW = Dimensions.get('window').width;/*======导入外部组件类======*/var TopListView = require('./HomeTopListView');// 导入外部json数据var TopMenuJSON = require('../../LocalData/TopMenu.json');// ES5var TopView = React.createClass({ getInitialState(){ return{ activePage:0, } }, render() { return ( <View style={styles.container}> {/*内容部分*/} <ScrollView horizontal={true} pagingEnabled={true} showsHorizontalScrollIndicator={false} onMomentumScrollEnd={this.onScrollAnimationEnd} > {this.renderScrollItem()} </ScrollView> {/*页码*/} <View style={styles.indicatorViewStyle}> {this.renderIndicator()} </View> </View> ); }, // 当一帧滚动结束的时候调用 onScrollAnimationEnd(e){ // 计算当前页码 var currentPage = Math.floor(e.nativeEvent.contentOffset.x / screenW); // 更新状态机 this.setState({ activePage:currentPage, }); }, // 返回子视图 renderScrollItem(){ var itemArr = []; var dataArr = TopMenuJSON.data; for (var i=0;i<dataArr.length;i++){ itemArr.push( <TopListView key={i} dataArr={dataArr[i]} /> ); } return itemArr; }, // 返回页码视图 renderIndicator(){ var indicatorArr = [],style; for (var i=0;i<2;i++){ // 设置圆点的样式 style = (i == this.state.activePage) ? {color:'orange'} : {color:'gray'}; indicatorArr.push( <Text key={i} style={[{fontSize:25},style]}>•</Text> ); } return indicatorArr; }});const styles = StyleSheet.create({ container: { backgroundColor: 'white', }, indicatorViewStyle:{ flexDirection:'row', justifyContent:'center', },});// 输出module.exports = TopView;
2.HomeTopListView.js
/** * Sample React Native App * https://github.com/facebook/react-native * @flow */import React, { Component } from 'react';import { AppRegistry, StyleSheet, Text, View, Image, ListView, TouchableOpacity, Platform} from 'react-native';var Dimensions = require('Dimensions');var screenW = Dimensions.get('window').width;// 全局常量const cols = 5const cellW = Platform.OS == 'ios' ? 70 : 60;const cellH = 70;const vMargin = (screenW - cellW * cols) / (cols + 1);// ES5var TopListView = React.createClass({ getDefaultProps(){ return{ dataArr:[], } }, getInitialState(){ // 创建数据源 var ds = new ListView.DataSource({rowHasChanged:(row1,row2)=>row1 !== row2}); return{ dataSource:ds.cloneWithRows(this.props.dataArr) } }, render() { return ( <ListView dataSource={this.state.dataSource} renderRow={this.renderRow} contentContainerStyle={styles.contentViewStyle} scrollEnabled={false} /> ); }, // 返回具体的一行 renderRow(rowData){ return( <TouchableOpacity activeOpacity={0.8}> <View style={styles.cellStyle}> <Image source={{uri:rowData.image}} style={{width:52,height:52}} /> <Text style={styles.titleStyle}>{rowData.title}</Text> </View> </TouchableOpacity> ) }});const styles = StyleSheet.create({ contentViewStyle:{ flexDirection:'row', flexWrap:'wrap', width:screenW, alignItems:'center', justifyContent:'center', }, cellStyle:{ alignItems:'center', justifyContent:'center', marginTop:10, width:cellW, height:cellH, marginLeft:vMargin, }, titleStyle:{ fontSize:Platform.OS == 'ios' ? 14 : 12, color:'gray' },});// 输出module.exports = TopListView;
3.用到的json数据
{ "data": [ [{ "title" : "美食", "image" : "ms" }, { "title" : "电影", "image" : "dy" }, { "title" : "酒店", "image" : "jd" }, { "title" : "休闲娱乐", "image" : "xxyl" }, { "title" : "外卖", "image" : "wm" }, { "title" : "自助餐", "image" : "zzc" }, { "title" : "KTV", "image" : "ktv" }, { "title" : "火车票机票", "image" : "hcpjp" }, { "title" : "丽人", "image" : "lr" }, { "title" : "周边游", "image" : "zby" } ], [ { "title" : "足疗按摩", "image" : "zlam" }, { "title" : "购物", "image" : "gw" }, { "title" : "今日新单", "image" : "jrxd" }, { "title" : "小吃快餐", "image" : "xckc" }, { "title" : "生活服务", "image" : "shfw" }, { "title" : "甜点饮品", "image" : "tdyp" }, { "title" : "美甲", "image" : "mj" }, { "title" : "景点门票", "image" : "jdmp" }, { "title" : "旅游", "image" : "ly" }, { "title" : "全部分类", "image" : "qbfl" }] ]}
0 0
- React Native商城项目实战12 - 首页头部内容
- React Native商城项目实战11 - 个人中心头部内容
- React Native商城项目实战13 - 首页中间上部分内容
- React Native商城项目实战14 - 首页中间下部分
- React Native商城项目实战15 - 首页购物中心
- React Native商城项目实战05 - 设置首页的导航条
- React Native商城项目实战10 - 个人中心中间内容设置
- react native 实战[商城DEMO]
- React Native商城项目实战01 - 初始化设置
- React Native商城项目实战02 - 主要框架部分(tabBar)
- React Native商城项目实战03 - 包装Navigator
- React Native商城项目实战08 - 设置“More”界面cell
- React Native商城项目实战16 - 购物中心详细页
- React Native实战项目
- React Native商城项目实战04 - 封装TabNavigator.Item的创建
- React Native商城项目实战06 - 设置安卓中的启动页
- React Native商城项目实战07 - 设置“More”界面导航条
- React Native商城项目实战09 - 个人中心自定义cell
- Spring中ApplicationContext的事件机制
- DSOFramer使用
- htons(), ntohl(), ntohs(),htons()这4个函数
- blob与string类型的相互转换
- nodejs调试之使用内建调试工具
- React Native商城项目实战12 - 首页头部内容
- js中this的理解
- computer security读书札记
- Halcon模板匹配——find_shape_model
- 解决mysql 1032 主从错误
- DB2时间查询
- eclipse mars.1 使用jetty 遇到的问题
- Android Studio:正确引入so文件的方法
- KMP(3)--poj2406(next数组的应用)