首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >React - Native中的可折叠标题-React Native中具有多个选项卡的重现

React - Native中的可折叠标题-React Native中具有多个选项卡的重现
EN

Stack Overflow用户
提问于 2019-12-23 01:45:14
回答 2查看 2.7K关注 0票数 4

我正尝试在React Native中创建一个屏幕,其中包含2个选项卡(使用react-native-tab-view),布局如下:

代码语言:javascript
运行
复制
 ------------------------
|   Collapsible Header   |
|------------------------|  
|    Tab A  |  Tab B     |
|------------------------| 
|                        |
|                        |
|        FlatList        |
|         in Tab         |
|         Content        |
|                        |
|                        |
|                        |
|                        |
 ------------------------

我采用的布局是可折叠标题和选项卡栏的绝对定位,FlatList实际上覆盖了整个屏幕(标题和选项卡栏都在它的顶部)。为了将可滚动部分放在选项卡栏之后,我在FlatList的contentContainerStyle中添加了一个paddingTop。这就是问题的根源-当在Tab A中滚动,然后移动到Tab B时,会因为填充而出现空白。

我创建了一个完整的世博会项目来展示这个问题:https://expo.io/@bartzy/collapsible-tab-view-example这是世博会项目的Github存储库:https://github.com/bartzy/CollapsibleTabViewExample

以下是示例应用程序的快速视频,显示了切换到Tab 2后的空白填充空间:

我非常感谢任何关于如何消除标签间移动时的空白,同时保持所需的折叠行为的想法。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-11-20 04:30:16

这是一个常见的问题,关于如何解决这个问题的例子很少,但最近我为react-native-tab-view发布了一个包装器来解决这个问题。

演示

示例

来自quick start的示例。

代码语言:javascript
运行
复制
import * as React from 'react';
import { StyleSheet, View, Text, Animated } from 'react-native';
import {
  CollapsibleTabView,
  useCollapsibleScene,
} from 'react-native-collapsible-tab-view';
import { SceneMap } from 'react-native-tab-view';

type Route = {
  key: string;
  title: string;
};

const SomeRoute: React.FC<{ routeKey: string; color: string }> = ({
  routeKey,
  color,
}) => {
  const scrollPropsAndRef = useCollapsibleScene(routeKey);

  return (
    <Animated.ScrollView
      style={{ backgroundColor: color }}
      {...scrollPropsAndRef}
    >
      <View style={styles.content} />
    </Animated.ScrollView>
  );
};

const FirstScene = () => <SomeRoute routeKey="first" color="white" />;
const SecondScene = () => <SomeRoute routeKey="second" color="black" />;

const HEADER_HEIGHT = 250;

const renderHeader = () => (
  <View style={styles.header}>
    <Text style={styles.headerText}>COLLAPSIBLE</Text>
  </View>
);

const renderScene = SceneMap({
  first: FirstScene,
  second: SecondScene,
});

const App: React.FC<object> = () => {
  const [index, setIndex] = React.useState(0);
  const [routes] = React.useState<Route[]>([
    { key: 'first', title: 'First' },
    { key: 'second', title: 'Second' },
  ]);

  const handleIndexChange = (index: number) => {
    setIndex(index);
  };

  return (
    <CollapsibleTabView<Route>
      navigationState={{ index, routes }}
      renderScene={renderScene}
      onIndexChange={handleIndexChange}
      renderHeader={renderHeader} // optional
      headerHeight={HEADER_HEIGHT} // optional, will be computed.
    />
  );
};

export default App;

const styles = StyleSheet.create({
  header: {
    height: HEADER_HEIGHT,
    backgroundColor: '#2196f3',
    justifyContent: 'center',
    alignItems: 'center',
    elevation: 4,
  },
  headerText: {
    color: 'white',
    fontSize: 24,
  },
  content: {
    height: 1500,
  },
});
票数 1
EN

Stack Overflow用户

发布于 2020-06-07 02:34:43

您需要保留对平面列表的引用和未处于焦点的选项卡的scolltoOffset引用。https://gist.github.com/maitham/6e0841800d88bf9c289fc45bbc903b1d。这似乎对我很有效。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59446635

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档