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

React Native第五章

好的 👍 我来给你写一个 React Native 示例,完整演示:

  • FlatList(普通列表)

  • SectionList(分组列表)

  • RefreshControl(下拉刷新)

这些是 React Native 常用的列表组件,掌握这三个基本能覆盖 90% 的列表场景。


🧩 一、FlatList + RefreshControl 示例

这是一个最常见的带下拉刷新的普通列表。

import React, { useState, useCallback } from 'react';
import { View, Text, FlatList, RefreshControl, StyleSheet } from 'react-native';const FlatListDemo = () => {const [data, setData] = useState(Array.from({ length: 10 }, (_, i) => `Item ${i + 1}`));const [refreshing, setRefreshing] = useState(false);// 下拉刷新逻辑const onRefresh = useCallback(() => {setRefreshing(true);setTimeout(() => {setData(prev => [`New Item ${Date.now()}`, ...prev]); // 模拟新增数据setRefreshing(false);}, 1500);}, []);// 渲染每一项const renderItem = ({ item }) => (<View style={styles.item}><Text style={styles.itemText}>{item}</Text></View>);return (<FlatListdata={data}renderItem={renderItem}keyExtractor={(item, index) => index.toString()}refreshControl={<RefreshControl refreshing={refreshing} onRefresh={onRefresh} title="正在刷新..." />}ListHeaderComponent={<Text style={styles.header}>FlatList 示例</Text>}/>);
};const styles = StyleSheet.create({header: { fontSize: 18, fontWeight: 'bold', padding: 10, backgroundColor: '#eee' },item: { padding: 15, borderBottomWidth: 1, borderBottomColor: '#ccc' },itemText: { fontSize: 16 },
});export default FlatListDemo;

✅ 关键点说明:

  • data:列表数据

  • renderItem:渲染每个元素

  • keyExtractor:每个元素的唯一 key

  • refreshControl:配合 FlatListScrollView 使用的下拉刷新组件

  • onRefresh:刷新时执行的回调函数

  • refreshing:布尔值,控制刷新动画的显示


🧩 二、SectionList + RefreshControl 示例

SectionList 用于显示 分组列表(如通讯录 A-Z、分类商品等)。

import React, { useState, useCallback } from 'react';
import { View, Text, SectionList, RefreshControl, StyleSheet } from 'react-native';const SectionListDemo = () => {const [refreshing, setRefreshing] = useState(false);const [sections, setSections] = useState([{ title: '🍎 水果', data: ['苹果', '香蕉', '橘子'] },{ title: '🥬 蔬菜', data: ['西红柿', '黄瓜', '生菜'] },]);const onRefresh = useCallback(() => {setRefreshing(true);setTimeout(() => {setSections(prev => [{ title: '🆕 新增分组', data: ['新项目 ' + Date.now()] },...prev,]);setRefreshing(false);}, 1500);}, []);return (<SectionListsections={sections}keyExtractor={(item, index) => item + index}renderItem={({ item }) => (<View style={styles.item}><Text>{item}</Text></View>)}renderSectionHeader={({ section: { title } }) => (<View style={styles.header}><Text style={styles.headerText}>{title}</Text></View>)}refreshControl={<RefreshControl refreshing={refreshing} onRefresh={onRefresh} />}ListHeaderComponent={<Text style={styles.listHeader}>SectionList 示例</Text>}/>);
};const styles = StyleSheet.create({listHeader: { fontSize: 18, fontWeight: 'bold', padding: 10, backgroundColor: '#eee' },header: { backgroundColor: '#f2f2f2', padding: 10 },headerText: { fontWeight: 'bold' },item: { padding: 15, borderBottomWidth: 1, borderBottomColor: '#ddd' },
});export default SectionListDemo;

✅ 关键点说明:

  • sections:是一个数组,每个对象包含 { title, data }

  • renderSectionHeader:渲染分组标题

  • renderItem:渲染每组中的每一项

  • keyExtractor:唯一 key

  • refreshControl:同样支持下拉刷新


🧩 三、RefreshControl 属性介绍

属性类型说明
refreshingboolean是否显示刷新指示器
onRefresh() => void用户下拉时触发的回调
titlestring下拉刷新时显示的文字(iOS)
colorsstring[]Android 刷新指示器颜色数组
progressBackgroundColorstringAndroid 指示器背景色
tintColorstringiOS 刷新指示器颜色
titleColorstringiOS 标题颜色

🧩 四、额外:FlatList 常用属性一览

属性类型说明
dataany[]列表数据
renderItem({item, index}) => ReactNode渲染函数
keyExtractor(item, index) => string唯一 key
ListHeaderComponentReactNode列表头
ListFooterComponentReactNode列表尾
ItemSeparatorComponentReactNode项之间的分隔线
onEndReached() => void滚动到底部时触发(常用于加载更多)
onEndReachedThresholdnumber触发加载更多的阈值(0~1)
refreshControl<RefreshControl>下拉刷新组件

✅ 总结

场景推荐组件
普通列表FlatList
分组/带标题列表SectionList
下拉刷新RefreshControl(与上述任一结合)

http://www.dtcms.com/a/568463.html

相关文章:

  • 如何提高测试用例覆盖率?
  • 蒙古网站群建设html做的网站排版导致乱码
  • 【国产桌面操作系统】QT应用打deb包
  • React 12
  • 做网站 怎么发布专业模板建站哪家好
  • JavaSe—Set集合
  • 单调栈的“近亲”:用 O(n) 的「单调队列」征服「滑动窗口最大值」
  • Buildroot构建Linux系统根文件系统
  • 在自动驾驶数据闭环中的特征工程应用(上)
  • 【具身智能】Spatial Forcing 论文笔记 如何隐式地为 VLA 注入 3D 空间感知能力
  • 多模态技术深度探索:融合视觉与语言的AI新范式
  • 自动化单mysql多实例库的全量迁移脚本-v2版本
  • [CARLA系列--04]如何在Carla中去调用传感器模型--相机篇
  • 【ASP.NET MVC 进阶】DataAnnotations 特性验证全解析:从基础到避坑,让数据校验像 “安检“ 一样靠谱
  • 做ppt兼职的网站有哪些北京中燕建设公司网站
  • webgl 顶点、片元着色器传参,绘制彩色三角形
  • 实验室安全教育与管理平台学习记录(八)特种设备安全
  • 浙江网站制作国外翻墙设计网站
  • 《神经网络与深度学习》学习笔记一
  • 超越蓝牙与Wi-Fi,UWB技术如何解锁手机下一波创新浪潮?
  • 【VPX650G】基于 VPX 系统架构的 JFM9VU13P FPGA+JFMQL100TAI 超宽带信号处理平台
  • 软考 系统架构设计师系列知识点之杂项集萃(190)
  • Linux信号(下):信号保存和信号处理
  • 仅需一部智能手机,在NVIDIA Isaac Sim中重建真实场景:终极实战指南
  • Spring设计模式刨根问底
  • 河南郑州做网站汉狮网站赚钱的方式
  • 不是万维网的网站如何注册公司抖音号
  • AI 赋能科研实践:从选题到发表的技术重构之路
  • 技术的秩序:IT资产与配置管理的现代重构
  • 告别布线噩梦:8公里LoRa边缘网关如何重构工业物联边界