ReactNative ListView 组件

ReactNative ListView 组件

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

/**
 * ListView 数据列表
 * listView 是最用的设置每行显示的组件
 * section,header
 *
 * 使用ListView。DataSource,给他传递一个普通的数组数据,
 * 在使用dataSource对象实例化一个listView组件
 * listView实现:row/section组件定义、设置数组
 *
 * 设置ListView数据源:
 * state(数据源是不断变化的) 需要将dataSource对象设置为state属性
 */

//原始数据 数组(字符串)
let contents = [
    "1",
    "2",
    "3",
    "4",
    "5",
    "6",
    "7",
    "8",
    "9"
];

let MyListView = React.createClass({
    getInitialState: function () {
        //创建dataSource对象
        let ds = new ListView.DataSource({
            rowHasChanged: (oldRow, newRow) => oldRow !== newRow
        });

        return {
            //设置dataSource时,不直接使用原始数据,使用cloneWithRows对数据源进行复制
            //使用复制后的数据源实例化ListView.
            // 优势:当数据源发生变化时,ListView组件的dataSource不会改变
            dataSource: ds.cloneWithRows(contents)
        };
    },
    //渲染呢row组件,参数是每行要显示的数据对象
    _renderRow: function (rowDate: string) {
        return (
            <View style={styles.row}>
                <Text style={styles.content}>{rowDate}</Text>
            </View>
        );
    },
    render: function () {
        return (
            <ListView
                style={styles.container}
                dataSource={this.state.dataSource}
                renderRow={this._renderRow}
                // initialListSize=""
                // onEndReachedThreshold=""
                // pageSize=""
                // renderScrollComponent=""
                // scrollRenderAheadDistance=""
                // stickyHeaderIndices=""
            />
        );

    }
});
let styles = StyleSheet.create({
    container: {
        flex: 1,
        marginTop: 25
    },
    row: {
        justifyContent: "center",
        alignItems: "center",
        padding: 5,
        height: 100,
        borderBottomWidth: 1,
        borderColor: "#ccc"

    },
    content: {
        flex: 1,
        fontSize: 20,
        color: "blue"
    }
});

module.exports = MyListView;


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值