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;
相关推荐
沙漠14 小时前
ReactNative总结系列二 --- 小工具&小技巧md
react native
humcomm1 天前
FinClip vs React Native:两大跨平台方案的深度对比
javascript·react native·react.js
水云桐程序员1 天前
Flutter与React Native的对比分析
flutter·react native·react.js
墨狂之逸才3 天前
React Native 状态管理大比拼:Event Bus 还是 Context?小白一看就懂!
react native
爱滑雪的码农3 天前
React Native 完整开发全流程(从零到上线)
javascript·react native·react.js
沐言人生3 天前
ReactNative 源码分析12——Native View创建流程onBatchComplete
android·react native
沐言人生5 天前
ReactNative 源码分析11——Native View创建流程setChildren和manageChildren
android·react native
沐言人生6 天前
ReactNative 源码分析10——Native View创建流程createView
android·react native
坏小虎6 天前
【聊天列表组件选型建议】FlashList、FlatList、LegendList三种列表组件
javascript·react native·react.js
sealaugh327 天前
react native(学习笔记第五课) 英语打卡微应用(4)- frontend的列表展示
笔记·学习·react native