Cannot add multiple fragments to LinearLayout
Asked Answered
F

5

12

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?

Festal answered 21/3, 2014 at 11:54 Comment(3)
Does your first fragment set match_parent for width and height? Then you know what's going on...Coward
No it has wrap_content for layout_height attribute.Festal
@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 fragmentsLagunas
G
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();
Grieve answered 27/5, 2014 at 18:11 Comment(2)
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.Astera
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?Mieshamiett
N
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>
Nunatak answered 21/3, 2014 at 12:1 Comment(1)
You can, but definitely don't have. I don't see why you would need to do this, AND it's not scalable.Haydenhaydn
E
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.

Elaterite answered 16/6, 2017 at 8:4 Comment(0)
C
2

solved it by adding this to the linearlayout:

android:orientation="vertical"
Candidacandidacy answered 10/12, 2018 at 8:32 Comment(0)
S
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>
Stone answered 19/4, 2018 at 7:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.