Skip to content

Instantly share code, notes, and snippets.

@lx7575000
Last active March 2, 2016 16:01
Show Gist options
  • Save lx7575000/550b700c769ff25f8315 to your computer and use it in GitHub Desktop.
Save lx7575000/550b700c769ff25f8315 to your computer and use it in GitHub Desktop.
由于文档中没有具体介绍带有header section的ListView实现,所以对于Rreact Native中ListView的进行了具体分析和实现建议
'use strict';
import React, {
Component,
View,
Text,
NavigatorIOS,
ListView,
} from 'react-native';
//创建100个测试数据,数组内容为 ‘row X’
let mockArray = Array(101).join('1').split('').map((el, index) => {
return 'row ' + (index + 1)});
let mockData = [
{
id:"aaa",
sub: Array(5).fill(1).map((el, index) => {
return "first " + index
})
},
{
id:"bbb",
sub: Array(5).fill(2).map((el, index) => {
return "second " + index
})
},
{
id:"ccc",
sub: Array(5).fill(3).map((el, index) => {
return "third " + index
})
},
{
id:"ddd",
sub: Array(5).fill(4).map((el, index) => {
return "fourth " + index
})
}
]
/*
mockData = [
{
id: 'aaa',
sub: ['first0','first1', 'first2', 'first3' ... 'first10']
},
{
id: 'bbb',
sub: ['second0','second1', 'second2', 'second3' ... 'second10']
},
{
id: 'ccc',
sub: ['third0','third1', 'third2', 'third3' ... 'third10']
},
{
id: 'ddd',
sub: ['fourth0','fourth1', 'fourth2', 'fourth3' ... 'fourth10']
}
]
*/
/*
convertToObject返回的dataBlob, sectionIDs,rowIDs 分别为
dataBlob = {
aaa: {
id: 'aaa',
sub: ['first0','first1', 'first2', 'first3' ... 'first10'],
"aaa0:first 0":"first 0", "aaa1:first 1": "first 1", ... "aaa10:first 10": "first 10",
},
bbb: {
id: 'bbb',
sub: ['second0','second1', 'second2', 'second3' ... 'second10'],
"bbb0:second 0": "second 0", "bbb1:second 1": "second 1", ... "bbb10:second 10": "second 10",
},
ccc: {
id: 'ccc',
sub: ['third0','third1', 'third2', 'third3' ... 'third10'],
"ccc0:third 0":"third 0", "ccc1:third 1":"third 1", ... "ccc10:third 10": "third 10",
},
ddd: {
id: 'ddd',
sub: ['fourth0','fourth1', 'fourth2', 'fourth3' ... 'fourth10'],
"ddd0:fourth 0":"fourth 0" , "ddd1:fourth 1":"fourth 1", ... "ddd10:fourth 10":"fourth 10",
},
}
sectionIDs = [
'aaa', 'bbb', 'ccc', 'ddd'
]
rowIDs = [
['aaa:first0', 'aaa:first1', ..., 'aaa:first10'],
['bbb:second0', 'bbb:second1', ..., 'bbb:second10'],
['ccc:third0', 'ccc:third1', ..., 'ccc:third10'],
['ddd:fourth0', 'ddd:fourth1', ..., 'ddd:fourth10'],
]
*/
export default class ListViewDemo extends Component{
constructor(props){
super(props);
let convertToObject = (arr) => {
let dataBlob = {},
sectionIDs = [],
rowIDs = [];
sectionIDs = arr.map((el, index) => {
let sid = el.id;
dataBlob[sid] = el;
rowIDs[index] = el.sub.map((ele) =>{
let key = el.id + ":" + ele;
dataBlob[key] = ele;
return key;
});
return sid;
});
console.log(dataBlob);
/*
sectionIDs 中存储 sticky标题元素
rowIDs 中分别存储各个单元内的元素
*/
return {
dataBlob,
sectionIDs,
rowIDs,
}
}
let {dataBlob, sectionIDs, rowIDs} = convertToObject(mockData);
let dds = new ListView.DataSource({
getSectionData: this.getSectionData,
getRowData: this.getRowData,
rowHasChanged: (r1, r2) => r1 !== r2,
sectionHeaderHasChanged: (s1, s2) => s1 !== s2,
}).cloneWithRowsAndSections(dataBlob, sectionIDs, rowIDs);
let ds = new ListView.DataSource({
rowHasChanged: (r1, r2) => r1 !== r2
});
this.state = {
dataSource: ds.cloneWithRows(mockArray),
detailDataSource: dds,
}
}
//删了一下对应属性,发现该函数没有调用
getSectionData(dataBlob, sectionID){
return dataBlob[sectionID];
}
/*
dataBlob 对应的就是同名的dataBlob
sectionID 就是各单元对应的 aaa, bbb, ccc, ddd
rowID 对应的就是 rowIDs数组内各个数组的具体元素
*/
getRowData(dataBlob, sectionID, rowID){
// console.log('dataBlob ' + dataBlob);
// console.log('sectionID ' + sectionID);
// console.log('rowID ' + rowID);
return dataBlob[rowID]
}
/*
_renderRow 返回的rowData值为: first0 ....
*/
_renderRow(rowData, sectionID, rowID){
return (
<View style={{flex: 1}}>
<Text>
{rowData}
</Text>
</View>
);
}
/*
_renderSectionHeader 中传入的参数
sectionID 为dataBlob对应的 关键字
sectionData 是dataBlob对象中各关键字对应的值
*/
_renderSectionHeader(sectionData, sectionID){
return(
<View style={{backgroundColor: '#6435c9', flexDirection: 'column', justifyContent: 'center', alignItems: 'flex-start', padding: 6}}>
<Text style={{fontSize: 25, color: 'white'}}>
{`${sectionData.id}`}
</Text>
</View>
)
}
_renderSectionListView(){
return (
<ListView
dataSource={this.state.detailDataSource}
renderRow={this._renderRow}
renderSectionHeader={this._renderSectionHeader}
/>
);
}
_renderSimpleListView(){
return(
<ListView
dataSource={this.state.dataSource}
renderRow={(item) => {
return(
<View style={{flex: 1, flexDirection: 'row',backgroundColor: 'blue'}}>
<Text style={{fontSize: 22}}>{item}</Text>
</View>
)}}
/>
);
}
render(){
return(
<View style={{flex: 1, marginTop: 20}}>
{this._renderSectionListView()}
</View>
)
}
}
>通过遍历sectionIDs中的元素
>>访问得到dataBlob中关键字相对应的对象 设名字为 ValueObject (例如: {'aaa0:first 0' : 'first 0', ....})
>>>然后通过遍历访问rowIDs数组中的各数组内各元素keyValue (例如'aaa0:first 0')
>>>>从ValueObject取出与keyValue相对应的 Value值 (例如: 'first 0')
**因此推荐想用该列表做类似通讯录的页面可以以下面的例子结构来做比较好 **
AddressList = {
'a': {
'啊老师': 123456,
'安静': 654321,
...
},
'b': {
'白静': 567895,
'邦迪': 574893,
...
},
'c':{
'曹操': 747483,
},
.
.
.
.
'z': {
'赵小姐': 1111111,
},
}
function getSectionIDs(data){
let IDs = [];
for(let id in data){
let length = IDs.length;
IDs[length] = id;
}
return IDs;
}
function getRowID(data, IDs){
let Lists = []'
for(let id of IDs){
let rowIDList = [];
for(let {rowid,} in data[id]){
rowIDList.push(rowid);
}
Lists.push(rowIDList);
}
return Lists;
}
sectionIDs = ['a', 'b', 'c', ..., 'z'] //getSectionIDs(AddressList)
rowIDs = [
['啊老师', '安静'],
['白静', '邦迪'],
...
['赵小姐']
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment