Una*_*dra 5 java android react-native native-module react-native-native-module
关于这个问题,我一直在尝试通过Android中的本机模块来完成。
我已经.../java/com/myproject/multiplecamerastream
按照React Native ToastModule的示例声明了我的模块(此处的功能并不重要):
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();
}
}
Run Code Online (Sandbox Code Playgroud)
然后,我的模块包装器:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
我已经能够注册它并使它工作。但是,它仅在Android中调用Toast(不涉及布局)。
我想设置一个layout,所以当我<MultipleCameraStream />
在JSX中调用时,它将呈现一个原生的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>
Run Code Online (Sandbox Code Playgroud)
我如何才能从我的模块脚本(MultipleCameraStreamModule
)中调用该布局,以及如何引用其元素,以便可以从Android 模块端以编程方式与它们进行交互?
谢谢。
RN Native UI网站本身有解释,但是我也迷失了。:(
但是,让我知道是否需要对此进行改进:
1)创建一个视图,该视图将使您的xml MultipleCameraStreamLayout.xml膨胀。理想情况下,可以在Android纯代码中使用此CustomView。
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);
...
Run Code Online (Sandbox Code Playgroud)
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.
}
Run Code Online (Sandbox Code Playgroud)
3)现在将其添加到React包的createViewManager方法中,您已经在MultipleCameraStreamPackage中创建了它。因此,它将是:
@Override
public List<ViewManager> createViewManagers(
ReactApplicationContext reactContext) {
return Arrays.<ViewManager>asList(
new MyCustomReactViewManager() //Add here.
);
}
Run Code Online (Sandbox Code Playgroud)
4)您现在可以通过重新安装使用Android代码
react-native run-android
Run Code Online (Sandbox Code Playgroud)
5)用javascript公开它。创建一些文件CustomView.js
import {requireNativeComponent, ViewPropTypes} from 'react-native';
//
module.exports = requireNativeComponent('MyCustomReactViewManager', null); //Add props are bit different.
Run Code Online (Sandbox Code Playgroud)
6)开始在您的视图中使用它。例如。
import CustomView from './CustomView.js';
...
render() {
return
...
<CustomView style={{height:200, width:200}}/>
...;
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助。
如果您需要代码示例,请在此处上传。
归档时间: |
|
查看次数: |
4248 次 |
最近记录: |