当前位置: 首页 > news >正文

詳細講一下RN(React Native)中的列表組件FlatList和SectionList

1. FlatList 基礎使用

import React from 'react';
import { View, Text, FlatList, StyleSheet } from 'react-native';export const SimpleListDemo: React.FC = () => {// 1. 準備數據const data = [{ id: '1', title: '項目 1' },{ id: '2', title: '項目 2' },{ id: '3', title: '項目 3' },];// 2. 定義如何渲染每一項const renderItem = ({ item }) => (<View style={styles.item}><Text>{item.title}</Text></View>);// 3. 渲染 FlatListreturn (<FlatListdata={data}                    // 數據源renderItem={renderItem}        // 渲染項keyExtractor={item => item.id} // 指定 key/>);
};const styles = StyleSheet.create({item: {padding: 20,borderBottomWidth: 1,borderBottomColor: '#ccc',},
});

2. 添加頭部和底部

import React from 'react';
import { View, Text, FlatList, StyleSheet } from 'react-native';export const ListWithHeaderFooter: React.FC = () => {const data = [/* ... */];// 1. 定義頭部組件const ListHeader = () => (<View style={styles.header}><Text>這是列表頭部</Text></View>);// 2. 定義底部組件const ListFooter = () => (<View style={styles.footer}><Text>這是列表底部</Text></View>);return (<FlatListdata={data}renderItem={({ item }) => (<View style={styles.item}><Text>{item.title}</Text></View>)}ListHeaderComponent={ListHeader}   // 添加頭部ListFooterComponent={ListFooter}   // 添加底部keyExtractor={item => item.id}/>);
};const styles = StyleSheet.create({header: {padding: 15,backgroundColor: '#f0f0f0',},item: {padding: 20,borderBottomWidth: 1,borderBottomColor: '#ccc',},footer: {padding: 15,backgroundColor: '#f0f0f0',},
});

3. 下拉刷新和上拉加載

import React, { useState } from 'react';
import { View, Text, FlatList, RefreshControl, ActivityIndicator, StyleSheet 
} from 'react-native';export const RefreshLoadMoreList: React.FC = () => {const [refreshing, setRefreshing] = useState(false);const [loading, setLoading] = useState(false);const [data, setData] = useState([/* 初始數據 */]);// 1. 處理下拉刷新const onRefresh = async () => {setRefreshing(true);try {// 這裡請求新數據const newData = await fetchNewData();setData(newData);} finally {setRefreshing(false);}};// 2. 處理上拉加載更多const onLoadMore = async () => {if (loading) return;setLoading(true);try {// 這裡請求更多數據const moreData = await fetchMoreData();setData([...data, ...moreData]);} finally {setLoading(false);}};return (<FlatListdata={data}renderItem={({ item }) => (<View style={styles.item}><Text>{item.title}</Text></View>)}// 下拉刷新配置refreshControl={<RefreshControlrefreshing={refreshing}onRefresh={onRefresh}/>}// 上拉加載配置onEndReached={onLoadMore}onEndReachedThreshold={0.1}ListFooterComponent={loading ? <ActivityIndicator /> : null}/>);
};

4. 常用配置項說明

<FlatList// 基礎配置data={data}                          // 列表數據renderItem={renderItem}              // 渲染每一項的方法keyExtractor={item => item.id}       // 生成 key 的方法// 樣式相關contentContainerStyle={styles.list}  // 內容容器樣式style={styles.container}             // FlatList 本身樣式// 性能優化initialNumToRender={10}              // 首次渲染的項目數maxToRenderPerBatch={10}             // 每次渲染的最大數量windowSize={5}                       // 渲染窗口大小// 滾動相關showsVerticalScrollIndicator={false} // 是否顯示滾動條scrollEnabled={true}                 // 是否可以滾動
/>

5. 空列表處理

import React from 'react';
import { View, Text, FlatList, StyleSheet } from 'react-native';export const EmptyList: React.FC = () => {const data = [];  // 空數據const EmptyComponent = () => (<View style={styles.empty}><Text>暫無數據</Text></View>);return (<FlatListdata={data}renderItem={({ item }) => (/* ... */)}ListEmptyComponent={EmptyComponent}  // 當數據為空時顯示/>);
};const styles = StyleSheet.create({empty: {flex: 1,justifyContent: 'center',alignItems: 'center',padding: 20,},
});

