ruq*_*qqq 10 android react-native
我已经尝试过构建原生的ui组件,它经常受到欢迎.该组件似乎没有以react-native呈现.
虽然,对于这个问题,我特意尝试在反应原生中呈现Android片段,但没有任何东西出现.示例代码:
public class PreferenceViewManager extends SimpleViewManager<FrameLayout> {
public static final String REACT_CLASS = "RCTPreferenceView";
private WeakReference<Activity> activityWeakReference;
public PreferenceViewManager(WeakReference<Activity> activityWeakReference) {
this.activityWeakReference = activityWeakReference;
}
@Override
public String getName() {
return REACT_CLASS;
}
@Override
public FrameLayout createViewInstance(ThemedReactContext context) {
Log.d(REACT_CLASS, "PreferenceView createViewInstance");
final FrameLayout view = new FrameLayout(context);
view.setId(View.generateViewId());
// Testing if the view does get rendered, it should show white even if fragment is not rendered!
view.setBackgroundColor(Color.WHITE);
// not sure where to call fragment beginTransaction properly, the code below is just for testing
view.postDelayed(new Runnable() {
@Override
public void run() {
onAfterUpdateTransaction(view);
}
}, 2000);
return view;
}
@Override
public void onAfterUpdateTransaction(FrameLayout view) {
super.onAfterUpdateTransaction(view);
activityWeakReference.get().getFragmentManager().beginTransaction().replace(view.getId(), new PreferenceView()).commit();
Log.d(REACT_CLASS, "PreferenceView Commit"); // for debug
}
}
Run Code Online (Sandbox Code Playgroud)
使用上面的视图管理器,视图所在的位置甚至不是白色,表示FrameLayout本身不会被渲染.
我究竟做错了什么?
实际上,您几乎走对了方向。您的 FrameLayout 未呈现的原因是您没有在 ReactNative 中明确添加样式以指定其高度和宽度。但是,即便如此,您的片段也不会被渲染。您可以执行一些更改来渲染片段。
在您的 ViewManager 中,执行以下几项更改:
@Override
public FrameLayout createViewInstance(ThemedReactContext context) {
final FrameLayout view = new FrameLayout(context);
MyFragment fragment = MyFragment.newInstance();
// Add the fragment into the FrameLayout
reactContext.getCurrentActivity().getFragmentManager()
.beginTransaction()
.add(fragment, "My_TAG")
.commit();
// Execute the commit immediately or can use commitNow() instead
reactContext.getCurrentActivity().getFragmentManager().executePendingTransactions();
// This step is needed to in order for ReactNative to render your view
addView(fragment.getView(), LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
return view;
}
Run Code Online (Sandbox Code Playgroud)
然后在你的 ReactNative 组件中
class ExampleComponent extends React.Component {
render() {
return (
<View>
<Text>Hello</Text>
<RCTPreferenceView
{...this.props}
style={{ height: "80%", width: "100%" }}
/>
</View>
);
}
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助那些试图在 ReactNative 视图中渲染片段的人
| 归档时间: |
|
| 查看次数: |
2322 次 |
| 最近记录: |