如下图所示:

代码如下:

import React, { useRef, Component } from 'react';
import {
Platform,
Text,
View,
TextInput,
TouchableOpacity,
ScrollView,
Image,
Button,
SectionList,
StyleSheet,
ToastAndroid,
Dimensions,
Alert,
} from 'react-native'; import Icon from 'react-native-vector-icons/Ionicons';
   const DATA = [
    {
titleId: "1",
titleName: "水果",
data: [
{ id: '01', name: '香蕉', selected: false },
{ id: '02', name: '梨', selected: false },
{ id: '03', name: '葡萄', selected: false },
{ id: '04', name: '猕猴桃', selected: false },
{ id: '05', name: '苹果', selected: false },
{ id: '06', name: '桃子', selected: false },
{ id: '07', name: '西瓜', selected: false },
{ id: '08', name: '橘子', selected: false },
]
},
{
titleId: "2",
titleName: "菜品",
data: [
{ id: '09', name: '辣椒', selected: false },
{ id: '10', name: '白菜', selected: false },
{ id: '11', name: '青菜', selected: false },
{ id: '12', name: '茄子', selected: false },
{ id: '13', name: '南瓜', selected: false },
{ id: '14', name: '土豆', selected: false },
{ id: '15', name: '西红柿', selected: false },
{ id: '16', name: '粉条', selected: false },
{ id: '17', name: '豇豆', selected: false },
{ id: '18', name: '牛肉', selected: false },
{ id: '19', name: '猪肉', selected: false },
{ id: '20', name: '鸡翅', selected: false },
{ id: '21', name: '鸡爪', selected: false },
{ id: '22', name: '鸭肉', selected: false }, ]
}, ]; export default class TestScreen extends Component {
constructor(props) {
super(props);
this.state = {
sourceData: DATA,
selectedItem: []//选中的项
}
}; render() {return (
<View style={styles.container}><SectionList
sections={this.state.sourceData}
keyExtractor={(item, index) => index.toString()}
extraData={this.state}
stickySectionHeadersEnabled={true}//吸顶效果
renderItem={this._renderItem} //cell
renderSectionHeader={({ section: { titleName } }) => (
<View style={{ height: 40, justifyContent: 'center', backgroundColor: 'rgba(232,240,248,1)' }}>
<Text style={[, { color: "#0a3989", textAlign: 'center', fontSize: CommonVar.userStyle.titleFontSize + 2 }]}>{titleName}</Text>
</View>
)}
ItemSeparatorComponent={() => {
return <View style={{ borderWidth: 0.2, borderColor: "#d2d2d2" }} />
}}
/>
</View>
)
} _renderItem = (info) => {
// console.log(info);
if (info.item.selected == true) {
return <TouchableOpacity onPress={this._itemPress.bind(this, info.item, info.index)}>
<View style={{ height: 45, flexDirection: 'row', justifyContent: 'space-between', backgroundColor: '#FFFFFF' }}>
<Text style={{ marginLeft: 10, alignSelf: 'center', color: "#000000" }}>{info.item.name}</Text>
<Icon name="ios-checkmark-outline" color='blue' size={25} style={{ alignSelf: 'center', marginRight: 5 }} />
</View>
</TouchableOpacity>
} else {
return <TouchableOpacity onPress={this._itemPress.bind(this, info.item, info.index)}>
<View style={{ height: 45, flexDirection: 'row', justifyContent: 'space-between', backgroundColor: '#FFFFFF' }}>
<Text style={{ marginLeft: 10, alignSelf: 'center', color: "#000000" }}>{info.item.name}</Text>
<Icon name="ios-square-outline" color='#d2d2d2' size={25} style={{ alignSelf: 'center', marginRight: 5 }} />
</View>
</TouchableOpacity>
}
} _itemPress(selectItem, index) {
var $this = this;
this.state.sourceData.forEach(function (item1, lev1Index) {
item1.data.forEach(function (item2, lev2Index) {
if (item2.id == selectItem.id) {
//循环数据是否存在,存在就移除
var isExist = false;
$this.state.selectedItem.forEach(function (obj, objIndex) {
if (obj.id == selectItem.id && obj.titleId == item1.titleId) {
//找到存在的对象删除掉
$this.state.selectedItem.splice(objIndex, 1);
isExist = true;
}
})
if (isExist == false) {
//不存在就加到集合中去
$this.state.selectedItem.push({ id: selectItem.id, titleId: item1.titleId });
}
$this.state.sourceData[lev1Index].data[index].selected = !selectItem.selected;
}
})
})
console.log(this.state.selectedItem)
this.setState({ sourceData: this.state.sourceData })
}
} const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#FFFFFF"
}, })

