reactjs 将水平滚动视图添加到底部导航选项卡

6ss1mwsb  于 2023-05-22  发布在  React
关注(0)|答案(1)|浏览(122)

早上好/下午好/晚上好
我必须为我的班级的最终项目创建一个应用程序,我决定使用React 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;

尝试创建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>
        );
    }
}
cl25kdpy

cl25kdpy1#

要集成水平滚动,您需要使用HorizontalScrollView组件 Package 各个屏幕。但是如果你需要在不同的屏幕之间滑动,你必须使用不同的导航器,比如“材料顶部标签导航器”(createMaterialTopTabNavigator):在这里你可以把它放到底部。这种方法更容易接受,因为它符合React Native中通常处理屏幕之间导航的方式。

1.安装软件包

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

2.修改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 '组件 Package ,因为' createMaterialTopTabNavigator '处理屏幕之间的滑动功能。“swipeEnabled” prop 使屏幕之间的滑动成为可能,“tabBarPosition='bottom”将标签定位在底部。
希望这能解决你的问题。

相关问题