反应原生。标签栏不会更改按钮

时间:2017-03-05 08:01:27

标签: javascript ios react-native react-router

我在这个GitHub之后创建了tabbar https://github.com/skv-headless/react-native-scrollable-tab-view

我需要像那样创建tabbar

所以,我试过了。但不适合我。我点击tabbar后,我的场景没有变化。或者我的代码中有一些问题。

任何人都可以给我一些例子吗?关于tabbar的详细信息,我不明白。

谢谢

这是我的代码。

Route.js

import React, { Component, PropTypes } from 'react';
import {
    StyleSheet,
    View,
    Text,
    Image,
    Dimensions,
    TouchableOpacity,
    MapView,
    zoom,
    DeviceEventEmitter
} from 'react-native';
import {Accelerometer, Gyroscope, Magnetometer} from 'NativeModules';
var {height, width} = Dimensions.get('window');
import api from './api';
import Profile from './Profile';
import ScrollableTabView, {DefaultTabBar, ScrollableTabBar } from 'react-native-scrollable-tab-view';
import Info from './Info';
// import motion from './motion';





export default class Route extends Component {
    constructor(props){
    super(props);
    this.state = {
        Accx: 0,
        Accy: 0,
        Accz: 0,
        Gyx: 0,
        Gyy: 0,
        Gyz: 0,
        Magx: 0,
        Magy: 0,
        Magz: 0,
        region: {
            latitude: 13.980881,
            longitude: 100.5545009,
        }
    };

    this.onRegionChange = this.onRegionChange.bind(this);
}

componentDidMount() {
    Accelerometer.setAccelerometerUpdateInterval(0.1); // in seconds
    DeviceEventEmitter.addListener('AccelerationData', (data) => {
        this.setState({
            Accx: data.acceleration.x,
            Accy: data.acceleration.y,
            Accz: data.acceleration.z,
        });
    });
    Accelerometer.startAccelerometerUpdates(); // you'll start getting AccelerationData events above

    Gyroscope.setGyroUpdateInterval(0.1); // in seconds
    DeviceEventEmitter.addListener('GyroData', (data) => {
        this.setState({
            Gyx: data.rotationRate.x,
            Gyy: data.rotationRate.y,
            Gyz: data.rotationRate.z,
        });
    });
    Gyroscope.startGyroUpdates(); // you'll start getting GyroscopicData events above

    Magnetometer.setMagnetometerUpdateInterval(0.1); // in seconds
    DeviceEventEmitter.addListener('MagnetometerData', (data) => {
        this.setState({
            Magx: data.magneticField.x,
            Magy: data.magneticField.y,
            Magz: data.magneticField.z,
        });
    });
    Magnetometer.startMagnetometerUpdates(); // you'll start getting MagnetomerData events above
}

onRegionChange(region) {
    this.setState({ region });
}


    render() {
        return (


            <View style={styles.container}>
            <ScrollableTabView
            style={{marginTop: 20, }}
            initialPage={1}
            renderTabBar={() => <ScrollableTabBar />}
        >
            <Text tabLabel='Profile'></Text>
            <Text tabLabel='Route'></Text>
            <Text tabLabel='Information'></Text>
            </ScrollableTabView>
            <TouchableOpacity tabLabel='Profile' onPress={() => this.tabView.Profile(0)}>
            </TouchableOpacity>
            <TouchableOpacity tabLabel='Route' onPress={() => this.tabView.Route(1)}>
            </TouchableOpacity>
            <TouchableOpacity tabLabel='Information' onPress={() => this.tabView.Route(2)}>
            </TouchableOpacity>

        <MapView style={styles.map}
          mapType="standard"
          showsUserLocation={true}
          followsUserLocation={true}
          showsCompass={false}
          showsPointOfInterest={false}
                    region={this.state.region}
          onRegionChange={this.onRegionChange}
        >

        </MapView>
                <View style={styles.container}>
          <Text>
          Latitude: {this.state.region.latitude}{'\n'}
          Longitude: {this.state.region.longitude}{'\n'}
                    AccX: {this.state.Accx}
                    AccY: {this.state.Accy}
                    AccZ: {this.state.Accz}{'\n'}
                    GyX: {this.state.Gyx}
                    GyY: {this.state.Gyy}
                    GyZ: {this.state.Gyz}{'\n'}
                    MagX: {this.state.Magx}
                    MagY: {this.state.Magy}
                    MagZ: {this.state.Magz}{'\n'}
          </Text>
        </View>
      </View>

        );
    }
}





const styles = StyleSheet.create({
    container: {
        flex: 1,
        alignItems: 'center',
        justifyContent: 'center',
        backgroundColor: 'white'
    },
    image: {
        width: 200,
        height: 180,
    },
    text: {
        color: 'white',
        fontWeight: 'bold',
        backgroundColor: 'transparent',
        marginTop: 20,
    },
    map: {
        //top: -150,
    width: 700,
    height: 400
  }
});

Profile.js

import React, { Component } from 'react';
import {
  StyleSheet,
  Text,
  Image,
  View
} from 'react-native';

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  tabIcon: {
    width: 16,
    height: 16,
  },
});

const Profile = ()  => {
  return (
    <View style={styles.container}>
      <Text style={styles.welcome}>
        THIS IS THE profile SCREEN!
      </Text>
    </View>
  );
}

export default Profile

Information.js

import React, { Component } from 'react';
import {
  StyleSheet,
  Text,
  Image,
  View
} from 'react-native';

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  tabIcon: {
    width: 16,
    height: 16,
  },
});

const Infomation = ()  => {
  return (
    <View style={styles.container}>
      <Text style={styles.welcome}>
        THIS IS THE info SCREEN!
      </Text>
    </View>
  );
}

export default Information

1 个答案:

答案 0 :(得分:2)

我也使用相同的插件,因为你只有3个标签,那么你不需要ScrollableTabBar,你也可以使用DefaultTabBar,因为只有3个标签。以下代码是相同的样本。

<ScrollableTabView renderTabBar={() => <DefaultTabBar /> }>
   <View tabLabel={label1}></View>
   <View tabLabel={label2}></View>
   <View tabLabel={label3}></View>
</ScrollableTabView>

关于代码的更改,我认为你必须在ScrollableTabView中渲染所有Compoenent,如下所示

<ScrollableTabView renderTabBar={() => <DefaultTabBar /> }>
   <View tabLabel={'Profile'}> <Profile/></View>
   <View tabLabel={'Route'}> /*Render Route component directly here */</View>
   <View tabLabel={'Infomation'}><Infomation/></View>
</ScrollableTabView>