React Native实例之房产搜索APP

前端之家收集整理的这篇文章主要介绍了React Native实例之房产搜索APP前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

React Native 开发越来越火了,web app也是未来的潮流,现在react native已经可以完成一些最基本的功能. 通过开发一些简单的应用,可以更加熟练的掌握 RN 的知识. 在学习的过程,发现了一个房产搜索APP的实例,但只有ios版本,
本文主要是房产搜索APP的android版本实现。

原Ios版本
React Native 实例 - 房产搜索App Mystra

原版效果

主要内容:

  • 使用Navigator栈@R_730_404@面.
  • 使用fetch请求网络数据.
  • 使用ListView展示列表数据.

首页搜索

搜索页(SearchPage)包含一个搜索库,可以使用地址或邮编搜索英国的房产信息.

通过输入框的参数创建网络请求URL,并把请求发送出去,获取信息.

  1. /** * 访问网络服务的Api,拼接参数 * 输出: http://api.nestoria.co.uk/api?country=uk&pretty=1&encoding=json&listing_type=buy&action=search_listings&page=1&place_name=London * * @param key 最后一个参数的键,用于表示地理位置 * @param value 最后一个参数的值,具体位置 * @param pageNumber page的页面数 * @returns {string} 网络请求的字符串 */
  2. function urlForQueryAndPage(key,value,pageNumber) {
  3. var data = {
  4. country: 'uk',pretty: '1',encoding: 'json',listing_type: 'buy',action: 'search_listings',page: pageNumber
  5. };
  6.  
  7. data[key] = value;
  8.  
  9. var querystring = Object.keys(data)
  10. .map(key => key + '=' + encodeURIComponent(data[key]))
  11. .join('&');
  12. return 'http://api.nestoria.co.uk/api?' + querystring;
  13. }
  14.  
  15. class SearchPage extends Component {
  16.  
  17. /** * 构造器 * @param props 状态 */
  18. constructor(props) {
  19. super(props);
  20. this.state = {
  21. searchString: 'London',// 搜索
  22. isLoading: false,// 加载
  23. message: '' // 消息
  24. };
  25. }
  26.  
  27. onSearchTextChanged(event) {
  28. this.setState({searchString: event.nativeEvent.text});
  29. console.log(this.state.searchString);
  30. }
  31.  
  32.  
  33. /** * 执行网络请求,下划线表示私有 * @param query url * @private */
  34. _executeQuery(query) {
  35. console.log(query);
  36. this.setState({isLoading: true});
  37.  
  38. // 网络请求
  39. fetch(query)
  40. .then(response => response.json())
  41. .then(json => this._handleResponse(json.response))
  42. .catch(error => this.setState({
  43. isLoading: false,message: 'Something bad happened ' + error
  44. }));
  45. }
  46.  
  47. /** * 处理网络请求的回调 * @param response 请求的返回值 * @private */
  48. _handleResponse(response) {
  49. const { navigator } = this.props;
  50. this.setState({isLoading: false,message: ''});
  51. if (response.application_response_code.substr(0,1) === '1') {
  52. console.log('123');
  53. console.log('Properties found: ' + response.listings);
  54.  
  55. // 使用listings调用结果页面SearchResults
  56. navigator.push({
  57. title: '搜索结果',component: SearchResults,index:1,params:{
  58. listings:response.listings,mynav:navigator
  59.  
  60. }
  61.  
  62. });
  63. console.log('456');
  64. } else {
  65. this.setState({message: 'Location not recognized; please try again.'});
  66. }
  67. }
  68.  
  69. /** * 查询的点击事件 */
  70. onSearchPressed() {
  71. var query = urlForQueryAndPage('place_name',this.state.searchString,1);
  72. this._executeQuery(query);
  73. }
  74.  
  75. render() {
  76. var spinner = this.state.isLoading ?
  77. (<ActivityIndicator size='large'/>) : (<View/>);
  78. return (
  79. <View style={styles.container}>
  80. <Text style={styles.description}>
  81. 搜索英国的房产
  82. </Text>
  83. <Text style={styles.description}>
  84. 地址(London)/邮编(W1S 3PR)均可
  85. </Text>
  86. <View style={styles.flowRight}>
  87. <TextInput
  88. style={styles.searchInput}
  89. value={this.state.searchString}
  90. onChange={this.onSearchTextChanged.bind(this)} // bind确保使用组件的实例
  91. placeholder='Search via name or postcode'/>
  92. <TouchableHighlight
  93. style={styles.button}
  94. underlayColor='#99d9f4'
  95. onPress={this.onSearchPressed.bind(this)}>
  96. <Text style={styles.buttonText}>Go</Text>
  97. </TouchableHighlight>
  98. </View>
  99. <Image source={require('./resources/house.png')}
  100. style={styles.image}/>
  101. {spinner}
  102. <Text style={styles.description}>
  103. {this.state.message}
  104. </Text>
  105. </View>
  106. );
  107. }
  108. }

