React Native:如何从模块调用本机Android布局?

时间:2017-12-13 09:45:59

标签: java android react-native native-module react-native-native-module

关于this question,我一直试图通过Android中的原生模块完成此任务。

我已按照React Native ToastModule上的示例.../java/com/myproject/multiplecamerastream宣布我的模块(这里的功能并不重要):

public class MultipleCameraStreamModule extends ReactContextBaseJavaModule {

  private static final String CAMERA_FRONT = "SHORT";
  private static final String CAMERA_BACK = "LONG";

  public MultipleCameraStreamModule(ReactApplicationContext reactContext) {
    super(reactContext);
  }

  @Override
  public String getName() {
    return "MultipleCameraStream";
  }

  @Override
  public Map<String, Object> getConstants() {
    final Map<String, Object> constants = new HashMap<>();
    constants.put(CAMERA_FRONT, Toast.LENGTH_SHORT);
    constants.put(CAMERA_BACK, Toast.LENGTH_LONG);
    return constants;
  }

  @ReactMethod
  public void show(String message, int duration) {
    Toast.makeText(getReactApplicationContext(), message, duration).show();
  }
}

然后,我的模块打包器:

public class MultipleCameraStreamPackage implements ReactPackage { 

  @Override
  public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
    return Collections.emptyList();
  }

  @Override
  public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
    List<NativeModule> modules = new ArrayList<>();
    modules.add(new MultipleCameraStreamModule(reactContext));
    return modules;
  }
}

我已经能够注册并使其工作。但是,它只调用Android中的Toast(不涉及布局)。

我想设置一个布局,所以当我在JSX中调用<MultipleCameraStream />时,会呈现原生Android布局,如下所示:

/* .../multiplecamerastream/MultipleCameraStreamLayout.xml */
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
>
    <TextView android:id="@+id/multipleCameraText"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Hello, I am a TextView" 
    />
    <Button android:id="@+id/multipleCameraButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello, I am a Button" 
    />
</LinearLayout>

如何从我的模块脚本中调用布局MultipleCameraStreamModule),以及如何引用其元素以便我可以以编程方式从Android 模块端与他们进行交互?

谢谢。

1 个答案:

答案 0 :(得分:4)

RN Native UI网站本身有解释,但我也迷失了。 :(

但接下来,请告诉我是否需要改进:

1)创建一个将使xml MultipleCameraStreamLayout.xml膨胀的视图。理想情况下,此CustomView可用于Android纯代码。

public class CustomView extends LinearLayout {
 private Context context;
 public CustomView(Context context) {
   super(context);//ADD THIS
   this.context = context;
 }
 ..
 public void init() {
   //modified here.
    inflate(context, R.layout.xxxxxxxxx, this);
  ...

2)设置完成后,将其放入另一个扩展SimpleViewManager的类(View Manager)中。样品:

public class MyCustomReactViewManager extends SimpleViewManager<CustomView> {
   public static final String REACT_CLASS = "MyCustomReactViewManager";

   @Override
   public String getName() {
     return REACT_CLASS;
   }

   @Override
   public CustomView createViewInstance(ThemedReactContext context) {
     return new CustomView(context); //If your customview has more constructor parameters pass it from here.
   }

3)现在将它添加到React包createViewManager方法中,您已在MultipleCameraStreamPackage中创建它。所以,它将是:

@Override
public List<ViewManager> createViewManagers(
        ReactApplicationContext reactContext) {
    return Arrays.<ViewManager>asList(
            new MyCustomReactViewManager() //Add here.
    );
}

4)您现在可以使用Android代码重新安装

react-native run-android

5)在javascript中公开它。创建一些文件CustomView.js

import {requireNativeComponent, ViewPropTypes} from 'react-native';
//
module.exports = requireNativeComponent('MyCustomReactViewManager', null); //Add props are bit different.

6)开始在视图中使用它。例如

import CustomView from './CustomView.js';
...
render() {
 return 
  ...
  <CustomView style={{height:200, width:200}}/>
  ...;
}

希望这有帮助。

如果您需要代码示例,请上传here

相关问题