react-navigation 监听顶部导航栏点击/滑动状态

发布时间:2019-06-22 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了react-navigation 监听顶部导航栏点击/滑动状态脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

问题描述:

使用createMATErialTopTabNavigator创建顶部导航栏,希望实现切换到指定的Tab再获取数据,查看官方文档只能找到tabBarOnPress 方法监听点击回调,但是无法监听滑动切换
import React From 'react';
import {DeviceEventEmitter,View} from 'react-native';
import { createMaterialTopTabNavigator } from 'react-navigation';

export default class testPage extends React.Component{
    constructor(props) {
            super(props);
            this.topTabList = ['All','Java', 'Javascript', 'PHP'];
           
    }
    _getTopBar() {
            let topBars = {}
            this.topTabList.foreach((item, index) => {
                topBars['TopBar_' + item] = {
                    screen: (props)=><ChildComponent {...props} tabName={item} InitTabName={this.topTabList[0]}/>,
                    navigationOptions: {
                        title: item,
                    }
                }
            })
            return topBars
    }
    getTopTabList(){
            if(!this.createTopNavigator){
                this.createTopNavigator = createMaterialTopTabNavigator(
                    this._getTopBar(),
                    {
                          //code...
                    }
                );
            }
            return this.createTopNavigator;
    }
        

    render(){
        const TopTabList = this.getTopTabList();
        // 在导航栏组件render位置使用onNavigationStateChange方法监听顶部导航切换-可监听滑动+点击
        return <View>
            <TopTabList
                onNavigationStateChange={(prevState, currentState)=>{
                     let {index} = currentState;
                     let TabName = currentState.routes[index].routeName;
                     TabName = TabName.split('_')[1];
                    //触发事件监听器,更新列表数据
                    //tip:如果希望切换已经加载过一次之后不重新获取数据,可以通过setParams设一个flag,判断flag是否需要触发监听器
                     DeviceEventEmitter.emit('TabChange',TabName);  
                }}
            />
        </View>
    }
}


class ChildComponent extends React.Component{
    constructor(props){
        super(props);
        this.tabName= this.props.tabName; //当前tabName
        this.InitTabName = this.props.InitTabName; //初始化列表
    }
    componentWillMount(){
         // 加载初始化Tab列表
            if(this.StoreName===this.InitTabName){
                this.updateList();
            }
        // 监听Tab切换
            this.TopBarChangeListener = DeviceEventEmitter.addListener('TabChange',tabName=>{
                if(this.tabName===tabName){
                    //***更新列表***
                    this.updateList();
                }
            })
    }
    // 更新列表
    updateList(){
        let {navigation} = this.props;
        navigation.setParams({hasList:true});
        this.loadData(); 
    }
    loadData(){
        //***Send Request***
    }
    componentWillUnmount(){
        //移除事件监听器
        if(this.TopBarChangeListener){
                this.TopBarChangeListener.remove();
            }
        }
    }
    render(){
        return <View>
            {/* code... */}
        </View>
    }
}


脚本宝典总结

以上是脚本宝典为你收集整理的react-navigation 监听顶部导航栏点击/滑动状态全部内容,希望文章能够帮你解决react-navigation 监听顶部导航栏点击/滑动状态所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。