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

Rn使用FlatList导航栏自动回到中间

在这里插入图片描述

import { useState, useRef } from 'react'
import { FlatList, View, Text, StyleSheet, TouchableOpacity } from 'react-native'const Center = () => {const tabs = ["语文", "数学", "英语", "政治", "历史", "地理"]const [active, setActive] = useState(0)const flatRef = useRef()const activeTab = (index) => {setActive(index)flatRef.current.scrollToIndex({ viewPosition: 0.5, index: index, animated: true })}return (<><View style={styles.nav}><FlatList data={tabs} ref={flatRef} horizontal showsHorizontalScrollIndicator={false}renderItem={({ item, index }) => (<TouchableOpacity key={index} onPress={() => activeTab(index)}style={[styles.nav_item,active == index && styles.nav_item_active,tabs.length - 1 == index && styles.nav_item_last]}><Text style={active == index && styles.nav_item_active_text}>{item}</Text></TouchableOpacity>)}/></View></>)
}const styles = StyleSheet.create({nav: {height: 45,flexDirection: "row",alignItems: "center",borderBottomColor: "#eee",borderBottomWidth: 1,paddingHorizontal: 10},nav_item: {paddingHorizontal: 20,paddingVertical: 4,borderColor: "#DADCE2",borderWidth: 1,borderRadius: 20,alignItems: "center",justifyContent: "center",marginRight: 10},nav_item_last: {marginRight: 0},nav_item_active: {backgroundColor: "#2C72FF",borderColor: "transparent",},nav_item_active_text: {color: "#ffffff"}
})export default Center
http://www.lryc.cn/news/188984.html

相关文章:

  • 单例模式中的线程安全问题
  • Android 13 骁龙相机点击拍照流程分析(一)——点击拍照到更新到左下角缩略图
  • Docker 的网络与数据管理
  • 在QGIS中给矢量数据属性编号的一种方法
  • 对一个变速器原理的分析
  • 秒验:可以自定义UI的一键登录服务
  • pmm最新版本v2.40.0尝鲜体验
  • 2023年中国数据存储市场现状及发展前景预测分析
  • xlsx冻结单元格
  • yolov8剪枝实践
  • 功能基础篇6——系统接口,操作系统与解释器系统
  • 由于导线材质不同绕组直流电阻不平衡率超标
  • 选择智慧公厕解决方案,开创智慧城市公共厕所新时代
  • FFmpeg 基础模块:AVIO、AVDictionary 与 AVOption
  • 代数——第3章——向量空间
  • 2023年软考网工上半年下午真题
  • Flutter 直接调用so动态库,或调用C/C++源文件内函数
  • elasticsearch(ES)分布式搜索引擎03——(RestClient查询文档,ES旅游案例实战)
  • 198、RabbitMQ 的核心概念 及 工作机制概述; Exchange 类型 及 该类型对应的路由规则
  • 系统架构设计:18 论基于DSSA的软件架构设计与应用
  • Android原生实现控件outline方案(API28及以上)
  • ROS学习笔记(六)---服务通信机制
  • 常见的C/C++开源QP问题求解器
  • 前端axios发送请求,在请求头添加参数
  • CTF Misc(3)流量分析基础以及原理
  • Telink泰凌微TLSR8258蓝牙开发笔记(二)
  • vue3+elementPlus:el-tree复制粘贴数据功能,并且有弹窗组件
  • JTS:10 Crosses
  • MySQL中的SHOW FULL PROCESSLIST命令
  • VsCode 常见的配置、常用好用插件