2. SessionList基礎使用

import React from 'react';
import { View, Text, SectionList, StyleSheet } from 'react-native';export const SimpleSectionList: React.FC = () => {// 1. 準備分組數據const sections = [{title: '分組A',data: [{ id: '1', name: '項目A1' },{ id: '2', name: '項目A2' },]},{title: '分組B',data: [{ id: '3', name: '項目B1' },{ id: '4', name: '項目B2' },]}];// 2. 渲染每個項目const renderItem = ({ item }) => (<View style={styles.item}><Text>{item.name}</Text></View>);// 3. 渲染分組標題const renderSectionHeader = ({ section }) => (<View style={styles.header}><Text style={styles.headerText}>{section.title}</Text></View>);return (<SectionListsections={sections}                     // 分組數據renderItem={renderItem}                 // 渲染每個項目renderSectionHeader={renderSectionHeader} // 渲染分組標題keyExtractor={item => item.id}          // key提取器/>);
}const styles = StyleSheet.create({item: {padding: 15,backgroundColor: 'white',},header: {padding: 10,backgroundColor: '#f0f0f0',},headerText: {fontSize: 16,fontWeight: 'bold',},
});

2. 添加分組間距和分隔線

import React from 'react';
import { View, SectionList, StyleSheet } from 'react-native';export const SectionListWithSeparators: React.FC = () => {const sections = [/* ... */];// 1. 項目之間的分隔線const ItemSeparator = () => (<View style={styles.itemSeparator} />);// 2. 分組之間的間距const SectionSeparator = () => (<View style={styles.sectionSeparator} />);return (<SectionListsections={sections}renderItem={renderItem}renderSectionHeader={renderSectionHeader}ItemSeparatorComponent={ItemSeparator}     // 項目分隔線SectionSeparatorComponent={SectionSeparator} // 分組分隔線stickySectionHeadersEnabled={true}         // 分組標題固定/>);
};const styles = StyleSheet.create({itemSeparator: {height: 1,backgroundColor: '#eee',},sectionSeparator: {height: 10,backgroundColor: '#f5f5f5',},
});

3. 下拉刷新和加載更多

import React, { useState } from 'react';
import { SectionList, RefreshControl, ActivityIndicator 
} from 'react-native';export const RefreshableSectionList: React.FC = () => {const [refreshing, setRefreshing] = useState(false);const [loading, setLoading] = useState(false);const [sections, setSections] = useState([/* 初始數據 */]);// 1. 處理下拉刷新const onRefresh = async () => {setRefreshing(true);try {const newData = await fetchNewData();setSections(newData);} finally {setRefreshing(false);}};// 2. 處理加載更多const onLoadMore = async () => {if (loading) return;setLoading(true);try {const moreData = await fetchMoreData();setSections([...sections, ...moreData]);} finally {setLoading(false);}};return (<SectionListsections={sections}renderItem={renderItem}renderSectionHeader={renderSectionHeader}// 下拉刷新refreshControl={<RefreshControlrefreshing={refreshing}onRefresh={onRefresh}/>}// 加載更多onEndReached={onLoadMore}onEndReachedThreshold={0.2}ListFooterComponent={loading ? <ActivityIndicator /> : null}/>);
};

4.空列表和列表頭尾

import React from 'react';
import { View, Text, SectionList } from 'react-native';export const SectionListWithHeaderFooter: React.FC = () => {const sections = [/* ... */];// 1. 列表頭部const ListHeader = () => (<View style={styles.listHeader}><Text>列表頭部</Text></View>);// 2. 列表底部const ListFooter = () => (<View style={styles.listFooter}><Text>列表底部</Text></View>);// 3. 空列表顯示const ListEmpty = () => (<View style={styles.empty}><Text>暫無數據</Text></View>);return (<SectionListsections={sections}renderItem={renderItem}renderSectionHeader={renderSectionHeader}ListHeaderComponent={ListHeader}ListFooterComponent={ListFooter}ListEmptyComponent={ListEmpty}/>);
};

5. 常用配置項總結

