React Native【实战范例】网格导航 FlatList

c 复制代码
import React from "react";
import {
  FlatList,
  Image,
  SafeAreaView,
  StyleSheet,
  Text,
  View,
} from "react-native";
interface GridItem {
  id: string;
  title: string;
  imageUrl: string;
}
// 网格布局数据
const gridData = Array.from({ length: 30 }, (_, i) => ({
  id: `grid-${i}`,
  title: `项目 ${i + 1}`,
  imageUrl: `https://picsum.photos/seed/${i}/200/200`,
}));
const GridFlatList = () => {
  // 渲染网格列表项
  const renderGridItem = ({ item }: { item: GridItem }) => (
    <View style={styles.gridItem}>
      <Image
        source={{ uri: item.imageUrl }}
        style={styles.gridImage}
        resizeMode="cover"
      />
      <Text style={styles.gridTitle}>{item.title}</Text>
    </View>
  );
  return (
    <SafeAreaView style={styles.container}>
      <View style={styles.section}>
        <Text style={styles.sectionTitle}>网格导航</Text>
        <FlatList
          data={gridData}
          renderItem={renderGridItem}
          keyExtractor={(item) => item.id}
          numColumns={3}
          contentContainerStyle={styles.gridContent}
        />
      </View>
    </SafeAreaView>
  );
};
const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: "#f5f5f5",
  },
  section: {
    marginBottom: 20,
  },
  sectionTitle: {
    fontSize: 18,
    fontWeight: "bold",
    padding: 15,
  },
  iconText: {
    fontSize: 24,
    marginBottom: 5,
  },
  gridContent: {
    paddingHorizontal: 5,
  },
  gridItem: {
    width: "33.33%",
    padding: 5,
  },
  gridImage: {
    width: "100%",
    height: 100,
    borderRadius: 8,
  },
  gridTitle: {
    fontSize: 12,
    textAlign: "center",
    marginTop: 5,
  },
});
export default GridFlatList;
相关推荐
大龄秃头程序员17 小时前
CocoaPods 静态库 & 动态库实践笔记:从 Podfile 配置到 RN 新架构的一些思考
react native
光影少年2 天前
RN的Fabric 渲染流程
运维·前端·javascript·react native·react.js·fabric
wordbaby3 天前
App 热更新(OTA)原理深解 —— 以 React Native 为例
前端·react native
光影少年7 天前
RN 的EventEmitter 双向通信
前端·react native·react.js
光影少年8 天前
React Native Module 注册流程
前端·react native·react.js
光影少年11 天前
RN Bridge 原理
前端·react native·react.js
moMo11 天前
用 React 写一个 TodoList
react native·react.js
光影少年12 天前
RN原生交互 & 桥接
前端·javascript·react native·react.js·前端框架
光影少年14 天前
RN 路由栈管理、页面销毁、返回拦截
javascript·react native·react.js