2016-07-09 32 views
1

我正在使用DrawerLayoutAndroid并努力解决如何编程打开和关闭它。反应本地编程打开DrawerLayoutAndroid

我看你应该使用

openDrawer(0),但你如何引用DrawerLayoutAndroid

我有我做了完全一样的文档抽屉里,然后我有一个按钮我认为,如果你按下它,我希望它打开DrawerLayoutAndroid

我创造了这个功能

toggleDrawer(){ 
    openDrawer(0); 
}; 

但明显犯规工作,只是抛出一个错误。

回答

7

你应该为此使用refs。我在这里粘贴的例子,供大家参考

var DrawerExample = React.createClass({ 

     openDrawer:function() { 
      this.refs['myDrawer'].openDrawer(); 
     }, 

     closeDrawer:function() { 
      this.refs['myDrawer'].closeDrawer(); 
     }, 

     render: function() { 
      var navigationView = (
       <View style={{flex: 1, backgroundColor: '#fff'}}> 
        <Text style={{margin: 10, fontSize: 15, textAlign: 'left'}}>I'm in the Drawer!</Text> 

        <TouchableHighlight onPress={this.closeDrawer}> 
         <Text>{'Close Drawer'}</Text> 
        </TouchableHighlight> 

       </View> 
      ); 
      return (
       <DrawerLayoutAndroid ref="myDrawer" 
        drawerWidth={300} 
        drawerPosition={DrawerLayoutAndroid.positions.Left} 
        renderNavigationView={() => navigationView}> 
        <View style={{flex: 1, alignItems: 'center'}}> 
         <Text style={{margin: 10, fontSize: 15, textAlign: 'right'}}>Hello</Text> 
         <Text style={{margin: 10, fontSize: 15, textAlign: 'right'}}>World!</Text> 

         <TouchableHighlight onPress={this.openDrawer}> 
          <Text>{'Open Drawer'}</Text> 
         </TouchableHighlight> 

        </View> 
       </DrawerLayoutAndroid> 
      ); 
     }, 
    }); 
2

如果你必须从一个组件编程方式打开它(视图,例如),它里面的drawerlayoutandroid,您将需要调用函数openDrawer(),但与“.bind(this)”。否则,即使使用refs,您也无法使其工作,(正如Jagadish写的那样,您仍然需要使用它)。我有这个问题几天,直到我发现我刚写的东西。希望能帮助到你。

+1

谢谢,我已经知道了使用正常功能视图里面,但我会给你一个不管怎么说,因为它可以帮助其他人 –

2
you have to Use 

REF = {_抽屉=>(this.drawer = _drawer)}

import React, { Component } from "react"; 
import { 
    Text, 
    View, 
    DrawerLayoutAndroid, 
    TouchableHighlight 
} from "react-native"; 
export default class DrawerExample extends Component { 

    constructor() { 
    super(); 
    this.openDrawer = this.openDrawer.bind(this); 
    } 

    openDrawer() { 
    this.drawer.openDrawer(); 
    } 

    render() { 
    var navigationView = (
     <View style={{ flex: 1, backgroundColor: "#fff" }}> 
     <Text style={{ margin: 10, fontSize: 15, textAlign: "left" }}> 
      I'm in the Drawer! 
     </Text> 
     </View> 
    ); 
    return (
     <DrawerLayoutAndroid 
     drawerWidth={300} 
     ref={_drawer => (this.drawer = _drawer)} 
     drawerPosition={DrawerLayoutAndroid.positions.Left} 
     renderNavigationView={() => navigationView} 
     > 
     <View style={{ flex: 1, alignItems: "center" }}> 
      <Text style={{ margin: 10, fontSize: 15, textAlign: "right" }}> 
      Hello 
      </Text> 
      <Text style={{ margin: 10, fontSize: 15, textAlign: "right" }}> 
      World! 
      </Text> 
      <TouchableHighlight onPress={this.openDrawer}> 
      <Text>{"Open Drawer"}</Text> 
      </TouchableHighlight> 
     </View> 
     </DrawerLayoutAndroid> 
    ); 
    } 
} 
+1

如果你解释为什么你的代码可以工作会更好。 –

+0

我有使用REF打开抽屉 \t \t REF = {_抽屉=>(this.drawer = _drawer)} openDrawer(){ this.drawer.openDrawer(); } –

相关问题