Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有没有办法让react原生android应用程序中的视图组件(由JS编写)更新?_Android_React Native - Fatal编程技术网

有没有办法让react原生android应用程序中的视图组件(由JS编写)更新?

有没有办法让react原生android应用程序中的视图组件(由JS编写)更新?,android,react-native,Android,React Native,我正在使用react native构建一个android应用程序,我想知道如何解决这种情况:在Java代码中,一些更新必须通知javascript刷新视图 是的,这是可能的。此页面上有更多信息: 您需要使用发射器在JavaScript中发回事件信号 实际上,我还没有在实践中做到这一点,但我很快就会做到。让我知道你进展如何 要使用他们的示例,请在Java中: ... private void sendEvent(ReactContext reactContext,

我正在使用react native构建一个android应用程序,我想知道如何解决这种情况:在Java代码中,一些更新必须通知javascript刷新视图

是的,这是可能的。此页面上有更多信息:

您需要使用发射器在JavaScript中发回事件信号

实际上,我还没有在实践中做到这一点,但我很快就会做到。让我知道你进展如何

要使用他们的示例,请在Java中:

...
private void sendEvent(ReactContext reactContext,
                   String eventName,
                   @Nullable WritableMap params) {
  reactContext
  .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
  .emit(eventName, params);
}
...
WritableMap params = Arguments.createMap();
...
sendEvent(reactContext, "keyboardWillShow", params); 
然后在JS中:

var { DeviceEventEmitter } = require('react-native');
...
var ScrollResponderMixin = {
  mixins: [Subscribable.Mixin],

  componentWillMount: function() {
  ...
  this.addListenerOn(DeviceEventEmitter,
                   'keyboardWillShow',
                   this.scrollResponderKeyboardWillShow);
  ...
  },
  scrollResponderKeyboardWillShow:function(e: Event) {
  this.keyboardWillOpenTo = e;
  this.props.onKeyboardWillShow && this.props.onKeyboardWillShow(e);
},
...
componentWillMount: function() {
     DeviceEventEmitter.addListener('keyboardWillShow', function(e: Event) {
      // handle event.
 });
}
...
或者,您可以直接在JS中侦听事件:

var { DeviceEventEmitter } = require('react-native');
...
var ScrollResponderMixin = {
  mixins: [Subscribable.Mixin],

  componentWillMount: function() {
  ...
  this.addListenerOn(DeviceEventEmitter,
                   'keyboardWillShow',
                   this.scrollResponderKeyboardWillShow);
  ...
  },
  scrollResponderKeyboardWillShow:function(e: Event) {
  this.keyboardWillOpenTo = e;
  this.props.onKeyboardWillShow && this.props.onKeyboardWillShow(e);
},
...
componentWillMount: function() {
     DeviceEventEmitter.addListener('keyboardWillShow', function(e: Event) {
      // handle event.
 });
}
...