在React Native应用程序中加载片段 [英] Load fragment in a React Native app
问题描述
我正在开发我的Android库和ReactNative之间的桥梁,以便能够在混合应用程序上使用我的库.
I'm developing a bridge between an Android library of mine and ReactNative, to be able to use my library also on hybrid apps.
我首先使用react-native init
创建了一个基本应用,该应用具有默认的Android和iOS文件夹
I first created a basic app with react-native init
which has the default Android and iOS folders
然后我用gradle
导入了我的库,并创建了扩展ReactContextBaseJavaModule
和ReactPackage
的文件,以确保App.js
可以从我的库中看到并调用方法,但是现在我遇到了问题.
Then I imported my library with gradle
and created the files extending ReactContextBaseJavaModule
and ReactPackage
to make sure the App.js
could see and call methods from my library, but now I have a problem.
我从React Native调用到我的Android库的这些方法之一会返回我需要显示的Fragment
,但我不确定该怎么做.
One of these methods I call from React Native to my Android library gives back a Fragment
I need to display and I am not sure how to do it.
到目前为止,我已经能够在android
文件夹下的React Native项目中的MainActivity.java
中编写Java代码,以这种方式进行处理
So far I was able to write java code in the MainActivity.java
present in the React Native project under the android
folder to handle it in such a way
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
fragment = MyLibrary.getFragmentMethod();
getFragmentManager().beginTransaction().add(fragment, TAG).commit();
}
我想做的更像是:
getFragmentManager().beginTransaction().replace(R.id.frame_layout_id, fragment, TAG).commit();
但是我不知道如何定义我的React Native应用将使用哪种布局
But I have no clue how to define which layout my React Native app would use
p.s.我试图在onCreate
方法中添加行setContentView(R.layout.main)
,但是为了使它不再可见(可能是因为覆盖了ReactNative"layout"
p.s. I tried to add the line setContentView(R.layout.main)
in the onCreate
method but that mede it so that the App.js
was not visible anymore (probably because was overwriting the ReactNative "layout"
推荐答案
您可以将自己的容器添加到现有布局.例如:
You can add your own container to the existing layout. For example:
window.decorView.findViewById<ViewGroup>(android.R.id.content)
.addView(
FrameLayout(this).apply {
id = R.id.fragment_container
layoutParams = FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT
)
}
)
现在,您可以将片段添加到此容器中. 另外,此库可能会有所帮助- https://github.com/hudl/react-native -android-fragment
Now you can add your fragment to this container. Also this library might be helpful - https://github.com/hudl/react-native-android-fragment
这篇关于在React Native应用程序中加载片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!