React Native【实战范例】水平滚动分类 FlatList

c 复制代码
import React from "react";
import { FlatList, SafeAreaView, StyleSheet, Text, View } from "react-native";
interface itemType {
  id: string;
  title: string;
  icon: string;
}
// 水平滚动数据
const horizontalData: itemType[] = [
  { id: "1", title: "电影", icon: "🎬" },
  { id: "2", title: "音乐", icon: "🎵" },
  { id: "3", title: "书籍", icon: "📚" },
  { id: "4", title: "游戏", icon: "🎮" },
  { id: "5", title: "体育", icon: "⚽" },
  { id: "6", title: "美食", icon: "🍔" },
  { id: "7", title: "旅行", icon: "✈️" },
  { id: "8", title: "科技", icon: "📱" },
];
const HorizontalFlatList = () => {
  // 渲染水平列表项
  const renderHorizontalItem = ({ item }: { item: itemType }) => (
    <View style={styles.horizontalItem}>
      <Text style={styles.iconText}>{item.icon}</Text>
      <Text style={styles.horizontalTitle}>{item.title}</Text>
    </View>
  );
  return (
    <SafeAreaView style={styles.container}>
      <View style={styles.section}>
        <Text style={styles.sectionTitle}>水平滚动分类</Text>
        <FlatList
          data={horizontalData}
          renderItem={renderHorizontalItem}
          keyExtractor={(item) => item.id}
          horizontal
          showsHorizontalScrollIndicator={false}
          contentContainerStyle={styles.horizontalContent}
        />
      </View>
    </SafeAreaView>
  );
};
const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: "#f5f5f5",
  },
  section: {
    marginBottom: 20,
  },
  sectionTitle: {
    fontSize: 18,
    fontWeight: "bold",
    padding: 15,
  },
  horizontalContent: {
    paddingHorizontal: 15,
  },
  horizontalItem: {
    alignItems: "center",
    marginRight: 20,
    padding: 10,
    backgroundColor: "#ffffff",
    borderRadius: 15,
    shadowColor: "#000",
    shadowOffset: { width: 0, height: 2 },
    shadowOpacity: 0.1,
    shadowRadius: 2,
  },
  iconText: {
    fontSize: 24,
    marginBottom: 5,
  },
  horizontalTitle: {
    fontSize: 14,
  },
});
export default HorizontalFlatList;
相关推荐
冰冷的bin7 小时前
【React Native】点赞特效动画组件FlowLikeView
react native·react.js·typescript
懒人村杂货铺14 小时前
[特殊字符] 跨端视频通话实战:腾讯云 TRTC + IM(React Native & Web)
react native·音视频·腾讯云
木西1 天前
React Native DApp 开发全栈实战·从 0 到 1 系列(流动性挖矿-前端部分)
react native·web3·智能合约
小妖怪的夏天2 天前
react native 出现 FATAL EXCEPTION: OkHttp Dispatcher
react native·react.js·okhttp
qczg_wxg2 天前
高阶组件介绍
开发语言·javascript·react native·ecmascript
qczg_wxg4 天前
React Native的动画系统
javascript·react native·react.js
qczg_wxg4 天前
React Native常用的API
react native
Winson℡4 天前
在 React Native 层禁止 iOS 左滑返回(手势返回/手势退出)
react native·react.js·ios
卸任4 天前
从0到1搭建react-native自动更新(OTA和APK下载)
前端·react native·react.js
wayne2144 天前
「原生 + RN 混合工程」一条命令启动全攻略:解密 react-native.config.js
android·react native