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;
相关推荐
彧翎Pro9 小时前
跨平台开发新选择:Flutter与React Native深度对比
flutter·react native·react.js
F2E_Zhangmo1 天前
react native如何发送蓝牙命令
javascript·react native·react.js
光影少年1 天前
RN中如何处理权限申请(相机、相册、定位、存储)?使用第三方库还是原生封装?
react native·react.js·掘金·金石计划
光影少年1 天前
Android和iOS原生开发的基础知识对RN开发的重要性,RN打包发布时原生端需要做哪些配置?
android·前端·react native·react.js·ios
wayne2143 天前
React Native 0.85 — 新动画后端 & Jest 预设独立包
react native
Joyee6913 天前
RN 的新渲染器 Fabric
前端·react native
光影少年3 天前
RN长列表(FlatList)性能优化的具体手段有哪些?
react native·react.js·性能优化
Embrace9244 天前
React Native + Realm 离线方案处理
javascript·react native·react.js·realm