React Native中制作滑动导航的最佳方法

3
我正在尝试在react-native中重新创建这个应用程序:

enter image description here

但我不知道如何制作红色组件。 我在网上搜索并找到了react-navigation V5,但为了做这个似乎有些复杂。

我还考虑创建一个水平的ScrollView,每次点击新的“页面”时,重渲染所有当前视图。但这是一个好方法吗?

谢谢回答。

2个回答

5

我喜欢@mainak的建议,使用react-native-tab-view

为了补充那个答案,我做了一个简单的实现,可以实现你想要的功能。

首先安装库:yarn add react-native-tab-viewnpm i react-native-tab-view --save

然后你可以像这样做:

// ...
import {TabView, SceneMap, TabBar} from 'react-native-tab-view';
// ...

const initialLayout = {width: Dimensions.get('window').width};

const renderTabBar = (props) => (
  <TabBar
    {...props}
    tabStyle={{width: 120}}
    scrollEnabled={true}
    indicatorStyle={{backgroundColor: 'white', height: 5, borderRadius: 10}}
  />
);

const App = () => {
  // ...
  const [index, setIndex] = React.useState(0);
  const [routes] = React.useState([
    {key: 'first', title: 'First'},
    {key: 'second', title: 'Second'},
    {key: 'third', title: 'Third'},
    {key: 'fourth', title: 'Fourth'},
    {key: 'fifth', title: 'Fifth'},
  ]);

  const renderScene = SceneMap({
    first: FirstRoute,
    second: SecondRoute,
    third: ThirdRoute,
    fourth: FourthRoute,
    fifth: FifthRoute,
  });

  return (
    <TabView
      navigationState={{index, routes}}
      renderTabBar={renderTabBar}
      renderScene={renderScene}
      onIndexChange={setIndex}
      initialLayout={initialLayout}
    />
  );
};


const styles = StyleSheet.create({
  scene: {
    flex: 1,
  },
});

我已经定义了名为FirstRouteSecondRoute等的视图组件...但是您可以将其更改为要在选项卡栏中拥有的屏幕。

这里的关键部分是renderTabBar,它是传递给TabView组件的自定义TabBar组件。在TabBar组件上,我们有一个称为scrollEnabled的选项,如果设置为true,则允许我们根据选项卡视图和选项卡的大小从左到右滚动栏。

我已经为每个选项卡定义了120的宽度,但是您可能希望根据选项卡标签的大小来调整此值。

您提到的活动选项卡下面的红色突出显示称为指示符,可以使用indicatorStyle属性进行样式设置:

indicatorStyle={{backgroundColor: 'white', height: 5, borderRadius: 10}}

更多信息请查看GitHub页面上的文档:https://github.com/satya164/react-native-tab-view

2
你可以试试使用react-native-tab-view。我曾经用过它来完成类似的工作。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接