Curio
Curio

Reputation: 1371

Android: Order of addition of fragments

I have this code:

 findViewById(R.id.button2).setOnClickListener(new View.OnClickListener()
    {
        //Se aggiungo un fragment A al container X e aggiungo il fragment B allo stesso container, il fragment B andrà sopra
        //il fragment A
        @Override
        public void onClick(View v)
        {
            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

            if(ko==0)
            {
                MyFragment fragment = MyFragment.createFragment(0);
                fragmentTransaction.add(R.id.formazione3,fragment);
                ko++;
            }

            else if(ko==1)
            {
                MyFragment fragment = MyFragment.createFragment(1);
                fragmentTransaction.add(R.id.formazione2,fragment);
                ko++;
            }

            else if(ko==2)
            {
                MyFragment fragment = MyFragment.createFragment(2);
                fragmentTransaction.add(R.id.moduli2,fragment);
                ko++;
            }

            else if(ko==3)
            {
                MyFragment fragment = MyFragment.createFragment(1);
                fragmentTransaction.add(R.id.moduli5,fragment);
            }

            fragmentTransaction.commit();
        }
    });

Well, I'm experiencing the order of addition of fragments. I have 4 fragments and I don't understand why some fragments, when I add them, go under the bigger fragment, while other ones go on the bigger fragment in their container.

formazione3>formazione2 (formazione2 is contained in formazione3) moduli2>moduli5 (moduli5 is contained in moduli2, moduli2 is contained in part in formazione3)

When I add the second fragment, it doesn't show, I think that it goes under the previous fragment, so it goes under the bigger. When I add the third fragment, it goes in part under the first fragment, the first is bigger. When I add the fourth fragment, it goes on the third, but the third is bigger than the fourth and above all the the fourth is contained in the third. How does it work? I don't understand at all!

This is the layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.utente.fantacalcio.FormazioniActivity"
android:weightSum="1"
android:id="@+id/activity_formazioni_layout">

<Button
    android:text="Button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="12dp"
    android:id="@+id/button2" />

<Button
    android:text="Button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:id="@+id/button" />

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true">

    <RelativeLayout
        android:layout_width="107dp"
        android:layout_height="match_parent"
        android:id="@+id/moduli">

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/formazione">

    </RelativeLayout>
</LinearLayout>

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="92dp"
        android:layout_height="match_parent"
        android:id="@+id/moduli1">

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/formazione1">

    </RelativeLayout>
</LinearLayout>

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="71dp"
        android:layout_height="match_parent"
        android:id="@+id/moduli2">

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/formazione2">

    </RelativeLayout>
</LinearLayout>

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="51dp"
        android:layout_height="match_parent"
        android:id="@+id/moduli3">

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/formazione3">

    </RelativeLayout>
</LinearLayout>

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="29dp"
        android:layout_height="match_parent"
        android:id="@+id/moduli4">

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/formazione4">

    </RelativeLayout>
</LinearLayout>

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="11dp"
        android:layout_height="match_parent"
        android:id="@+id/moduli5">

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/formazione5">

    </RelativeLayout>
</LinearLayout>

Upvotes: 0

Views: 133

Answers (1)

Cochi
Cochi

Reputation: 2209

Your parent layout is a RelativeLayout , the default behavior for this layout is that first child view is under the last child view.

In your xml this child :

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="51dp"
        android:layout_height="match_parent"
        android:id="@+id/moduli3">

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/formazione3">

    </RelativeLayout>
</LinearLayout>

is under this child view :

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="71dp"
        android:layout_height="match_parent"
        android:id="@+id/moduli2">

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/formazione2">

    </RelativeLayout>
</LinearLayout>

so when the views are inflating , this is the opposite formazione3 is over formazione2.

Moreover your LinearLayout have these attributes :

android:layout_width="match_parent"
android:layout_height="match_parent"

so they take all the place available.

Try to set fixe size in dp instead of match_parent to see what's really happen.

Hope this helps.

Sorry for my poor english.

Upvotes: 1

Related Questions