react-navigation 監聽頂部導航欄點擊/滑動狀態

問題描述:react

使用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>
    }
}
相關文章
相關標籤/搜索