bar*_*tzy 5 react-native react-native-tab-view react-native-reanimated
我正在尝试在 React Native 中创建一个带有 2 个选项卡(使用react-native-tab-view)的屏幕,布局如下:
------------------------
| Collapsible Header |
|------------------------|
| Tab A | Tab B |
|------------------------|
| |
| |
| FlatList |
| in Tab |
| Content |
| |
| |
| |
| |
------------------------
Run Code Online (Sandbox Code Playgroud)
我选择的布局是可折叠标题和选项卡栏的绝对定位,并且 FlatList 实际上覆盖了整个屏幕(标题和选项卡栏都位于其顶部)。为了将可滚动部分放在选项卡栏之后,我paddingTop在contentContainerStyleFlatList 的 中添加了 。这是问题的根源 - 当在选项卡 A 中滚动,然后移动到选项卡 B 时,由于填充,将会出现空白。
我创建了一个完整的 Expo 项目来展示这个问题:https://expo.io/@bartzy/collapsible-tab-view-example 这是 Expo 项目的 Github 存储库:https://github.com/bartzy/CollapsibleTabViewExample
这是示例应用程序的快速视频,显示切换到选项卡 2 后的空白填充空间:

我很感激有关如何在选项卡之间移动时消除空白空间,同时保持所需的折叠行为的任何想法。
这是一个常见问题,关于如何解决这个问题的示例很少,但最近我发布了一个react-native-tab-view 的包装器来解决这个问题。
从快速开始的例子。
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,
},
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5298 次 |
| 最近记录: |