To perform add/replace/remove/attach/detach transactions of 2 or more fragments inside a single parent LinearLayout I recommend to follow these basic steps:
Inside your Fragment classes, make sure you specify LayoutParams for your fragments setting the layout_height (or layout_width for horizontal orientation) to "0" while setting the layout_weight to some value:
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_a, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT);
params.weight = 1.0f;
FragmentManager manager = getActivity().getFragmentManager();
FragmentA fragmentA = (FragmentA) manager.findFragmentByTag("A");
fragmentA.getView().setLayoutParams(params);
}
Here I show the code for a single Fragment (FragmentA) class, but make sure you have similar blocks inside each fragment you're gonna use.
And now, inside the Activity, where you have your LinearLayout, here's an example of adding such fragments inside a single LinearLayout:
public void addA(View v) {
FragmentA fragmentA = new FragmentA();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(R.id.linearLayout, fragmentA, "A");
transaction.commit();
}
Where linearLayout will be the parent for the fragments inside our activity layout.