1、新建ListViewTest.js文件;
- 導入
ListView
import React, {Component, PropTypes} from 'react';
import {
StyleSheet,
Text,
ListView,
Image,
View
} from 'react-native';
- 在構造方法中創建
datasource
數據源,用相應的clone
方法設置datasource
的初始值
constructor(props) {
super(props);
// 創建datasource數據源
const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.state = {
// 用相應的clone方法設置datasource的初始值
dataSource: ds.cloneWithRows(data.result),//data.result為模擬的數據或服務端得到的數據
isLoading: true
}
this.onLoad()
}
模擬數據
var data = {
"result": [
{
"fullName": "張三",
"email": "e.moore@thompson.org",
"time": "2018-10-10"
},
{
"fullName": "李四",
"email": "r.jackson@johnson.edu",
"time": "2018-10-10"
},
{
"fullName": "王五",
"email": "c.perez@lewis.org",
"time": "2018-10-10"
},
{
"fullName": "趙六",
"email": "k.jackson@martinez.net",
"time": "2018-10-10"
},
{
"fullName": "馮七",
"email": "f.jackson@miller.edu",
"time": "2018-10-10"
},
{
"fullName": "小明",
"email": "o.brown@hernandez.gov",
"time": "2018-10-10"
},
{
"fullName": "小長",
"email": "e.hernandez@martinez.org",
"time": "2018-10-10"
},
{
"fullName": "小紅",
"email": "n.moore@thompson.io",
"time": "2018-10-10"
},
{
"fullName": "小明",
"email": "x.brown@thomas.co.uk",
"time": "2018-10-10"
},
{
"fullName": "小麗",
"email": "j.gonzalez@davis.net",
"time": "2018-10-10"
},
{
"fullName": "張三張三",
"email": "d.lopez@thompson.io",
"time": "2018-10-10"
},
{
"fullName": "張三張三張三張三張三",
"email": "x.brown@thomas.co.uk",
"time": "2018-10-10"
},
{
"fullName": "張三張三張三",
"email": "j.gonzalez@davis.net",
"time": "2018-10-10"
}
]
};
- 渲染
ListView
render() {
return (
<View style={styles.container}>
<ListView
dataSource={this.state.dataSource}//關聯state中的datasource
renderRow={(item) => this.renderRow(item)}//制定listView的顯示效果
//行與行之間的分割線,用renderSeparator實現
renderSeparator={(sectionID, rowID, adjacentRowHighlighted) =>
this.renderSeparator(sectionID, rowID, adjacentRowHighlighted)
}
//頁腳,底部的圖片和文字,提示性,圖片和文字都可以
renderFooter={() => this.renderFooter()}
// 下拉刷新,要用到RefreshControl,需要導入
refreshControl={
<RefreshControl refreshing={this.state.isLoading} onRefresh={() => this.onLoad()}/>
}
/>
<Toast
ref={toast => {
this.toast = toast
}}
/>
</View>
);
}
a、制定listView的顯示效果
renderRow(item) {
return <View style={styles.row}>
<TouchableOpacity onPress={() => {//點擊一行顯示姓名,要用到TouchableOpacity組件,需要導入
this.toast.show('你單擊了:' + item.fullName, DURATION.LENGTH_LONG)
}}>
<Text style={styles.text1}>姓名:{item.fullName}</Text>
<Text style={styles.text2}>郵箱:{item.email}</Text>
<Text style={styles.text3}>time:{item.time}</Text>
</TouchableOpacity>
</View>
}
其中TouchableOpacity
組件是點擊該行的時候彈出Toast
,需要創建一下組件:
<Toast
ref={toast => {
this.toast = toast
}}
/>
b、行與行之間的分割線,用renderSeparator實現
renderSeparator(sectionID, rowID, adjacentRowHighlighted) {
return <View key={rowID} style={styles.line}></View>
}
c、頁腳,底部的圖片和文字,提示性,圖片和文字都可以
renderFooter() {
// return <Image style={{width:400,height:100}} source={{uri:'http://img.zcool.cn/community/0142135541fe180000019ae9b8cf86.jpg@1280w_1l_2o_100sh.png'}}></Image>
return <View style={{justifyContent: "center", height: 20, alignItems: 'center'}}>
<Text style={styles.tip}>我是有底線的</Text>
</View>
}
在這要注意,本地的圖片不需要設置寬高就可以,但是網絡上的圖片需要設置寬高,不然圖片無法顯示
d、下拉刷新,要用到RefreshControl,需要導入
// 刷新的狀態,時間2s
onLoad() {
setTimeout(() => {
this.setState({
isLoading: false
})
}, 2000)
}
2、外部調用
- 導入
import ListView from "./ListViewTest"
- 直接使用
<ListView/>
運行結果:
運行截圖
列表底部有文字的截圖
附帶
ListView.js
源碼:
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import React, {Component, PropTypes} from 'react';
import {
StyleSheet,
Text,
ListView,
Image,
TouchableOpacity,
RefreshControl,
View
} from 'react-native';
import TabNavigator from 'react-native-tab-navigator';
import NavigationBar from "./js/common/NavigationBar";
import Toast, {DURATION} from 'react-native-easy-toast'
var data = {
"result": [
{
"fullName": "張三",
"email": "e.moore@thompson.org",
"time": "2018-10-10"
},
{
"fullName": "李四",
"email": "r.jackson@johnson.edu",
"time": "2018-10-10"
},
{
"fullName": "王五",
"email": "c.perez@lewis.org",
"time": "2018-10-10"
},
{
"fullName": "趙六",
"email": "k.jackson@martinez.net",
"time": "2018-10-10"
},
{
"fullName": "馮七",
"email": "f.jackson@miller.edu",
"time": "2018-10-10"
},
{
"fullName": "小明",
"email": "o.brown@hernandez.gov",
"time": "2018-10-10"
},
{
"fullName": "小長",
"email": "e.hernandez@martinez.org",
"time": "2018-10-10"
},
{
"fullName": "小紅",
"email": "n.moore@thompson.io",
"time": "2018-10-10"
},
{
"fullName": "小明",
"email": "x.brown@thomas.co.uk",
"time": "2018-10-10"
},
{
"fullName": "小麗",
"email": "j.gonzalez@davis.net",
"time": "2018-10-10"
},
{
"fullName": "張三張三",
"email": "d.lopez@thompson.io",
"time": "2018-10-10"
},
{
"fullName": "張三張三張三張三張三",
"email": "x.brown@thomas.co.uk",
"time": "2018-10-10"
},
{
"fullName": "張三張三張三",
"email": "j.gonzalez@davis.net",
"time": "2018-10-10"
},
{
"fullName": "張三張三",
"email": "d.lopez@thompson.io",
"time": "2018-10-10"
},
{
"fullName": "張三張三張三張三張三",
"email": "x.brown@thomas.co.uk",
"time": "2018-10-10"
},
{
"fullName": "張三張三張三",
"email": "j.gonzalez@davis.net",
"time": "2018-10-10"
},
{
"fullName": "張三張三",
"email": "d.lopez@thompson.io",
"time": "2018-10-10"
}
]
};
export default class ListViewTest extends Component<Props> {
constructor(props) {
super(props);
// 創建datasource數據源
const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.state = {
// 用相應的clone方法設置datasource的初始值
dataSource: ds.cloneWithRows(data.result),//data.result為模擬的數據或服務端得到的數據
isLoading: true
}
this.onLoad()
}
renderRow(item) {
return <View style={styles.row}>
<TouchableOpacity onPress={() => {//點擊一行顯示姓名,要用到TouchableOpacity組件
this.toast.show('你單擊了:' + item.fullName, DURATION.LENGTH_LONG)
}}>
<Text style={styles.text1}>姓名:{item.fullName}</Text>
<Text style={styles.text2}>郵箱:{item.email}</Text>
<Text style={styles.text3}>time:{item.time}</Text>
</TouchableOpacity>
</View>
}
renderSeparator(sectionID, rowID, adjacentRowHighlighted) {
return <View key={rowID} style={styles.line}></View>
}
renderFooter() {
// return <Image style={{width:400,height:100}} source={{uri:'http://img.zcool.cn/community/0142135541fe180000019ae9b8cf86.jpg@1280w_1l_2o_100sh.png'}}></Image>
return <View style={{justifyContent: "center", height: 20, alignItems: 'center'}}>
<Text style={styles.tip}>我是有底線的</Text>
</View>
}
// 刷新的狀態,時間2s
onLoad() {
setTimeout(() => {
this.setState({
isLoading: false
})
}, 2000)
}
// 渲染listView
render() {
return (
<View style={styles.container}>
<ListView
dataSource={this.state.dataSource}//關聯state中的datasource
renderRow={(item) => this.renderRow(item)}//制定listView的顯示效果
//行與行之間的分割線,用renderSeparator實現
renderSeparator={(sectionID, rowID, adjacentRowHighlighted) =>
this.renderSeparator(sectionID, rowID, adjacentRowHighlighted)
}
//頁腳,底部的圖片和文字,提示性,圖片和文字都可以
renderFooter={() => this.renderFooter()}
// 下拉刷新,要用到RefreshControl,需要導入
refreshControl={
<RefreshControl refreshing={this.state.isLoading} onRefresh={() => this.onLoad()}/>
}
/>
<Toast
ref={toast => {
this.toast = toast
}}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
text1: {
fontSize: 15,
marginLeft: 10,
marginTop: 7
},
text2: {
fontSize: 15,
marginLeft: 10,
marginTop: 7
},
text3: {
fontSize: 14,
marginLeft: 250,
marginTop: -16,
},
icon: {
height: 22,
width: 22,
},
row: {
height: 60,
},
line: {
height: 1,
backgroundColor: '#F0F0F0'
},
tip: {
fontSize: 10
}
});