2016-08-05 14 views
3

私はReact Nativeを使用したいと思うように既にAndroidアプリケーションを構築しています。それは大規模なアプリケーションであり、すべてを一度に移行したくありません。たとえば、私のツールバーのコードはちょっと複雑なので、今はそのままにしておきたいと思います。しかし、私は(setContentView上のエラーを取得していますReactRootViewを入れ子にすることができます(アンドロイド)

mReactRootView = (ReactRootView) findViewById(R.id.react_root); 
... // Other init 
setContentView(mReactRootView); 

<RelativeLayout 
    android:id="@+id/relative_layout" 
    android:background="@color/background_color" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/action_bar" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true"/> 

    <com.facebook.react.ReactRootView 
     android:id="@+id/react_root" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 
</RelativeLayout> 

とのonCreateで()の代わりに、私はこれをやっている新しいReactRootViewの作成:だから、私のレイアウトは次のようになりますmReactRootView):

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 

これは私が巣を他のビュー内ルートビューを反応することはできないと信じて私をリード。現在のツールバーの実装を維持するにはどうすればよいですか?他のページではReact Nativeを使用していますか?私はこれが存在することを知っています:https://facebook.github.io/react-native/docs/native-components-android.htmlネイティブコンポーネントへのブリッジングは私の唯一のオプションですか?

編集は:以下コンスタンチンの提案に続き、ここで私が持っているものです。

ReactFragment.java

public class ReactFragment extends Fragment implements DefaultHardwareBackBtnHandler { 
    ViewGroup group = (ViewGroup) inflater.inflate(R.layout.react_fragment, container, false); 
    activity = getActivity(); 

    mReactRootView = new ReactRootView(activity); 
    mReactInstanceManager = ReactInstanceManager.builder() 
      .setApplication(activity.getApplication()) 
      .setBundleAssetName("index.bundle") 
      .setJSMainModuleName("index") 
      .addPackage(new MainReactPackage()) 
      .setUseDeveloperSupport(true) 
      .setInitialLifecycleState(LifecycleState.RESUMED) 
      .build(); 

    group.addView(mReactRootView); 
    mReactRootView.startReactApplication(mReactInstanceManager, "AwesomeProject", null); 
    mReactInstanceManager.onHostResume(activity, this); 
    return group; 
} 
// onPause() onDestroy() etc. 

react_fragment.xml

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/react_root" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
</FrameLayout> 

追加

をmain_content.xmlします
... 
<fragment 
    android:name="com.amazon.kindlestore.main.ReactFragment" 
    android:id="@+id/react_frag" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"/> 
... 

そして、私の主な活動に私はこのようなフラグメントを追加している:

ReactFragment reactFragment = (ReactFragment) getFragmentManager().findFragmentById(R.id.react_frag); 
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); 
fragmentTransaction.add(R.id.react_frag, reactFragment); 
fragmentTransaction.commit(); 

これが動作しているようだ、私は私の母国ツールバーの下に

+0

私は私のアプリで同じ問題を持っている(トップメニューにアンドロイドを選択することを忘れないでください)。私はアンドロイドネイティブアプリケーションを使用するカスタムタブバーを使用する必要があります。しかし今では私のアプリケーションの一部は反応ネイティブです反応するネイティブブリッジを使用して反応ネイティブでタブバーを表示/非表示する可能性があります –

答えて

0

をレンダリングコンポーネントに反応しているあなたは、フラグメントを作成する必要がありますあなたのXMLレイアウトでそれを使用してください。このフラグメントでReactRootViewを作成し、ReactInstanceManagerを作成または取得します。これは、フラグメントのライフサイクルコールバックにフックする必要があります。そして、このインスタンスマネージャを使用してReactRootViewの内部でRNアプリケーションを起動します。

チェックアウトこの公式guide

+0

私はこれを試して、いくつかの進歩を遂げたが、物事はまだ動作していません。あなたは私が質問にした編集を見て、何かが欠落しているかもしれないかどうか確認できますか? – Chris

+0

BundleAssetName、JSMainModuleNameを変更したことに気付きました。それらの名前であなたのjsバンドルが利用可能になっていますか?あなたのアプリをパッケージ化するときにjsバンドルアセットを提供するのか、それとも開発ノードサーバーから提供するのですか?そして、正しい反応コンポーネント名を使用しますか?つまり、 'AppRegistry.registerComponent( 'AwesomeProject'、...'? –

+0

のようなjsコ​​ードで登録しますか?その更新を投稿した後、ReactRootViewを別のReactRootView私はreact_fragment.xmlをReactRootViewの代わりにFrameLayoutを含むように変更しました。物事はうまくいくようです。ありがとう! – Chris

関連する問題