Hon*_*fus 1 react-native react-native-android
我有一个Android类,它扩展了SimpleViewManager类并封装了一个MapView.从React Native JavaScript类设置道具就像一个魅力(使用@ReactProp注释).但是,我希望能够在视图上调用方法,如下所示:
public void centerToMyLocation(MapView view) {
view.getController().setCenter(myLocation);
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试使用@ReactMethod注释,获取MapViewJavaScript中的参考,然后centerToMyLocation在该对象上调用该方法.但它不起作用(我得到mapViewRef.centerToMyLocation is not a function错误).
如何从我呈现本机MapView组件的JavaScript类中调用该方法?
好的,以下对我有用:
ref的render方法中添加到本机组件.findNodeHandle方法react-native,将获取ref的参数作为参数传递.该findNodeHandle方法返回一个反应ID,保存它.UIManager.dispatchViewManagerCommand方法from react-native,将react ID作为第一个参数传递.第二个参数是命令ID.第三个参数是附加参数(可以null).receiveCommand方法ViewManager,该方法公开组件.在此方法中,处理该命令.示例JS:
componentDidMount() {
this.mapViewHandle = findNodeHandle(this.mapViewRef);
}
center() {
UIManager.dispatchViewManagerCommand(this.mapViewHandle, 0, null);
}
render() {
return (
<MapView
ref={(mv) => this.mapViewRef = mv} />
);
}
Run Code Online (Sandbox Code Playgroud)
示例Java:
@Override
public void receiveCommand(MapView view, int commandId, @Nullable ReadableArray args) {
super.receiveCommand(view, commandId, args);
if (commandId == 0) {
if (mMyLocation != null) {
view.getController().setCenter(mMyLocation);
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3319 次 |
| 最近记录: |