这个是效果:

第一步:准备数据源:Car.json

{"data": [
{
"cars": [
{
"icon": "m_180_100.png",
"name": "AC Schnitzer"
},
{
"icon": "m_92_100.png",
"name": "阿尔法·罗密欧"
},
{
"icon": "m_9_100.png",
"name": "奥迪"
},
{
"icon": "m_97_100.png",
"name": "阿斯顿·马丁"
}
],
"title": "A"
}........
}

第二步:

import React, {Component} from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
SectionList,
Dimensions,
Image
} from 'react-native'; const dimension = Dimensions.get('window')
var Car = require('./Car.json'); var itemWidth = 100;
var cols = 3;
var cMargin = (dimension.width - (itemWidth * cols)) / 4.0;
var rMargin = 12; export default class SectionListView1 extends Component { componentDidMount() { } _renderSectionHeader(info) {
return (
<View>
<Text key={info.section.key} style={styles.sectionStyle}>
{info.section.title}
</Text>
</View>
)
} _renderItem(info) { return (
<View style={styles.cellStyle}>
<Image source={{uri: info.item.icon}} style={styles.imageStyle}/>
<Text>
{info.item.name}
</Text>
</View>
)
} _separatorCom() {
return (
<View style={{height: 4, width: 500, backgroundColor: 'orange'}}> </View>
)
} render() {
var dataSource = [];
for (var i = 0; i < Car.data.length; i++) {
let datas = [];
for (var j = 0; j < Car.data[i].cars.length; j++) {
datas.push(Car.data[i].cars[j]);
}
dataSource.push({key: i, data: datas, title: Car.data[i].title});
} return (
<View style={styles.container}>
<SectionList
renderSectionHeader={this._renderSectionHeader}
renderItem={this._renderItem}
sections={dataSource}
// refreshing={false}
// onRefresh={()=>{alert("刷新")}}
// ItemSeparatorComponent={this._separatorCom}
// SectionSeparatorComponent={() => <View style={{height: 20, backgroundColor: 'blue'}}></View>}
keyExtractor={(item, index) => ("index" + index + item)}
// onEndReached={(info)=>{alert("到达底部")}}
// onEndReachedThreshold={0}
// stickySectionHeadersEnabled={true}
ListHeaderComponent={() => <View
style={{backgroundColor: 'yellow', alignItems: 'center',justifyContent: 'center',width:dimension.width,height:50}}><Text>这个是我的表头</Text></View>}
ListFooterComponent={() => <View
style={{backgroundColor: 'red', alignItems: 'center',width:dimension.width}}><Text>这个是我的表尾</Text></View>}
contentContainerStyle={styles.sectionListStyle}//设置cell的样式
pageSize={4} />
</View>
);
}
} const styles = StyleSheet.create({
sectionListStyle: { flexDirection: 'row',
flexWrap: 'wrap',
alignItems: 'flex-start',
backgroundColor: '#dd6ddd',
},
sectionStyle: {
width: dimension.width,
padding: 12,
backgroundColor: '#21c6cd',
color: '#fff'
},
cellStyle: {
alignItems: 'center',
borderRadius: 5,
borderWidth: 1,
borderColor: '#ff496b',
marginLeft: cMargin,
marginTop:rMargin,
marginBottom:rMargin,
padding: 6,
width:itemWidth
},
imageStyle: {
width: 70,
height: 70
}
})
; module.exports = SectionListView1;

如果大家把上面描述的的SectionList的下面两句代码删除,则会出现单行情况,如果有兴趣,自行调试

contentContainerStyle={styles.sectionListStyle}//设置cell的样式
pageSize={4}

renderItem:定义每个元素组件的渲染方式,默认传入参数rowData,要访问其中的"title"可以通过rowData.item.title访问到。
ItemSeparatorComponent:定义每个元素之间分割组件

ListHeaderComponent:定义头部组件
ListFooterComponent:定义底部组件

ListEmptyComponent:data为空时显示的组件

columnWrapperStyle:定义每个组件栏的包裹样式,不支持单行组件
numColumns:number,定义每行显示的元素个数
refreshControl:定义头部刷新组件,例如:
          refreshControl={ //下拉刷新组件
<RefreshControl
refreshing={this.state.refreshing} //通过bool值refreshing控制是否刷新
onRefresh={this._onRefresh.bind(this)} //刷新时需要执行的函数
/>
}
onEndReached:在列表滚动到底部一定距离时调用这个函数,一般在此定义滚动到底部时加载新的数据。
onEndReachedThreshold:决定当距离内容最底部还有多远时触发onEndReached回调。注意此参数是一个比值而非像素单位。比如,0.5表示距离内容最底部的距离为当前列表可见长度的一半时触发。

