react native SectionList组件实现多选
如下图所示:

代码如下:
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';
{
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组件实现多选的更多相关文章
- 封装 React Native 原生组件(iOS / Android)
封装 React Native 原生组件(iOS / Android) 在 React Native中,有很多种丰富的组件了,例如 ScrollView.FlatList.SectionList.Bu ...
- React Native 之 组件化开发
前言 学习本系列内容需要具备一定 HTML 开发基础,没有基础的朋友可以先转至 HTML快速入门(一) 学习 本人接触 React Native 时间并不是特别长,所以对其中的内容和性质了解可能会有所 ...
- React Native的组件ListView
React Native的组件ListView类似于iOS中的UITableView和UICollectionView,也就是说React Native的组件ListView既可以实现UITableV ...
- React Native交互组件之Touchable
React Native交互组件之Touchable:只要在组件外面包一个Touchable组件就可以实现点击交互. TouchableHighlight:高亮触摸 当点击时,组件的透明度会改变,可以 ...
- React Native常用组件在Android和IOS上的不同
React Native常用组件在Android和IOS上的不同 一.Text组件在两个平台上的不同表现 1.1 height与fontSize 1.1.1只指定font,不指定height 在这种情 ...
- react native 常用组件汇总
react-native-uploader //文件上传https://github.com/aroth/react-native-uploader jpush-react-native //官方版本 ...
- React Native常用组件Image使用
前言 学习本系列内容需要具备一定 HTML 开发基础,没有基础的朋友可以先转至 HTML快速入门(一) 学习 本人接触 React Native 时间并不是特别长,所以对其中的内容和性质了解可能会有所 ...
- React Native 中组件的生命周期
概述 就像 Android 开发中的 View 一样,React Native(RN) 中的组件也有生命周期(Lifecycle).所谓生命周期,就是一个对象从开始生成到最后消亡所经历的状态,理解生命 ...
- React Native 获取组件(Component)在屏幕上的位置
年后主客户端的需求以及老的业务迁移RN,现在疯狂的在学RN.在迁移需求的时候遇到需要获取组件在屏幕上的绝对位置.页面如下: 就需要展开的时候获取sectionHeader(默认排序)在屏幕上的具体位置 ...
- React Native常用组件之ListView
1. ListView常用属性 ScrollView 相关属性样式全部继承 dataSource ListViewDataSource 设置ListView的数据源 initialListSize n ...
随机推荐
- TiDB 底层存储结构 LSM 树原理介绍
作者:京东物流 刘家存 随着数据量的增大,传统关系型数据库越来越不能满足对于海量数据存储的需求.对于分布式关系型数据库,我们了解其底层存储结构是非常重要的.本文将介绍下分布式关系型数据库 TiDB 所 ...
- test20230109考试总结-2023寒搜索专题
前言 2023 年的第一篇考试总结-- 赛时得分情况: A B C D E F G \(\texttt{Total}\) \(\texttt{Rank}\) \(40\) \(80\) \(0\) \ ...
- ApiView/Request类源码分析/序列化器
内容概要 ApiView+JsonResponse编写接口 ApiView+Response编写接口 ApiView源码解析 Request对象源码分析 序列化器介绍和快速使用/反序列化 反序列化的校 ...
- 编程思想转换-Lambda表达式
编程思想转换 做什么,而不是怎么做 我们真的希望创建一个匿名内部类对象吗?不.我们只是为了做这件事情而不得不创建一个对象.我们真正希望做的事情是︰将run方法体内的代码传递给 Thread类知晓. 传 ...
- 行为型模式 - 解释器模式Interpreter
学习而来,代码是自己敲的.也有些自己的理解在里边,有问题希望大家指出. 模式的定义与特点 解释器模式(Interperter Pattern),给定一个语言,定义它的文法表示,并定义一个解释器,这个解 ...
- mysql查询逗号,分隔的多个id连表查询
先来几个SQL看看效果 SELECT i.*,n.*FROM iLEFT JOIN n on FIND_IN_SET(n.id, i.n_id)GROUP BY n.id效果图: 想把信息都放一行里面 ...
- Longbow.Tasks
Longbow.Tasks 概述 大体分为了Scheduler(调度任务),Storage(持久化),Trigger(触发器),Task(任务)和逻辑模块,大体流程为通过逻辑代码进行实例化相关类,根据 ...
- Android第六次作业
图片一 用内部存储实现文件写入和读取功能 <?xml version="1.0" encoding="utf-8"?> <LinearLayo ...
- 笔记:C#Datatable 根据某字段数量 自动复制该行的数量
/// <summary> /// 根据Datatable某字段数量自动复制该行查询 /// </summary> /// <param name="dt&qu ...
- 图书管理员(NOIP 2017 PJT2)
0.题目 1.输入 输入 n,q: 输入图书,存入vector string a[20]数组,a[i][j],其中i表示图书编号的位数 2.查询操作 2.1 每输入一个读者需求 存入 int t; s ...