react-native /**
- Created by targetcloud on 2016/12/21.
- [url=http://blog.csdn.net/callzjy/article/details/53856163]http://blog.csdn.net/callzjy/article/details/53856163[/url] */ import React, { Component } from 'react'; import { AppRegistry, StyleSheet, Text, View, ListView, Image, TouchableOpacity } from 'react-native';
var CommonCell = require('../Common/CommonCell'); var GuessLikeData = require('../../LocalDatas/guessLike.json'); var Dimensions = require('Dimensions'); var {width} = Dimensions.get('window');
getInitialState(){
return{
dataSource: new ListView.DataSource({rowHasChanged: (row1, row2) => row1 !== row2})
}
},
render() {
return (
<View style={{marginTop:12}}>
<CommonCell leftIcon = 'cnxh' leftTitle = '猜你喜欢'/>
<ListView dataSource={this.state.dataSource} renderRow={this.renderRow}/>
</View>
);
},
renderRow(row){
return(
<TouchableOpacity onPress={()=>alert(row.title)}>
<View style={{backgroundColor:'white',padding:10,borderBottomColor:'#FAFAFA',borderBottomWidth:0.1,flexDirection:'row'}}>
<Image source={{uri: (row.imageUrl.search('w.h') == -1) ? row.imageUrl : row.imageUrl.replace('w.h', '120.90') }} style={{width:120,height:90,borderRadius:8}}/>
<View style={{marginLeft:8,width:width-156,justifyContent:'center'}}>
<View style={{flexDirection:'row',marginBottom:7,justifyContent:'space-between'}}>
<Text style={{fontSize:18,fontWeight:'bold'}}>{row.title}</Text>
<Text style={{color:'grey',alignSelf:'flex-end'}}>{row.topRightInfo}</Text>
</View>
<Text style={{color:'gray'}}>{row.subTitle}</Text>
<View style={{flexDirection:'row',marginTop:7,justifyContent:'space-between'}}>
<View style={{flexDirection:'row',marginTop:7,justifyContent:'flex-start',alignItems:'flex-end'}}>
<Text style={{color:'rgba(33,192,174,1.0)',fontSize:18,fontWeight:'bold'}}>{row.mainMessage+row.mainMessage2} </Text>
<Text style={{color:'grey',}}>{row.subMessage} </Text>
{this.renderCampaignTag(row.campaign)}
</View>
<Text style={{color:'grey',alignSelf:'flex-end'}}>{row.bottomRightInfo}</Text>
</View>
</View>
</View>
</TouchableOpacity>
)
},
renderCampaignTag(campaign){
if (!campaign){
return <Text/>
}else {
return <Text style={{color:'orange',borderWidth:0.5,borderColor:'orange', borderRadius:4, padding:0.1}}>{campaign.tag}</Text>
}
},
componentDidMount(){
fetch(this.props.api_url)
.then((response) => response.json())
.then((responseData) => {
this.setState({
dataSource: this.state.dataSource.cloneWithRows(responseData.data)
});
})
.catch((error)=>{
this.setState({
dataSource: this.state.dataSource.cloneWithRows(GuessLikeData.data)
});
})
},
});
module.exports = GuessLike;