react native SectionList组件实现多选的更多相关文章

  1. 封装 React Native 原生组件(iOS / Android)

    封装 React Native 原生组件(iOS / Android) 在 React Native中,有很多种丰富的组件了,例如 ScrollView.FlatList.SectionList.Bu ...

  2. React Native 之 组件化开发

    前言 学习本系列内容需要具备一定 HTML 开发基础,没有基础的朋友可以先转至 HTML快速入门(一) 学习 本人接触 React Native 时间并不是特别长,所以对其中的内容和性质了解可能会有所 ...

  3. React Native的组件ListView

    React Native的组件ListView类似于iOS中的UITableView和UICollectionView,也就是说React Native的组件ListView既可以实现UITableV ...

  4. React Native交互组件之Touchable

    React Native交互组件之Touchable:只要在组件外面包一个Touchable组件就可以实现点击交互. TouchableHighlight:高亮触摸 当点击时,组件的透明度会改变,可以 ...

  5. React Native常用组件在Android和IOS上的不同

    React Native常用组件在Android和IOS上的不同 一.Text组件在两个平台上的不同表现 1.1 height与fontSize 1.1.1只指定font,不指定height 在这种情 ...

  6. react native 常用组件汇总

    react-native-uploader //文件上传https://github.com/aroth/react-native-uploader jpush-react-native //官方版本 ...

  7. React Native常用组件Image使用

    前言 学习本系列内容需要具备一定 HTML 开发基础,没有基础的朋友可以先转至 HTML快速入门(一) 学习 本人接触 React Native 时间并不是特别长,所以对其中的内容和性质了解可能会有所 ...

  8. React Native 中组件的生命周期

    概述 就像 Android 开发中的 View 一样,React Native(RN) 中的组件也有生命周期(Lifecycle).所谓生命周期,就是一个对象从开始生成到最后消亡所经历的状态,理解生命 ...

  9. React Native 获取组件(Component)在屏幕上的位置

    年后主客户端的需求以及老的业务迁移RN,现在疯狂的在学RN.在迁移需求的时候遇到需要获取组件在屏幕上的绝对位置.页面如下: 就需要展开的时候获取sectionHeader(默认排序)在屏幕上的具体位置 ...

  10. React Native常用组件之ListView

    1. ListView常用属性 ScrollView 相关属性样式全部继承 dataSource ListViewDataSource 设置ListView的数据源 initialListSize n ...

随机推荐

  1. 从Babel开始认识AST抽象语法树

    前言 AST抽象语法树想必大家都有听过这个概念,但是不是只停留在听过这个层面呢.其实它对于编程来讲是一个非常重要的概念,当然也包括前端,在很多地方都能看见AST抽象语法树的影子,其中不乏有vue.re ...

  2. LibreOJ 144. DFS 序 1

    题面 给一棵有根树,这棵树由编号为 \(1\dots N\) 的 \(N\) 个结点组成.根结点的编号为 \(R\).每个结点都有一个权值,结点 \(i\) 的权值为 \(v_i\). 接下来有 \( ...

  3. 如何在 Nuxt 3 中使用 wavesurfer.js

    安装 wavesurfer.js 在项目中安装 wavesurfer.js npm install --save wavesurfer.js 常规方式引入 如果你的根目录中没有 components ...

  4. MySQL性能优化浅析及线上案例

    作者:京东健康 孟飞 1. 数据库性能优化的意义 业务发展初期,数据库中量一般都不高,也不太容易出一些性能问题或者出的问题也不大,但是当数据库的量级达到一定规模之后,如果缺失有效的预警.监控.处理等手 ...

  5. 记一下Go类型转换问题

    数值类型间可以相互转换 int<->int64,uint8<->float32,uint64<->float64 字符类型转换也可以 string<-> ...

  6. 系列化和反序列化的概述-对象的序列化_Object Output Stream类

    系列化和反序列化的概述 Java提供了一种对象序列化的机制.用一个字节序列可以表示一个对象,该字节序列包含该对象的数据对象的类型和对象中存储的属性等信息.字节序列写出到文件之后,相当于文件中持久保存了 ...

  7. 增加for循环-泛型的概念

    增加for循环 增强for循环(也称for each循环)是JDK1.5以后出来的一个高级for循环,专门用来遍历数组和集合的.它的内部原理其实是个lterator迭代器,所以在遍历的过程中,不能对集 ...

  8. Sundial (二)

    相关重要的组件一览 Triggers(触发器)相关类 保存触发器相关参数,例如起止时间,次数,间隔时间等,其中Sundial支持多种类型触发器 多种类型的触发器必须重写GetNextOccurrenc ...

  9. Spring异步Async和事务Transactional注解

    Spring开发中我们我们常常用到@Transaction和@Async,但这2个注解加在一起很多的开发者不敢用,担心事务不生效.下面我们就仔细讲解一下这2个注解同时运用,文章用3个场景讲述它们之间的 ...

  10. C#依赖注入(直白明了)讲解 一看就会系列

    最基础的:UI-BLL-DAL 这是我们耳熟能详的分层 (补充:)  我们的类正常都不是孤立存在的.很多都是要依赖于其它的类. 比如说我们有一个Work类,Work类在工作的时候需要把信息记录下来. ...