我必须为我班上的期末项目创建一个App,我决定使用Reaction Native来完成.但我才刚刚开始.我从一段YouTube视频中得到了这段代码,但看看其他代码片段,我想知道这一段代码对于它应该做的事情来说是不是太复杂了.

我看到了实现底部导航栏的其他方法,它允许在我的应用程序的所有页面之间添加水平滚动视图

所以我在想,我应该完全改变我的代码,或者有什么方法可以给它添加水平滚动视图吗?

预先感谢您的回答

文件:MainContainer.js

import * as React from 'react';

import { NavigationContainer } from '@react-navigation/native';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import Ionicons from 'react-native-vector-icons/Ionicons'

//Screens
import WelcomeScreen from './screens/Welcome'
import InventoryScreen from './screens/Inventory'
import PatientsScreen from './screens/Patients'

// Screen names
const welcomeName = 'Bienvenue'
const inventoryName = 'Inventaire'
const patientsName = 'Patients'

const Tab = createBottomTabNavigator();

export default function MainContainer(){
    return(
      <NavigationContainer>
        <Tab.Navigator 
        initialRouteName={welcomeName}
        screenOptions={({route}) => ({
            tabBarIcon: ({focused, color, size}) => {
                let iconName;
                let rn = route.name;

                if(rn === welcomeName){
                    iconName = focused ? 'home' : 'home-outline'
                } else if (rn === inventoryName){
                    iconName = focused ? 'list' : 'list-outline'
                } else if (rn === patientsName){
                    iconName = focused ? 'people-circle' : 'people-circle-outline'
                }

                return <Ionicons name={iconName} size={size} color={color}/>
            },
        })}
        tabBarOptions={{
            activeTintColor: 'tomato',
            inactiveTintColor: 'grey',
            labelStyle: { paddingBottom : 10, fontSize : 10},
            style: {padding: 10, height: 70},
        }}
        
        >

        <Tab.Screen name={welcomeName} component={WelcomeScreen}/>
        <Tab.Screen name={patientsName} component={PatientsScreen}/>
        <Tab.Screen name={inventoryName} component={InventoryScreen}/>

        </Tab.Navigator>
      </NavigationContainer>
    )
}

文件:App.js

import * as React from 'react';
import MainContainer from './navigation/MainContainer';

function App(){
  return(
    <MainContainer></MainContainer>
  )
}

export default App;

try 创建HorizontalScrollView.js文件,但不知道是否可以在MainContainer.js或App.js文件中实现它

import React, { Component } from 'react';
import {
    AppRegistry,
    ScrollView,
    Text, View,
    Dimensions } from 'react-native';

export default class HorizontalScrollView extends Component {
    render(){
        let screenWidth = Dimensions.get('window').width;
        let screenHeight = Dimensions.get('window').height;
        return(
            <ScrollView
                horizontal={true}
            >
                // That's where i should add the views of each page
            </ScrollView>
        );
    }
}

推荐答案

要集成水平滚动,您需要用HorizontalScrollView组件来包装各个屏幕.但如果你需要在不同的屏幕之间滑动,你必须使用不同的导航器,比如‘material 顶部选项卡导航器’(createMaterialTopTabNavigator):在这里你可以将它定位到底部.这种方法更容易被接受,因为它与Reaction Native中通常处理屏幕间导航的方式一致.

1. Install Package

npm install @react-navigation/material-top-tabs react-native-tab-view
npm install react-native-pager-view

2. Modify MainContainer.js

您可以使用createMaterialTopTabNavigator修改MainContainer以支持导航功能.

import * as React from 'react';
import { createMaterialTopTabNavigator } from '@react-navigation/material-top-tabs';
import { NavigationContainer } from '@react-navigation/native';
import Ionicons from 'react-native-vector-icons/Ionicons';

//Screens
import WelcomeScreen from './screens/Welcome'
import InventoryScreen from './screens/Inventory'
import PatientsScreen from './screens/Patients'

// Screen names
const welcomeName = 'Bienvenue'
const inventoryName = 'Inventaire'
const patientsName = 'Patients'

const Tab = createMaterialTopTabNavigator();

export default function MainContainer(){
    return(
        <NavigationContainer>
        <Tab.Navigator 
            initialRouteName={welcomeName}
            tabBarPosition='bottom' // Position your tab bar to the bottom
            swipeEnabled={true} // Enable swipe functionality
            screenOptions={({route}) => ({
                tabBarIcon: ({focused, color, size}) => {
                    let iconName;
                    let rn = route.name;

                    if(rn === welcomeName){
                        iconName = focused ? 'home' : 'home-outline'
                    } else if (rn === inventoryName){
                        iconName = focused ? 'list' : 'list-outline'
                    } else if (rn === patientsName){
                        iconName = focused ? 'people-circle' : 'people-circle-outline'
                    }

                    return <Ionicons name={iconName} size={size} color={color}/>
                },
            })}
            tabBarOptions={{
                activeTintColor: 'tomato',
                inactiveTintColor: 'grey',
                labelStyle: { paddingBottom : 10, fontSize : 10},
                style: {padding: 10, height: 70},
                showIcon: true, // Show icons
                showLabel: false // If you want to hide labels and only show icons
            }}>

            <Tab.Screen name={welcomeName} component={WelcomeScreen}/>
            <Tab.Screen name={patientsName} component={PatientsScreen}/>
            <Tab.Screen name={inventoryName} component={InventoryScreen}/>
        </Tab.Navigator>
      </NavigationContainer>
    );
}

在这里,各个屏幕不需要用‘HorizontalScrollView’组件包装,因为‘createMaterialTopTabNavigator’处理屏幕之间的滑动功能."swipeEnabled"props 使用户可以在屏幕之间滑动,而"tabBarPosition='bottom"props 则将标签置于底部.

希望这能解决你的问题.

Reactjs相关问答推荐

如何在react组件中使用formik进行验证

包装组件可以避免子组件重新渲染.?

在Next.js中实现动态更改的服务器组件

XChaCha20-Poly1305的解密函数

错误./src/TopScroll.js 31:21-31导出';(作为';随路由导入)在';Reaction-Router-Dom';中找不到

我如何在不被 destruct 的情况下将导航栏固定在顶部?

shadcn输入+窗体+Zod;一个部件正在改变要被控制的不受控制的输入;

如何在一个 Next.js 项目中使用两种布局?

React useEffect 依赖项

获取更多数据后更新 DOM,而不刷新 React.js 中的页面

基于 React/NextJS Timer 的文本淡入/淡出不起作用

使用 nextjs App Router 在动态客户端进行服务器端渲染

为什么刷新页面时我的localStorage PET值变成空字符串?

状态链接未传递到路由页面

React CSSTransition 两次创建新页面并在这两个相同的页面上运行转换

如何在react 中更新子组件中的变量?

从 API 中提取映射数组后出现重复元素

如何使用 cypress 获取 MUI TextField 的输入值?

Lodash 在命名导入中导入整个包

material uireact ,如何从表格中删除阴影?试图找到 api 但找不到正确的属性