React Native 网络获取数据后,listView显示数据
2017-08-18 16:30
316 查看
用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. 定义样式:
如果listview的DataSource是null的话,会加载loading的动画,如果不为空的话,就会显示Listview
3.显示loading...的UI
4.加载完数据后,显示ListView的代码:
UI效果:
完整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效果:
相关文章推荐
- React Native从零开始(八)ListView网络获取数据(Fetch)显示
- (原创)使用AsyncTask(带修改线程池方式)+自定义ImageLoader+LRU算法对图片三级缓存及其显示优化(只有在ListView滑动停止的时候才去网络请求获取图片数据)
- 获取网络数据中的数组显示成ListView的简单流程
- [置顶] 获取网络数据中的数组显示成ListView的简单流程
- json数据解析,并实现将网络json数据获取用listview显示
- 两个Fragment切换,BFragment中的listview 数据获取回来 为什第一次进入不显示第二次进入数据就显示了呢?ListView网络数据没有及时更新
- 根据地址, 通过Retrofit 获取网络数据, 显示在RecycleView 中
- listview中的imageview获取网络图片重复显示的问题
- 从网络读取数据并动态的显示在ListView中
- ListView从网络获取图片及文字显示
- 使用android-rss库实现从网络中获取rss信息,并通过ListView显示
- 请问关于从网络数据库获取数据 并显示的问题
- 获取服务器数据通过Gson解析显示Listview上
- Android ListView从网络获取图片及文字显示---图片缓存,多线程处理
- 获取网络上的图片 显示在listview上面
- 异步获取数据ListView显示
- 从网络读取数据并动态的显示在ListView中
- Android ListView从网络获取图片及文字显示
- 从网络获取数据,并绑定到listview上
- 模拟新闻客户端从服务器获取xml数据并显示到ListView