前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >RN实现ListView

RN实现ListView

作者头像
提莫队长
发布2018-05-18 15:18:55
1.1K0
发布2018-05-18 15:18:55
举报
文章被收录于专栏:刘晓杰刘晓杰

(1)实现单个item

代码语言:javascript
复制
import React, {Component, PropTypes} from 'react';
import {
    AppRegistry,
    StyleSheet,
    Text,
    View,
    Dimensions,
    Platform,
    Image
} from 'react-native';

const {width, height} = Dimensions.get('window');

export default class GDHotCell extends Component {

    static propTypes = {
        image: PropTypes.string,
        title: PropTypes.string,
    };

    //RawText " " must be wrapped in an explicit <Text> component

    render() {
        return (
            <View style={styles.container}>
                <Image source={{uri: this.props.image}} style={styles.icon}/>
                <View>
                    <Text numberOfLines={3} style={styles.txt}>{this.props.title}</Text>
                </View>
                <Image source={{uri: 'icon_cell_rightarrow'}} style={styles.arrow}/>
            </View>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        flexDirection: 'row',
        alignItems: 'center',
        justifyContent: 'space-between',
        backgroundColor: 'white',
        width: width,
        height: 100,
        borderBottomWidth: 0.5,
        borderBottomColor: 'gray',
        marginLeft: 15,
    },
    icon: {
        width: 70,
        height: 70,
    }
    ,
    arrow: {
        width: 10,
        height: 10,
        marginRight: 30,
    }
    ,
    txt: {
        width: width * 0.6,
    }
    ,
});

(2)生成整个listview

代码语言:javascript
复制
import React, {Component} from 'react';
import {
    AppRegistry,
    StyleSheet,
    Text,
    View,
    TouchableOpacity,
    Image,
    ListView,
    Dimensions
} from 'react-native';

import NavBar from '../main/GDNavBar';
import Cell from '../main/GDHotCell';

const {width, height} = Dimensions.get('window');

export default class GDHalfHourHot extends Component {

    constructor(props) {
        super(props);
        this.state = {
            dataSource: new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2}),
        };
        this.fetchData = this.fetchData.bind(this);
    }

    fetchData() {
        fetch('http://guangdiu.com/api/gethots.php')
            .then((response) => response.json())
            .then((responseData) => {
                this.setState({
                        dataSource: this.state.dataSource.cloneWithRows(responseData.data)
                    }
                );
            }).done();
    };

    renderRow(rowData) {
        return (
            <Cell
                image={rowData.image}
                title={rowData.title}
            />
        );
    };

    componentDidMount() {
        this.fetchData();
    };

    render() {
        return (
            <View style={styles.container}>
                <ListView
                    dataSource={this.state.dataSource}//加载数据
                    renderRow={this.renderRow}
                    showHorizontalScrollIndicator={false}
                    style={styles.listviewStyle}/>
            </View>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        alignItems: 'center',
        backgroundColor: 'white',
    },
    listviewStyle: {
        width: width
    }
});
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017年08月27日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • (1)实现单个item
  • (2)生成整个listview
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档