安卓端React Navigation的TabNavigator选项卡与react-native-scrollable-tab-view、FlatList一起使用,只显示第一页的内容。

解决方案: 给TabNavigator增加lazy: true 属性,官方解释,whether to lazily render tabs as needed as opposed to rendering them upfront

FlatList 初始化时不显示列表,需要拖拽一下才显示当 React Navigation 与 FlatList 配合使用时,会出现初始化时不显示列表的bug,必须要拖拽一下,才会正常显示

解决方案: 此时需要给FlatList设置removeClippedSubviews={false} 属性,即可解决问题(目前最新版0.46貌似已经解决了此bug),官方文档解释如下:

注意:removeClippedSubviews属性目前是不必要的,而且可能会引起问题。如果你在某些场景碰到内容不渲染的情况(比如使用LayoutAnimation时),尝试设置removeClippedSubviews={false}。我们可能会在将来的版本中修改此属性的默认值。

快速点击重复跳转

react-navigation目录下src/addNavigationHelpers.js

export default function<S: *>(navigation: NavigationProp<S, NavigationAction>) {
// 添加点击判断
let debounce = true;
return {
...navigation,
goBack: (key?: ?string): boolean =>
navigation.dispatch(
NavigationActions.back({
key: key === undefined ? navigation.state.key : key,
}),
),
navigate: (routeName: string,
params?: NavigationParams,
action?: NavigationAction,): boolean => {
if (debounce) {
debounce = false;
navigation.dispatch(
NavigationActions.navigate({
routeName,
params,
action,
}),
);
setTimeout(
() => {
debounce = true;
},
500,
);
return true;
}
return false;
},
/**
* For updating current route params. For example the nav bar title and
* buttons are based on the route params.
* This means `setParams` can be used to update nav bar for example.
*/
setParams: (params: NavigationParams): boolean =>
navigation.dispatch(
NavigationActions.setParams({
params,
key: navigation.state.key,
}),
),
};
}

单页面设置navigationOptions中, 使用this

static navigationOptions = ({navigation, screenProps}) => ({
headerTitle: '登录',
headerLeft:(
<Text onPress={()=>navigation.state.params.navigatePress()} style={{marginLeft:5, width:30, textAlign:"center"}} >
<Icon name='ios-arrow-back'size={24} color='white' />
</Text>
)
}); _onBackAndroid=()=>{
alert('点击headerLeft');
} componentDidMount(){
//在static中使用this方法
this.props.navigation.setParams({ navigatePress:this._onBackAndroid })
}

大多数页面都是push(从右往左),某些页面想从下往上

const TransitionConfiguration = () => ({
screenInterpolator: (sceneProps) => {
const { scene } = sceneProps;
const { route } = scene;
const params = route.params || {};
const transition = params.transition || 'forHorizontal';
return CardStackStyleInterpolator[transition](sceneProps);
},
}); const Navigator = StackNavigator (
{ // Root:{screen: FilterNews},
Root: {screen: Tab}, CompanyDetail: {screen: CompanyDetail},
LawOption: {screen: LawOption},
}, {
transitionConfig: TransitionConfiguration,
initialRouteName: 'Root',
navigationOptions: { //不要在此处设置 否则后面全部被覆盖
headerStyle: [Style.shadow, {backgroundColor: 'white',borderBottomColor:'white'}],
headerBackTitle: null,
headerTintColor: '#192847',// 返回箭头颜色
showIcon: true,
swipeEnabled: true,//是否可以滑动返回
animationEnabled: true,//是否带动画
gesturesEnabled: true,// 是否支持手势返回
headerTitleStyle: {fontSize: 18, color: Color.textBlack, alignSelf:'center'},//定义title的样式
cardStyle: {
backgroundColor: 'transparent',
},
}, // transitionConfig: (): Object => ({
// containerStyle: {
// backgroundColor: 'transparent',
// },
// }),
}
) // 使用的时候 ,参数加一个 transition: 'forVertical'
this.props.navigation.navigate('Login', {transition: 'forVertical', someValue: 0})

从下往上背景色是黑色

react-navigation/src/views/CardStack/TransitionConfigs.js

里 ModalSlideFromBottomIOS -> backgroundColor 改为 ‘#fff’

android 有键盘的时候会把 tabBar 顶起来(tabBar悬浮在键盘上方)

工程目录->android->app->src->main->AndroidManifest.xml-> <activity android:name=".MainActivity"

android:windowSoftInputMode="adjustResize"

替换为

android:windowSoftInputMode="stateAlwaysHidden|adjustPan"

返回指定页面

tabA 跳转到 tabB (tabNavigator, tabA页面内点击某个按钮跳转到tabB)

android 标题不居中

方案一:

const Stack = StackNavigator(
{
Tab: {
screen: Tab,
navigationOptions: ({navigator}) => ({
// drawerLockMode: 'locked-closed',
})
}, First: {screen: First},
},
{
navigationOptions: {
headerBackTitle: null,
headerTitleStyle: {fontSize: Common.headrTitleSize, alignSelf: 'center', textAlign: 'center'},//定义title的样式
headerRight: (
<View />
)
},
}
)

总处设置, 基本 OK

具体页面内设置的navigationOptions 会覆盖上面总处 navigationOptions 的选项

具体页面内 navigationOptions 如果重新设置了 headerTitleStyle, 就需要在具体处headerTitleStyle 添加 alignSelf: 'center'