搜索结果

获取的房产信息,逐行渲染并显示于ListView中.

  1. class SearchResults extends Component {
  2.  
  3. /** * 构造器,通过Navigator调用传递参数(passProps) * @param props 状态属性 */
  4. constructor(props) {
  5. super(props);
  6. var dataSource = new ListView.DataSource(
  7. {rowHasChanged: (r1,r2) => r1.guid!== r2.guid}
  8. );
  9. this.state = {
  10. dataSource: dataSource.cloneWithRows(this.props.listings)
  11. };
  12. }
  13.  
  14. /** * 点击每行,通过行数选择信息 * @param propertyGuid 行ID */
  15. rowPressed(propertyGuid) {
  16. //var property = this.props.listings.filter(prop => prop.guid == propertyGuid)[0];
  17. var property=this.props.listings[propertyGuid];
  18. var mynav=this.props.mynav;
  19. mynav.push({
  20. title: '房产信息',component: PropertyView,index:2,params:{
  21. property:property,//navigator:this.props.navigator
  22. mynav2:mynav
  23.  
  24. }
  25. });
  26. }
  27.  
  28. /** * 渲染列表视图的每一行 * @param rowData 行数据 * @param sectionID 块ID * @param rowID 行ID * @returns {XML} 页面 */
  29. renderRow(rowData,sectionID,rowID) {
  30. var price = rowData.price_formatted.split(' ')[0];
  31. return (
  32. <TouchableHighlight
  33. onPress={()=>this.rowPressed(rowID)}
  34. underlayColor='#dddddd'>
  35. <View style={styles.rowContainer}>
  36. <Image style={styles.thumb} source={{uri:rowData.img_url}}/>
  37. <View style={styles.textContainer}>
  38. <Text style={styles.price}>${price}</Text>
  39. <Text style={styles.title} numberOfLines={1}>
  40. {rowData.title}
  41. </Text>
  42. </View>
  43. </View>
  44. </TouchableHighlight>
  45. );
  46. }
  47.  
  48. /** * 渲染,每行使用renderRow渲染 * @returns {XML} 结果页面的布局 */
  49. render() {
  50. return (
  51. <ListView
  52. dataSource={this.state.dataSource}
  53. renderRow={this.renderRow.bind(this)}
  54. />
  55. );
  56. }
  57. }

房产信息
房产信息是单纯显示页面,显示图片文字内容.

  1. BackAndroid.addEventListener('hardwareBackPress',function() {
  2. if(_navigator == null){
  3. return false;
  4. }
  5. if(_navigator.getCurrentRoutes().length === 1){
  6. return false;
  7. }
  8. _navigator.pop();
  9. return true;
  10. });
  11.  
  12. var _navigator ;
  13. var PropertyView = React.createClass({
  14. getInitialState: function() {
  15. _navigator = this.props.mynav2;
  16. return {
  17.  
  18. };
  19. },render: function() {
  20. var property = this.props.property; // 由SearchResult传递的搜索结果
  21. var stats = property.bedroom_number + ' bed ' + property.property_type;
  22. if (property.bathroom_number) {
  23. stats += ',' + property.bathroom_number + ' ' +
  24. (property.bathroom_number > 1 ? 'bathrooms' : 'bathroom');
  25. }
  26.  
  27. var price = property.price_formatted.split(' ')[0];
  28.  
  29. return (
  30. <View> <View style={styles.container}> <Image style={styles.image} source={{uri: property.img_url}}/> <View style={styles.heading}> <Text style={styles.price}>${price}</Text> <Text style={styles.title}>{property.title}</Text> <View style={styles.separator}/> </View> <Text style={styles.description}>{stats}</Text> <Text style={styles.description}>{property.summary}</Text> </View> </View> ); } });

Codes

房产搜索APP安卓版

欢迎大家Follow,Star.

本文参考自wangchenlong

OK,that’s all! Enjoy it!

猜你在找的React相关文章