ListView

本文介绍如何使用 React Native 中的 ListView 组件实现列表视图,包括数据源的配置、滚动刷新功能的添加以及列表项的渲染等核心操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

// 数据源
var contents = ["ROW0","ROW1","ROW2","ROW3","ROW4"];

export default class MyListView extends Component {

    constructor(props){
        super(props);
        // 复用规则
        var ds = new ListView.DataSource({rowHasChanged:(r1, r2) => r1 !== r2});

        this.state = {
            dataSource: ds.cloneWithRows(contents), // 声明变量,赋值
            isRefreshing: false
        };
    }

    render() {
        return(
            <View style={styles.container}>
                <ListView
                    dataSource={this.state.dataSource} // 设置数据源
                    renderRow={this.renderRow} // 渲染cell // 样式设置 contentContainerStyle={styles.contentViewStyle}
                    refreshControl={
                        <RefreshControl
                            refreshing = {this.state.isRefreshing}
                            onRefresh={this.onRefresha.bind(this)} // 必须要绑定-ES6中
                            tintColor="#ff0000"
                            title="Loading..."
                            titleColor="#00ff00"
                        />
                    }
                />
            </View>

        );
    }
    // 刷新
    onRefresha(){

        this.setState({isRefreshing: true});

        setTimeout(() => {
            this.setState({isRefreshing: false});
        }, 1000);
    }
    renderRow(rowData){
        return(
            <View style={styles.itemStyle}>
                <Text>
                    {rowData}
                </Text>
            </View>
        );
    }
    
}

const styles = StyleSheet.create({
    container:{
        flex:1,
        backgroundColor: 'white',
        marginTop: 20
    },
    itemStyle:{
        alignItems: 'center',
        height: 44,
        borderBottomWidth:0.5,
        borderBottomColor: '#e2e2e2',
        justifyContent: 'center'
    }
});

module.exports = MyListView;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值