另外方案一在没有返回按钮的页面中标题可能会偏左,解决方案就是类似,在页面设置 headerLeft 为一个空View

headerLeft: (
<View />
)

方案二:

MyProject/node_modules/react-navigation/src/views/Header/Header.js

Line 392

alignItems: Platform.OS === 'ios' ? 'center' : 'flex-start',

替换为

alignItems: 'center',

Line 196

_renderTitle 方法中注销掉安卓部分的判断,或者把下面整段都注销

if (Platform.OS === 'android') {
// if (!options.hasLeftComponent) {
// style.left = 0;
// }
// if (!options.hasRightComponent) {
// style.right = 0;
// }
}

相对来说方案二更省事,高效,污染少,但是每次 npm install 后都需要重新来一遍

react-navigation的更多相关文章

  1. react-native 学习 ----- React Navigation

    很久没有的登陆博客园了,密码都是找回的,从当年的大学生已经正常的走上了程序员的道路,看到之前发的博客还是写的android,现在自己已经在使用了react-native了. 大学毕业了,做了java后 ...

  2. react-native导航器 react navigation 介绍

    开发环境搭建好之后,想要进一步了解react-native,可以先从react-native官网上的电影列表案例入手: https://reactnative.cn/docs/0.51/sample- ...

  3. React Navigation & React Native & React Native Navigation

    React Navigation & React Native & React Native Navigation React Navigation https://facebook. ...

  4. [RN] 04 - React Navigation

    react-navigation和react-router的对比: 支持的平台: react-navigation: react-native react-router: react-native.r ...

  5. React Native常用组件之TabBarIOS、TabBarIOS.Item组件、Navigator组件、NavigatorIOS组件、React Navigation第三方

    以下内容为老版本React Native,faceBook已经有了新的导航组件,请移步其他博客参考>>[我是传送门] 参考资料:React Navigation  react-native ...

  6. React-native 导航插件React Navigation 4.x的使用

    React-native 导航插件React Navigation 4.x的使用 文档 英文水平可以的话,建议直接阅读英文文档 简单使用介绍 安装插件 yarn add react-navigatio ...

  7. [RN] React Navigation 使用中遇到的显示 问题 汇总

    React Navigation 使用中遇到的显示 问题 汇总 https://www.jianshu.com/p/8b1f18affc5d

  8. react navigation goBack()返回到任意页面(不集成redux) 一

    方案一: 一.适用场景:在app端开发的时候,相反回到某一个页面的时候保持跳转页面的所有状态不更新,也就是说不触发新的生命周期. 例如:A——>B——>C——>D 要想从D页面直接返 ...

  9. React Navigation / React Native Navigation 多种类型的导航结合使用,构造合理回退栈

    React Navigation 更新到版本5已经是非常完善的一套导航管理组件, 提供了Stack , Tab , Drawer 导航方式 , 那么我们应该怎样设计和组合应用他们来构建一个完美的回退栈 ...

  10. React Navigation基本用法

    /** * Created by apple on 2018/9/23. */ import React, { Component } from 'react'; import {AppRegistr ...

随机推荐

  1. asp core 配置用户密码验证

    using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; usi ...

  2. winapi创建不能改变大小的窗口

    HWND hWnd = CreateWindow( "myWindowClass", //窗口类的名字 "my first window", //窗口标题 // ...

  3. 洛谷P2472 [SCOI2007]蜥蜴 题解

    题目链接: https://www.luogu.org/problemnew/show/P2472 分析: 这道题用最大流解决. 首先构建模型. 一根柱子可以跳入和跳出,于是拆成两个点:入点和出点. ...

  4. 网页学习:day1

    初始准备: Write some function Write a titie Write a article Write some button Button function写法: functio ...

  5. 个人永久性免费-Excel催化剂功能第45波-逻辑判断函数增强

    自定义函数的最大的作用是可以按需定制,在Excel的原生函数不提供的场景时,传统方法需要使用大量的嵌套函数去实现,实在太累,今天Excel催化剂再次送上一波绝对十分常用的函数逻辑判断类函数给大家使用! ...

  6. JS浅学

    (变量的名字.focus(); )让打开的新的页面获取焦点 (变量的名字.close();)关闭打开的页面 可以用(!变量名)直接判断是否打开过新的页面 用(变量名.closed)判断是不是被关闭了 ...

  7. Android拨打电话权限总结

    android在6.0和6.0以上拨打电话的权限声明 /** * 打电话 * * @param phoneNumber */ protected void startCallPhone(String ...

  8. [LeetCode] 32. Longest Valid Parentheses (hard)

    原题链接 题意: 寻找配对的(),并且返回最长可成功配对长度. 思路 配对的()必须是连续的,比如()((),最长长度为2:()(),最长长度为4. 解法一 dp: 利用dp记录以s[i]为终点时,最 ...

  9. 如何利用Azure Automation以及Tag自动开关VM

    这是本博客第一篇技术相关的小贴士,在这里我不会详细介绍所涉及的技术组件的具体使用细节,因为我相信这些大家都可以通过官方文档了解到.如果你是一个看了官方文档依然一脸茫然的IT小白,个人建议是先从基础重新 ...

  10. C# 不同访问符的访问级别

    public----成员可以由任何代码访问. private----成员只能由类中的代码访问(如果没有使用任何关键字,就默认使用这个关键字). internal----成员只能由定义它的项目(程序集) ...