ReactNative中SectionList的使用

ReactNative长列表数据组件一共有三个:

- ListView 核心组件,数据量大时性能较差,占用内存持续增加,故设计出来FlatList组件。

- FlatList 用于替代ListView,支持下拉刷新和上拉加载。

- SectionList 高性能的分组列表组件。

相比之下,SectionListListView简单一些,ListView在使用之前还需要创造datasource数据源,初始化datasource的值等。

1、导入

import {
    StyleSheet,
    Text,
    ListView,
    SectionList,
} from 'react-native';

2、使用

export default class SectionListTest extends Component{
    constructor(props){
        super(props);
    }

    //渲染列表
    _renderItem = (info) => {
        const txt = '  ' + info.item.title;
        return <Text key={info.section.key}
            style={{ height: 60, textAlignVertical: 'center', backgroundColor: "#ffffff", color: '#5C5C5C', fontSize: 15 }}>{txt}</Text>
    };
  //分组
    _sectionComp = (info) => {
        const txt = info.section.key;
        return <Text
            style={{ height: 15, textAlign: 'center', textAlignVertical: 'center', backgroundColor: '#E0E0E0', color: 'white', fontSize: 14 }}>{txt}</Text>
    };

    render() {
        const sections = [
            { key: "A", data: [{ title: "阿童木" }, { title: "阿玛尼" }, { title: "爱多多" }] },
            { key: "B", data: [{ title: "表哥" }, { title: "贝贝" }, { title: "表弟" }, { title: "表姐" }, { title: "表叔" }] },
            { key: "C", data: [{ title: "成吉思汗" }, { title: "超市快递" }] },
            { key: "W", data: [{ title: "王磊" }, { title: "王者荣耀" }, { title: "往事不能回味" },{ title: "王小磊" }, { title: "王中磊" }, { title: "王大磊" }] },
        ];

        return (
            <View style={{ flex: 1 }}>
                <SectionList
                    renderSectionHeader={this._sectionComp}
                    renderItem={this._renderItem}
                    sections={sections}
                    //设置列表之间的样式,边框
                    ItemSeparatorComponent={() => <View style={{height:1,backgroundColor:"#F0F0F0"}}></View>}
                    // ListHeaderComponent={() => <View style={{ backgroundColor: '#25B960', alignItems: 'center', height: 30 }}><Text style={{ fontSize: 18, color: '#ffffff' }}>通讯录</Text></View>}
                    ListFooterComponent={() => <View style={{ backgroundColor: '#20a1a1', alignItems: 'center', height: 20,justifyContent:'center'}}><Text style={{ fontSize: 10, color: '#ffffff' }}>没有更多啦</Text></View>}
                />
            </View>
        );
    }
}

ItemSeparatorComponentListView中的renderSeparator作用是一样的,用于设置每个数据之间的样式
ListHeaderComponentListFooterComponent作用是设置列表头部和底部的提示性用语
运行截图:

image.png

image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,989评论 25 708
  • linux下安装redis: https://my.oschina.net/u/1866821/blog/5063...
    南柯一梦00阅读 274评论 0 0
  • 你爸嗜酒如命。这句话是我妈说的,在我面前说了不止一次。 我自小是由姥姥带大的,和父母在一块的时间很少。所以小时候对...
    叶风眠阅读 416评论 2 5
  • 我们是现实版的SEX AND CITY 。十指力赚,去买那么一朵钻石花,但绝不为了那么一朵钻石花,丢了自己,丢了人...
    冰依洁颖阅读 127评论 0 0
  • 最近很多人反应,肝胆问题频频爆发,有偏头痛的,有刷牙干呕的,有晚上1-3点醒的,有入睡困难的…… 来吧,春天来了,...
    养生自明星阅读 652评论 0 0