1.设置页面跳转
半小时热门组件 GDHalfHourHot.js
/** * 近半小时热门 */ import React, { Component } from 'react'; import { StyleSheet, Text, View, TouchableOpacity, Image, } from 'react-native'; // 引入自定义导航栏组件 import CommunalNavBar from '../main/GDCommunalNavBar'; export default class GDHalfHourHot extends Component { // 返回中间按钮 renderTitleItem() { return( <Text style={styles.navbarTitleItemStyle}>近半小时热门</Text> ); } // 返回右边按钮 renderRightItem() { return( <TouchableOpacity> <Text style={styles.navbarRightItemStyle}>关闭</Text> </TouchableOpacity> ); } render() { return ( <View style={styles.container}> {/* 导航栏样式 */} <CommunalNavBar titleItem = {() => this.renderTitleItem()} rightItem = {() => this.renderRightItem()} /> </View> ); } } const styles = StyleSheet.create({ container: { flex: 1, alignItems: 'center', backgroundColor: '#F5FCFF', }, navbarTitleItemStyle: { fontSize:17, color:'black', marginLeft:50 }, navbarRightItemStyle: { fontSize:17, color:'rgba(123,178,114,1.0)', marginRight:15 }, });
首页调用 GDHome.js
/** * 首页 */ import React, { Component } from 'react'; import { StyleSheet, Text, View, TouchableOpacity, Image, } from 'react-native'; // 引入自定义导航栏组件 import CommunalNavBar from '../main/GDCommunalNavBar'; // 引入 近半小时热门组件 import HalfHourHot from './GDHalfHourHot'; export default class GDHome extends Component { // 跳转到近半小时热门 pushToHalfHourHot() { // this.props 可以获取所有组件属性 this.props.navigator.push({ component: HalfHourHot, }) } // 返回左边按钮 renderLeftItem() { // 将组件返回出去 return( <TouchableOpacity onPress={() => {this.pushToHalfHourHot()}} > <Image source={{uri:'hot_icon_20x20'}} style={styles.navbarLeftItemStyle} /> </TouchableOpacity> ); } // 返回中间按钮 renderTitleItem() { return( <TouchableOpacity> <Image source={{uri:'navtitle_home_down_66x20'}} style={styles.navbarTitleItemStyle} /> </TouchableOpacity> ); } // 返回右边按钮 renderRightItem() { return( <TouchableOpacity> <Image source={{uri:'search_icon_20x20'}} style={styles.navbarRightItemStyle} /> </TouchableOpacity> ); } render() { return ( <View style={styles.container}> {/* 导航栏样式 */} <CommunalNavBar leftItem = {() => this.renderLeftItem()} titleItem = {() => this.renderTitleItem()} rightItem = {() => this.renderRightItem()} /> </View> ); } } const styles = StyleSheet.create({ container: { flex: 1, alignItems: 'center', backgroundColor: '#F5FCFF', }, navbarLeftItemStyle: { 20, height:20, marginLeft:15, }, navbarTitleItemStyle: { 66, height:20, }, navbarRightItemStyle: { 20, height:20, marginRight:15, }, });
效果图:
2.先从 半小时热门 开始,像这样的数据展示,我们肯定是优先选择 ListView ,其中,cell 的样式分解如下:
GDCommunalHotCell.js
/** * 近半小时热门 cell */ import React, { Component, PropTypes } from 'react'; import { StyleSheet, Text, View, Dimensions, Platform, Image, } from 'react-native'; // 获取屏幕宽高 const {width, height} = Dimensions.get('window'); export default class GDCommunalHotCell extends Component { // 定义成员属性 static propTypes = { image: PropTypes.string, // 外部传字符串 title: PropTypes.string, } render() { return ( <View style={styles.container}> {/* 左边的图片 */} <Image source={{uri:this.props.image}} style={styles.imageStyle} /> {/* 中间的文字 */} <View> <Text numberOfLines={3} style={styles.titleStyle}>{this.props.title}</Text> </View> {/* 右边的箭头 */} <Image source={{uri:'icon_cell_rightArrow'}} style={styles.arrowStyle} /> </View> ); } } const styles = StyleSheet.create({ container: { flexDirection:'row', alignItems:'center', justifyContent:'space-between', backgroundColor:'white', height:100, width, borderBottomWidth:0.5, borderBottomColor:'gray', marginLeft:15 }, imageStyle: { 70, height:70, }, titleStyle: { width*0.65, }, arrowStyle: { 10, height:10, marginRight:30 } });
在 GDHalfHourHot.js 引入组件
/** * 近半小时热门 */ import React, { Component } from 'react'; import { StyleSheet, Text, View, TouchableOpacity, Image, ListView, Dimensions, } from 'react-native'; // 获取屏幕宽高 const {width, height} = Dimensions.get('window'); // 引入自定义导航栏组件 import CommunalNavBar from '../main/GDCommunalNavBar'; // 引入 cell import CommunalHotCell from '../main/GDCommunalHotCell'; export default class GDHalfHourHot extends Component { // 构造 constructor(props) { super(props); // 初始状态 this.state = { dataSource: new ListView.DataSource({rowHasChanged:(r1, r2) => r1 !== r2}), // 数据源 优化 }; // 绑定 this.fetchData = this.fetchData.bind(this); } // 网络请求 fetchData() { fetch('http://guangdiu.com/api/gethots.php') // 请求地址 .then((response) => response.json()) // 定义名称 将数据转为json格式 .then((responseData) => { // 处理数据 // 修改dataSource的值 this.setState({ dataSource: this.state.dataSource.cloneWithRows(responseData.data) }); }) .done() // 结束 } popToHome() { this.props.navigator.pop(); } // 返回中间按钮 renderTitleItem() { return( <Text style={styles.navbarTitleItemStyle}>近半小时热门</Text> ); } // 返回右边按钮 renderRightItem() { return( <TouchableOpacity> <Text style={styles.navbarRightItemStyle}>关闭</Text> </TouchableOpacity> ); } // 返回每一行cell的样式 renderRow(rowData) { // 使用cell组件 return( <CommunalHotCell image={rowData.image} title={rowData.title} /> ); } // 生命周期 组件渲染完成 已经出现在dom文档里 componentDidMount() { // 请求数据 this.fetchData(); } render() { return ( <View style={styles.container}> {/* 导航栏样式 */} <CommunalNavBar titleItem = {() => this.renderTitleItem()} rightItem = {() => this.renderRightItem()} /> {/* 商品列表 */} <ListView dataSource={this.state.dataSource} // 数据源 通过判断dataSource是否有变化,来判断是否要重新渲染、 renderRow={this.renderRow} showsHorizontalScrollIndicator={false} // 隐藏水平线 style={styles.listViewStyle} /> </View> ); } } const styles = StyleSheet.create({ container: { flex:1, alignItems: 'center', }, navbarTitleItemStyle: { fontSize:17, color:'black', marginLeft:50 }, navbarRightItemStyle: { fontSize:17, color:'rgba(123,178,114,1.0)', marginRight:15 }, listViewStyle: { width, } });