<SectionList// 基礎配置sections={sections}                        // 分組數據renderItem={renderItem}                    // 渲染項目renderSectionHeader={renderSectionHeader}  // 渲染分組標題keyExtractor={(item) => item.id}          // key提取器// 分組相關stickySectionHeadersEnabled={true}        // 分組標題是否固定renderSectionFooter={renderSectionFooter}  // 渲染分組底部// 分隔線ItemSeparatorComponent={ItemSeparator}     // 項目分隔線SectionSeparatorComponent={SectionSeparator} // 分組分隔線// 性能優化initialNumToRender={10}                    // 初始渲染數量maxToRenderPerBatch={10}                   // 每批渲染數量windowSize={5}                             // 渲染窗口大小// 樣式相關contentContainerStyle={styles.container}   // 內容容器樣式style={styles.list}                        // 列表樣式
/>

相关文章:

詳細講一下RN(React Native)中的列表組件FlatList和SectionList

1. FlatList 基礎使用 import React from react; import { View, Text, FlatList, StyleSheet } from react-native;export const SimpleListDemo: React.FC () > {// 1. 準備數據const data [{ id: 1, title: 項目 1 },{ id: 2, title: 項目 2 },{ id: 3, title: 項目 3…...

TDengine 与上海电气工业互联网平台完成兼容性认证

在工业数字化转型和智能化升级的浪潮中&#xff0c;企业对高效、可靠的数据管理解决方案的需求日益增长。特别是在风电智能运维、火电远程运维、机床售后服务等复杂多样的工业场景下&#xff0c;如何实现海量设备和时序数据的高效管理&#xff0c;已经成为推动行业升级的关键。…...

随机矩阵投影长度保持引理及其证明

原论文中的引理 2 \textbf{2} 2 1. \textbf{1. } 1. 引理 1 \textbf{1} 1(前提之一) 1.1. \textbf{1.1. } 1.1. 引理 1 \textbf{1} 1的内容 &#x1f449;前提&#xff1a; X ∼ N ( 0 , σ ) X\sim{}N(0,\sigma) X∼N(0,σ)即 f ( x ) 1 2 π σ e – x 2 2 σ 2 f(x)\text{}…...

深度学习利用数据加载、预处理和增强数据提高模型的性能

深度学习数据预处理是一个关键步骤&#xff0c;旨在提高模型的性能和准确性。 通过数据加载、预处理和增强&#xff0c;可以显著提高深度学习模型的性能和准确性。在实际应用中&#xff0c;需要根据具体的数据和任务来选择合适的预处理和增强技术。 以下将详细论述并举例说明如…...

ESP32服务器和PC客户端的Wi-Fi通信

ESP32客户端-服务器Wi-Fi通信 本指南将向您展示如何设置ESP32板作为服务端&#xff0c;PC作为客户端&#xff0c;通过HTTP通信&#xff0c;以通过Wi-Fi&#xff08;无需路由器或互联网连接&#xff09;交换数据。简而言之&#xff0c;您将学习如何使用HTTP请求将一个板的数据发…...

新型人工智能“黑帽”工具:GhostGPT带来的威胁与挑战

生成式人工智能的发展既带来了有益的生产力转型机会&#xff0c;也提供了被恶意利用的机会。 最近&#xff0c;Abnormal Security的研究人员发现了一个专门为网络犯罪创建的无审查AI聊天机器人——GhostGPT&#xff0c;是人工智能用于非法活动的新前沿&#xff0c;可以被用于网…...

Spring MVC (三) —— 实战演练

项目设计 我们会将前端的代码放入 static 包下&#xff1a; 高内聚&#xff0c;低耦合 这是我们在实现项目的设计思想&#xff0c;一个项目里存在很多个模块&#xff0c;每一个模块内部的要求类与类、方法与方法要相互配合紧密联系&#xff0c;这就是高内聚&#xff0c;低耦合…...

媒体新闻发稿要求有哪些?什么类型的稿件更好通过?

为了保证推送信息的内容质量&#xff0c;大型新闻媒体的审稿要求一向较为严格。尤其在商业推广的过程中&#xff0c;不少企业的宣传稿很难发布在这些大型新闻媒体平台上。 媒体新闻发稿要求有哪些&#xff1f;就让我们来了解下哪几类稿件更容易过审。 一、媒体新闻发稿要求有哪…...

【游戏设计原理】82 - 巴斯特原则

巴斯特原则的核心是“对你的玩家好一点”&#xff0c;这一点直击游戏设计的核心——玩家体验。 现代游戏设计不仅要注重挑战性&#xff0c;还要关注玩家的情绪波动与行为反应。当玩家因为过高的难度感到挫败甚至愤怒时&#xff0c;他们往往选择退出游戏&#xff0c;而不是迎接…...

DDD架构实战第六讲总结:领域驱动设计中的聚合

云架构师系列课程之DDD架构实战第六讲总结:领域驱动设计中的聚合 聚合提升了对象系统的粒度,保证了业务逻辑的完整性,减少了错误产生的概率 一、引言 本讲将探讨领域驱动设计(DDD)中的重要概念——聚合。聚合是业务完整性的单元,是一个更大力度的封装。在领域驱动设计中…...

vim如何设置自动缩进

:set autoindent 设置自动缩进 :set noautoindent 取消自动缩进 &#xff08;vim如何使设置自动缩进永久生效&#xff1a;vim如何使相关设置永久生效-CSDN博客&#xff09;...

C++入门14——set与map的使用

在本专栏的往期文章中&#xff0c;我们已经学习了STL的部分容器&#xff0c;如vector、list、stack、queue等&#xff0c;这些容器统称为序列式容器&#xff0c;因为其底层是线性序列的数据结构&#xff0c;里面存储的是元素本身。而本篇文章我们要来认识一下关联式容器。 &am…...

单片机内存管理剖析

一、概述 在单片机系统中&#xff0c;内存资源通常是有限的&#xff0c;因此高效的内存管理至关重要。合理地分配和使用内存可以提高系统的性能和稳定性&#xff0c;避免内存泄漏和碎片化问题。单片机的内存主要包括程序存储器&#xff08;如 Flash&#xff09;和数据存储器&a…...

【gopher的java学习笔记】Java中Service与Mapper的关系详解

在后端开发中&#xff0c;Java作为一种广泛使用的编程语言&#xff0c;其架构设计和层次划分对于系统的可维护性、可扩展性和性能有着至关重要的影响。特别是在使用MyBatis等持久层框架时&#xff0c;Service层与Mapper层的关系更是值得深入探讨。本文将从Java Web应用程序的角…...

2025美赛B题完整代码+建模过程

问题一 为朱诺市建立一个可持续旅游产业模型。具体要求包括考虑游客数量、总收入,以及为稳定旅游业而实施的措施,明确优化因素和约束条件,并制定额外收入的支出计划,展示这些支出如何反馈到模型中以促进可持续旅游业发展,同时进行敏感性分析,讨论哪些因素最为重要。 为了…...

【MySQL】我在广州学Mysql 系列——MySQL用户管理详解

ℹ️大家好&#xff0c;我是练小杰&#xff0c;本博客是春节前最后一篇了&#xff0c;在此感谢大佬们今年的支持&#xff01;&#xff01;&#x1f64f;&#x1f64f; 接下来将学习MYSQL用户管理的相关概念以及命令~~ 回顾&#xff1a;&#x1f449;【MYSQL触发器的使用】 数据…...

Linux-rt下卡死之hrtimer分析

Linux-rt下卡死之hrtimer分析 日志 超时读过程分析 #define readl_poll_timeout(addr, val, cond, delay_us, timeout_us) \readx_poll_timeout(readl, addr, val, cond, delay_us, timeout_us)34 #define readx_poll_timeout(op, addr, val, cond, sleep_us, timeout_us) \…...

【AI日记】25.01.24

【AI论文解读】【AI知识点】【AI小项目】【AI战略思考】【AI日记】【读书与思考】 AI kaggle 比赛&#xff1a;Forecasting Sticker Sales 读书 书名&#xff1a;法治的细节作者&#xff1a;罗翔 律己 AI&#xff1a;8 小时&#xff0c;良作息&#xff1a;00:30-8:30&…...

React 中hooks之useSyncExternalStore使用总结

1. 基本概念 useSyncExternalStore 是 React 18 引入的一个 Hook&#xff0c;用于订阅外部数据源&#xff0c;确保在并发渲染下数据的一致性。它主要用于&#xff1a; 订阅浏览器 API&#xff08;如 window.width&#xff09;订阅第三方状态管理库订阅任何外部数据源 1.1 基…...

C++11新特性之decltype

1.decltype的作用 decltype是C11新增的一个关键字&#xff0c;与auto的功能一样&#xff0c;都是在编译期间推导变量类型的。不了解auto的可以转到——C11新特性之auto。 为什么引入decltype&#xff1f;看过上边那篇博客的读者应该知道auto在有些场景中并不适用,所以引入declt…...

线程与协程

1. 线程与协程 1.1. “函数调用级别”的切换、上下文切换 1. 函数调用级别的切换 “函数调用级别的切换”是指&#xff1a;像函数调用/返回一样轻量地完成任务切换。 举例说明&#xff1a; 当你在程序中写一个函数调用&#xff1a; funcA() 然后 funcA 执行完后返回&…...

【解密LSTM、GRU如何解决传统RNN梯度消失问题】

解密LSTM与GRU&#xff1a;如何让RNN变得更聪明&#xff1f; 在深度学习的世界里&#xff0c;循环神经网络&#xff08;RNN&#xff09;以其卓越的序列数据处理能力广泛应用于自然语言处理、时间序列预测等领域。然而&#xff0c;传统RNN存在的一个严重问题——梯度消失&#…...

测试markdown--肇兴

day1&#xff1a; 1、去程&#xff1a;7:04 --11:32高铁 高铁右转上售票大厅2楼&#xff0c;穿过候车厅下一楼&#xff0c;上大巴车 &#xffe5;10/人 **2、到达&#xff1a;**12点多到达寨子&#xff0c;买门票&#xff0c;美团/抖音&#xff1a;&#xffe5;78人 3、中饭&a…...

Python实现prophet 理论及参数优化

文章目录 Prophet理论及模型参数介绍Python代码完整实现prophet 添加外部数据进行模型优化 之前初步学习prophet的时候&#xff0c;写过一篇简单实现&#xff0c;后期随着对该模型的深入研究&#xff0c;本次记录涉及到prophet 的公式以及参数调优&#xff0c;从公式可以更直观…...

涂鸦T5AI手搓语音、emoji、otto机器人从入门到实战

“&#x1f916;手搓TuyaAI语音指令 &#x1f60d;秒变表情包大师&#xff0c;让萌系Otto机器人&#x1f525;玩出智能新花样&#xff01;开整&#xff01;” &#x1f916; Otto机器人 → 直接点明主体 手搓TuyaAI语音 → 强调 自主编程/自定义 语音控制&#xff08;TuyaAI…...

《基于Apache Flink的流处理》笔记

思维导图 1-3 章 4-7章 8-11 章 参考资料 源码&#xff1a; https://github.com/streaming-with-flink 博客 https://flink.apache.org/bloghttps://www.ververica.com/blog 聚会及会议 https://flink-forward.orghttps://www.meetup.com/topics/apache-flink https://n…...

数据库分批入库

今天在工作中&#xff0c;遇到一个问题&#xff0c;就是分批查询的时候&#xff0c;由于批次过大导致出现了一些问题&#xff0c;一下是问题描述和解决方案&#xff1a; 示例&#xff1a; // 假设已有数据列表 dataList 和 PreparedStatement pstmt int batchSize 1000; // …...

QT: `long long` 类型转换为 `QString` 2025.6.5

在 Qt 中&#xff0c;将 long long 类型转换为 QString 可以通过以下两种常用方法实现&#xff1a; 方法 1&#xff1a;使用 QString::number() 直接调用 QString 的静态方法 number()&#xff0c;将数值转换为字符串&#xff1a; long long value 1234567890123456789LL; …...

vulnyx Blogger writeup

信息收集 arp-scan nmap 获取userFlag 上web看看 一个默认的页面&#xff0c;gobuster扫一下目录 可以看到扫出的目录中得到了一个有价值的目录/wordpress&#xff0c;说明目标所使用的cms是wordpress&#xff0c;访问http://192.168.43.213/wordpress/然后查看源码能看到 这…...

LOOI机器人的技术实现解析:从手势识别到边缘检测

LOOI机器人作为一款创新的AI硬件产品&#xff0c;通过将智能手机转变为具有情感交互能力的桌面机器人&#xff0c;展示了前沿AI技术与传统硬件设计的完美结合。作为AI与玩具领域的专家&#xff0c;我将全面解析LOOI的技术实现架构&#xff0c;特别是其手势识别、物体识别和环境…...