SectionList的使用的更多相关文章

  1. 史上最易懂——ReactNative分组列表SectionList使用详情及示例详解

    React Native系列 <逻辑性最强的React Native环境搭建与调试> <ReactNative开发工具有这一篇足矣> <解决React Native un ...

  2. React native 中 SectionList用法

    一.代码 import React, { Component } from 'react'; import { AppRegistry, View, Text, SectionList, } from ...

  3. React-Native新列表组件FlatList和SectionList学习 | | 联动列表实现

    React-Native在0.43推出了两款新的列表组件:FlatList(高性能的简单列表组件)和SectionList(高性能的分组列表组件). 从官方上它们都支持常用的以下功能: 完全跨平台. ...

  4. react native 踩坑之 SectionList state更新 不执行render重新渲染页面

    官方文档中指出 SectionList 本组件继承自PureComponent而非通常的Component,这意味着如果其props在浅比较中是相等的,则不会重新渲染.所以请先检查你的renderIt ...

  5. [RN] React Native 下列表 FlatList 和 SectionList

    1.FlatList FlatList组件用于显示一个垂直的滚动列表,其中的元素之间结构近似而仅数据不同. FlatList更适于长列表数据,且元素个数可以增删.和ScrollView不同的是,Fla ...

  6. React Native 之SectionList

    接上一篇: /pages/SectionListDemo.js import React, {Fragment,Component} from 'react'; import { SafeAreaVi ...

  7. SectionIndexer示例

    This small tutorial will show you how to create a ListView, enable fast scrolling, and create a alph ...

  8. Rafy 中的 Linq 查询支持(根据聚合子条件查询聚合父)

    为了提高开发者的易用性,Rafy 领域实体框架在很早开始就已经支持使用 Linq 语法来查询实体了.但是只支持了一些简单的.常用的条件查询,支持的力度很有限.特别是遇到对聚合对象的查询时,就不能再使用 ...

  9. C#读取ini文件的方法

    最近项目用到ini文件,读取ini文件,方法如下: using System; using System.Collections.Generic; using System.Linq; using S ...

随机推荐

  1. [React] 03 - Intro: react.js in twelve demos

    Ref: React 入门实例教程 这算什么,react学习例子的十二门徒?哈哈 如何运行别人的react项目? Ref: [React全家桶入门之CODE]项目代码与使用方法 使用git克隆项目到本 ...

  2. ASP.NET MVC 4 (六) 帮助函数

    帮助函数封装一些代码,方便我们在应用程序中重用,MVC内建很多帮助函数,可以很方便的生成HTML标记.首先列出后面示例中用到的数据模型类定义: namespace HelperMethods.Mode ...

  3. 如何使用点击事件弹出一个url的iframe选项卡

    在一些前后端对接的接口中,前端需要根据后端返回进行跳转,但是有时候需要跳转的地址是不固定的,需要前端灵活的根据接口进行跳转,于是,url被放在接口中返回,而前端想打开一个新窗口的话就会比较麻烦,因为c ...

  4. HTML 选择目录

    <input type="file" webkitdirectory directory multiple/>

  5. Oracle迁移至PostgreSQL工具之Ora2Pg

    1. 描述 Ora2Pg是一个免费的工具,用于将Oracle数据库迁移到PostgreSQL兼容的模式.它连接您的Oracle数据库,自动扫描并提取它的结构或数据,然后生成可以装载到PostgreSQ ...

  6. C++ Release编译时如何对某段代码不进行优化

    optimize#pragma optimize( "[optimization-list]", {on | off} ) Feature Only in Professional ...

  7. 更新docker时间-需要重启docker

    更新docker时间:1.docker run -d -v /etc/localtime:/etc/localtime:ro [IMAGE] 2.重启,docker-compose up -d 3.d ...

  8. ftp如何使用命令上传文件

    本地上传到服务器的步骤如下: 1."开始"-"运行"-输入"FTP" 2.open qint.ithot.net 这一步可以与第一步合并,在 ...

  9. mongodb三种存储引擎高并发更新性能专题测试

    背景说明 近期北京理财频道反馈用来存放股市实时数据的MongoDB数据库写响应请求很慢,难以跟上业务写入速度水平.我们分析了线上现场的情况,发现去年升级到SSD磁盘后,数据持久化的磁盘IO开销已经不是 ...

  10. 洛谷P1182 数列分段【二分】【贪心】

    题目:https://www.luogu.org/problemnew/show/P1182 题意: 有n个数,要分成连续的m段.将每段中的数相加,问之和的最大值的最小值是多少. 思路: 和P1316 ...