11

I am using a LinearLayout with a vertical orientation to list fragments. I add fragments to the container programmatically like this:

FragmentTransaction ft = fragmentManager.beginTransaction();

Fragment fragment1 = new Fragment();
ft.add(R.id.llContainer, fragment1);

Fragment fragment2 = new Fragment();
ft.add(R.id.llContainer, fragment2);

ft.commit();

But it only shows the first fragment. Why?

ferpar1988
  • 596
  • 2
  • 5
  • 17
  • 1
    Does your first fragment set match_parent for width and height? Then you know what's going on... – ElDuderino Mar 21 '14 at 11:57
  • No it has wrap_content for layout_height attribute. – ferpar1988 Mar 21 '14 at 12:01
  • 1
    @ferpar1988: as Mighter told you - layout container can host only one active fragment. therfore - you'll have to use to different layout containers if you want to show at the same time two different fragments – Tal Kanel Mar 21 '14 at 12:33

5 Answers5

20

You can have multiple fragments in a LinearLayout.

According to the documentation,

If you're adding multiple fragments to the same container, then the order in which you add them determines the order they appear in the view hierarchy

The problem with your code is that because you didn't specify fragment tags, it defaulted to the container id. Since the container id is the same for both transactions, the 2nd transaction replaced the 1st fragment, rather than added it to the container separately.

To do what you want, use something like:

FragmentTransaction ft = fragmentManager.beginTransaction();

Fragment fragment1 = new Fragment();
ft.add(R.id.llContainer, fragment1, "fragment_one");

Fragment fragment2 = new Fragment();
ft.add(R.id.llContainer, fragment2, "fragment_two");

ft.commit();
yincrash
  • 6,394
  • 1
  • 39
  • 41
  • I like this answer better because it's easier to implement this solution in a programmatic way, e.g. adding fragments numerically, and you can just use the indexes as tags. – DataGraham Dec 29 '14 at 03:52
  • 4
    I've having the same problem but this doesn't solve it. I had different tags for each fragment, but still it looks like they're overlapping each other. Any idea why? – Zip Jan 13 '17 at 19:41
9

I guess you have to define separe containers in your layout for each Fragment.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false"
    android:orientation="vertical">

    <FrameLayout
        android:id="@+id/content_secondary"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">
    </FrameLayout>

     <FrameLayout
         android:id="@+id/content_primary"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:layout_weight="1" >
     </FrameLayout>

</LinearLayout>
  • You can, but definitely don't have. I don't see why you would need to do this, AND it's not scalable. – jobbert Jan 29 '20 at 15:26
2

Had the same problem, but using

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.control_list, container, false);

I.E. using a xml layout file:

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

Solved my problem. When creating the LinearList programmatically only the first fragment showed up for me too.

That is using an xml file for the layout you want to add the fragments to.

Erik
  • 101
  • 5
2

solved it by adding this to the linearlayout:

android:orientation="vertical"
rob hofman
  • 21
  • 1
0

I had this problem because the layouts that I was inflating had a height of "match_parent" instead of "wrap_content". The LinearLayout and it's container's height is unchanged.

TestFragment.java

...
@Nullable
@Override
public View onCreateView(LayoutInflater inflater,@Nullable ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.test_fragment_layout, container, false);
}
...

test_fragment_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <!-- ... -->

</android.support.constraint.ConstraintLayout>