ListView(Stickey)

react-native中的StickeyListView其实就是带Section的UITableView。
参考:http://moduscreate.com/react-native-listview-with-section-headers/
原理就是二维数组。
下面是代码的实现:

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

var API_URL = 'http://demo9383702.mockable.io/users';

class StickeyListViewDemo extends Component {

  constructor(props) {
    super(props);
    var getSectionData = (dataBlob,sectinoID) => {
      return dataBlob[sectionID];
    };
    var getRowData = (dataBlob,sectionID,rowID) => {
      return dataBlob[sectionID + ':' + rowID];
    };
    this.state = {
      loaded:false,
      dataSource: new ListView.DataSource({
        getSectionData: getSectionData,
        getRowData: getRowData,
        rowHasChanged:(row1,row2) => row1 !== row2,
        sectionHeaderHasChanged:(s1,s2) => s1 !== s2
      })
    };
  }


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

    return this.renderListView();
  }

  renderLoadingView() {
    return (
        <View style={[styles.header]}>
         <Text style={[styles.headerText]}>User List</Text>
          <View>
            <ActivityIndicatorIOS
                animating = {!this.state.loaded}
                style={[styles.activityIndicator,{height: 80}]}
                size='large'
            />
          </View>
        </View>
    );
  }

  renderListView() {
    return (
      <View style={[styles.container]}>
        <View style={[styles.header]}>
          <Text style={[styles.headerText]}>User List</Text>
        </View>
        <ListView
            dataSource={this.state.dataSource}
            style={[styles.listView]}
            renderRow={this.renderRow.bind(this)}
            renderSectionHeader={this.renderSectionHeader.bind(this)}
        />
      </View>
    );
  }

  renderSectionHeader(sectionData,sectionID) {
    return (
      <View style={[styles.section]}>
        <Text style={[styles.text]}>{sectionData}</Text>
      </View>
    );
  }

  renderRow(rowData,sectionID,rowID) {
    return (
      <TouchableOpacity onPress={this.onPressRow.bind(this,rowData)}>
        <View style={[styles.rowStyle]}>
          <Text style={[styles.rowText]}>{rowData.name.title} {rowData.name.first} {rowData.name.last}</Text>
        </View>
      </TouchableOpacity>
    );
  }

  onPressRow(rowData,sectionID) {
    alert(rowData.email);
  }

  componentDidMount() {
    this.fetchData();
  }

  fetchData() {
    fetch(API_URL).then((response) => response.json()).then((responseData) => {
      var orgainzations = responseData.results,
          length = orgainzations.length,
          dataBlob = {},
          sectionIDs = [],
          rowIDs = [],
          organization,
          users,
          userLength,
          user,
          i,
          j;
      for(i=0;i<length;i++) {
        organization = orgainzations[i];

        sectionIDs.push(organization.id);
        dataBlob[organization.id] = organization.organization;

        users = organization.users;
        userLength = users.length;

        rowIDs[i] = [];

        for(j=0;j<userLength;j++) {
          user = users[j].user;
          rowIDs[i].push(user.md5);

          dataBlob[organization.id + ':' + user.md5] = user;
        }
      }
      this.setState({
        dataSource:this.state.dataSource.cloneWithRowsAndSections(dataBlob,sectionIDs,rowIDs),
        loaded:true
      });
          console.log('dataBlob is' + dataBlob);
          console.log('sectionIDs is' + sectionIDs);
          console.log('rowIDs is' + rowIDs)
    }).done();
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1
  },
  activityIndicator: {
    alignItems:'center',
    justifyContent:'center'
  },
  header: {
    height:60,
    justifyContent:'center',
    alignItems:'center',
    backgroundColor:'#3F51B5',
    flexDirection: 'column',
    paddingTop: 25
  },
  headerText: {
    fontWeight: 'bold',
    fontSize: 20,
    color: 'white'
  },
  text: {
    color: 'white',
    //paddingHorizontal: 8,
    fontSize: 16
  },
  rowStyle: {
    paddingVertical: 20,
    paddingLeft: 16,
    borderTopColor: 'white',
    borderLeftColor: 'white',
    borderRightColor: 'white',
    borderBottomColor: '#E0E0E0',
    borderWidth: 1
  },
  rowText: {
    color: '#212121',
    fontSize: 16
  },
  subText: {
    fontSize: 14,
    color: '#757575'
  },
  section: {
    flexDirection: 'column',
    justifyContent: 'center',
    alignItems: 'flex-start',
    paddingLeft: 6,
    backgroundColor: '#2196F3'
  },
  listView: {
    flex:1
  }
});

AppRegistry.registerComponent('StickeyListViewDemo', () => StickeyListViewDemo);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值