React Native 网络获取数据后,listView显示数据

用fetch()的方法获取网络数据,ListView来显示数据。

完整Demo:

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

var REQUEST_URL = 'https://raw.githubusercontent.com/facebook/react-native/master/docs/MoviesExample.json';

const styles = StyleSheet.create({
    container: {
        flex:1,
        flexDirection:'row',
        justifyContent:'center',
        alignItems:'center',
        backgroundColor:'#F5FCFF'
    },
    thunbnail:{
        width:100,
        height:80,
    },

    
});

class TestURL extends Component{
    constructor(props) {
        super(props)
        
        this.state={
           
            dataSource:null
        }
    }

    render() {
        if (!this.state.dataSource) {
            return this.renderLoadingView();
        }
        return this.renderMovie();
    }

    fetchData() {
        var myRequest = new Request(REQUEST_URL);
        myRequest.method='GET';
        const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2})
        fetch(myRequest).then((response) =>  response.json())
        .then((responseData) => this.setState({dataSource:ds.cloneWithRows(responseData.movies)}))
        .catch((error) => {console.log(error)})
    }

    componentDidMount() {
        this.fetchData();
    }

    renderLoadingView() {
        return(
            <View style={styles.container}>
            <Text >
                loading...
            </Text>
            </View>
        );
    }

    renderMovie() {
        return(
            <ListView
                dataSource={this.state.dataSource}
                renderRow={(movie)=>this._renderRow(movie)}
            />

        )
    }

    _renderRow(movie) {
        return (
            <View style={{flexDirection:'row'}}>
            <Image source={{uri:movie.posters.thumbnail}} style={styles.thunbnail}/>
            <Text>{movie.title}</Text>
            </View>
        )
    }

}

AppRegistry.registerComponent('Demo', ()=>TestURL);



下面进行详细介绍:

1. 定义样式:

 

const styles = StyleSheet.create({
    container: {
        flex:1,
        flexDirection:'row',
        justifyContent:'center',
        alignItems:'center',
        backgroundColor:'#F5FCFF'
    },
    thunbnail:{
        width:100,
        height:80,
    },
    
});
2. 数据加载完成前后UI的渲染:

 render() {
        if (!this.state.dataSource) {
            return this.renderLoadingView();
        }
        return this.renderMovie();
    }

如果listview的DataSource是null的话,会加载loading的动画,如果不为空的话,就会显示Listview

3.显示loading...的UI

renderLoadingView() {
        return(
            <View style={styles.container}>
            <Text >
                loading...
            </Text>
            </View>
        );
    }

4.加载完数据后,显示ListView的代码:

 renderMovie() {
        return(
            <ListView
                dataSource={this.state.dataSource}
                renderRow={(movie)=>this._renderRow(movie)}
            />

        )
    }

    _renderRow(movie) {
        return (
            <View style={{flexDirection:'row'}}>
            <Image source={{uri:movie.posters.thumbnail}} style={styles.thunbnail}/>
            <Text>{movie.title}</Text>
            </View>
        )
    }

}

UI效果:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值