前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ReactNative-ListView

ReactNative-ListView

作者头像
czjwarrior
发布2018-05-28 11:14:30
8380
发布2018-05-28 11:14:30
举报
文章被收录于专栏:岑志军的专栏岑志军的专栏

这只是一个简单的listView的小demo

初始化项目之后,index.ios.js代码如下

代码语言:javascript
复制
/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  ListView
} from 'react-native';

import Request from './test/Request';
import TestCell from './test/TestCell'

const Dimensions = require('Dimensions');
const {width, height, scale} = Dimensions.get('window');

export default class DemoApp extends Component {

    constructor(props){
        super(props);

        var ds = new ListView.DataSource({
            rowHasChanged:(r1, r2) => r1 !== r2
        });

        this.state = {
            dataArr: new Array,
            dataSource: ds
        };
    }
  render() {
    return (
      <View style={styles.container}>
        <ListView
            dataSource={this.state.dataSource}
            renderRow={(rowData) => this._renderRow(rowData)}
            contentInset=
        >
        
        </ListView>
      </View>
    );
  }

  _renderRow(rowData){
      return(
        <TestCell wine={rowData}></TestCell>
      )
  };

  componentDidMount() {
      var url_api = 'http://wifi.3wchina.net/index.php/mobile/mobile/appapi';
      Request.get(url_api, (responseData)=>{
            // 取出所有的数据
            var data = responseData;
            
            this.setState({
                dataArr: data,
                dataSource: this.state.dataSource.cloneWithRows(data)
            });
        }, (error)=>{
            alert(error);
        });
  }
}

const styles = StyleSheet.create({
    container: {
        flex:1,
        backgroundColor: '#e8e8e8'
    },
});

AppRegistry.registerComponent('DemoApp', () => DemoApp);

封装简单的get请求

代码语言:javascript
复制
module.exports = {
    /**
     * 基于fetch的get方法
     * @method get
     * @param {string} url
     * @param {function} callback 请求成功回调
     */
    get: function(url, successCallback, failCallback){
        fetch(url)
            .then((response) => response.json())
            .then((responseText) => {
                successCallback(responseText);
            })
            .catch(function(err){
                failCallback(err);
            });
    }
};

cell代码如下

代码语言:javascript
复制
import React, { Component, PropTypes } from 'react';
import {
    AppRegistry,
    StyleSheet,
    Text,
    View,
    TouchableOpacity,
    Image,
    ScrollView,
    InteractionManager,
    DeviceEventEmitter
} from 'react-native';

var Dimensions = require('Dimensions');
var {width, height} = Dimensions.get('window');

class TestCell extends Component{
    // 构造
    constructor(props){
        super(props);

        this.state = {
            wine:this.props.wine
        };
    }

    render(){
        var wine = this.state.wine;
        return(
                <TouchableOpacity style={styles.viewStyle}>
                
                    <TouchableOpacity style={styles.leftView}>
                        <View style=>
                            <Text
                                style={styles.titleStyle}
                                numberOfLines={1}
                            >
                            {wine.title}
                            </Text>
                        </View>
                    </TouchableOpacity>
                </TouchableOpacity>
        )
    }
}

const styles = StyleSheet.create({
    viewStyle:{
        flexDirection: 'row',
        backgroundColor: '#fff',
        borderBottomWidth:1,
        borderBottomColor: '#ccc'
    },

    leftView:{
        width: width * 0.7,
        height:44,
        flexDirection:'row',
        overflow:'hidden',
        alignItems:'center'
    }
});

module.exports = TestCell;

运行效果如下:

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-08-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 这只是一个简单的listView的小demo
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档