import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
ListView,
} from 'react-native';
export default class ListView1Demo extends Component {
constructor(props){
super(props)
// 創建dataSource對象,判斷row更新與否
var ds = new ListView.DataSource({
rowHasChanged:(r1, r2) => r1 !== r2
})
//初始化dataSource,後面建構listview的時候需要用到。
this.state = {
dataSource: ds
}
}
//在render方法裡面建構listview
render() {
return (
<View style={styles.container}>
<ListView
//设置datasource
dataSource = {this.state.dataSource.cloneWithRows(this.generateRowData())}
//渲染row
renderRow = {(rowData) => <Text>{rowData}</Text> }
/>
</View>
);
}
//產生listview內的資料
generateRowData() {
var data = [];
for (var i = 0; i < 100; i++) {
var text = "this is item :" +i;
data.push(text);
}
return data;
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
marginTop: 30,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
});
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
ListView,
} from 'react-native';
export default class SectionListViewDemo extends Component {
constructor(props){
super(props)
var ds = new ListView.DataSource({
//創建dataSource對象,判斷row更新與否
rowHasChanged:(r1, r2) => r1 !== r2,
//更新section的規則
sectionHeaderHasChanged: (s1, s2) => s1 !== s2,
})
this.state = {
sectionHeader: 'Hello',
dataSource: ds,
//這裡使用了字典的方式,那麼會自動將字典的key作為section的部分顯示出來
data: {a:this.generateRowData('a'), b:['l', 'f', 'b1', 'b2'], c:['c', 'cd1', 'c2', 'c3'], d:['l', 'i'], e:this.generateRowData('e')}
}
}
renderRow(rowData, rowId, sectionId) {
return (
<Text>{`${rowData}, index of section:${sectionId}`}</Text>
);
}
//然后在render方法里面构建listview
render() {
return (
<View style={styles.container}>
<Text style={styles.header}>{this.state.sectionHeader}</Text>
<ListView
//设置datasource
dataSource = {this.state.dataSource.cloneWithRowsAndSections(this.state.data)}
//下方是 指定section
//dataSource = {this.state.dataSource.cloneWithRowsAndSections(this.state.data, ['a'])}
//section Header. but ios & android something Differences
renderSectionHeader={(sectionData,sectionID)=>
<View style={styles.section}><Text>{sectionID}</Text></View>}
//renderRow方法的參數默認按照順序如下 (rowData, sectionID, rowID, highlightRow)
renderRow = {(rowData, sectionId, rowId) =>
this.renderRow( rowData, sectionId, rowId)}
/>
</View>
);
}
//產生listview內的資料
generateRowData(sectionName) {
var data = [];
for (var i = 0; i < 25; i++) {
var text = `${sectionName} - ${i}`;
data.push(text);
}
return data;
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
marginTop: 30,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
section: {
alignItems: 'center',
backgroundColor: 'skyblue'
},
header: {
// position: 'absolute',
// top: 0,
// zIndex: 1,
}
});