Quantcast
Channel: Active questions tagged react-native+android - Stack Overflow
Viewing all articles
Browse latest Browse all 29445

How to add a View as a Fragment in React Native

$
0
0

I go about adding an Android native View to React Native as follows :

BulbView (The View To Be Added) :

public class BulbView {

    Button button;

    LinearLayout linearLayout;
    LinearLayout linearLayoutContainer;

    public BulbView(Context context) {
        linearLayout = new LinearLayout(context);
        linearLayout.setOrientation(LinearLayout.VERTICAL);

        linearLayoutContainer = new LinearLayout(context);
        linearLayoutContainer.setOrientation(LinearLayout.VERTICAL);

        button = new Button(context);
        button.setText("This button is created from JAVA code");

        linearLayout.addView(button);
        linearLayout.addView(linearLayoutContainer);

        button.setOnClickListener(v -> {
            TextView newTV = new TextView(context);
            newTV.setText("ITEM");
            linearLayoutContainer.addView(newTV);
        });
    }

    public View getButton() {
        return linearLayout;
    }
}  

BulbManager :

public class BulbManager extends SimpleViewManager<View> {
    @Override
    public String getName() {
        return "Bulb";
    }

    @Override
    protected View createViewInstance(ThemedReactContext reactContext) {
        return new BulbView(reactContext).getButton();
    }
} 

The Packager :

public class BulbPackage implements ReactPackage {
    @Override
    public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
        return Collections.emptyList();
    }

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

Now we start calling the View :

public class MyReactActivity {
    private Context context;
    private ReactInstanceManager mReactInstanceManager;

    public MyReactActivity(Context context) {
        this.context = context;

        mReactInstanceManager = ReactInstanceManager.builder()
                .setApplication(((Activity) context).getApplication())
                .setCurrentActivity((Activity) context)
                .setBundleAssetName("index.android.bundle")
                .setJSMainModulePath("index")
                .addPackage(new MainReactPackage())
                .addPackage(new BulbPackage())
                .setUseDeveloperSupport(BuildConfig.DEBUG)
                .setInitialLifecycleState(LifecycleState.RESUMED)
                /** .setInitialLifecycleState(LifecycleState.BEFORE_CREATE) **/
                .build();
    }

    public View reactJsView() {
        ReactRootView mReactRootView = new ReactRootView(context);
        mReactRootView.startReactApplication(mReactInstanceManager, "HelloWorld", null);
        return mReactRootView;
    }
}  

I then call it as a Fragment :

public class TestFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return new MyReactActivity(getContext()).reactJsView();
    }
}  

button.setOnClickListener above does not work as expected when I add the Fragment to a View other than the root view.

getFragmentManager().beginTransaction().add(scrollViewWrapperItems_LL.getId(), new TestFragment()).commit();  

A new Android View (newTV) should be added into a LinearLayout, but that does not seem to happen. However, a linearLayoutContainer.getChildCount() shows that the children's count does increase, but the container view `(linearLayoutContainer)'' does not visually show this. The added child is invisible.

However, adding it to the root view rootContainer_LL below works. It does not work when added into any other child view such as scrollViewWrapperItems_LL.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setMyReactActivity(new MyReactActivity(this));

    rootContainer_LL = new LinearLayout(this);
    rootContainer_LL.setOrientation(LinearLayout.VERTICAL);
    rootContainer_LL.setId(Integer.valueOf(123456));

    scrollViewWrapperItems_LL = new LinearLayout(this);
    scrollViewWrapperItems_LL.setOrientation(LinearLayout.VERTICAL);
    scrollViewWrapperItems_LL.setId(Integer.valueOf(12345));

    myButton = new Button(this);
    myButton.setText("REV _Button");
    myButton.setOnClickListener(v -> {
        getFragmentManager().beginTransaction().add(scrollViewWrapperItems_LL.getId(), new TestFragment()).commit();
    });

    ScrollView scrollView = new ScrollView(this);
    scrollView.addView(scrollViewWrapperItems_LL);

    rootContainer_LL.addView(myButton);
    rootContainer_LL.addView(scrollView);

    setContentView(rootContainer_LL);
}

How can I fix this so that I can add the React native into any child view rather than being restricted to only using it in the root View?

Thank you all in advance.


Viewing all articles
Browse latest Browse all 29445

Trending Articles