0

i have an app with many fragments. this is layout on horizontal mode:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment android:name="com.map.manager.ListUser"
          android:id="@+id/listuser_fragment"
          android:layout_weight="1"
          android:layout_width="0dp"
          android:layout_height="match_parent" />

    <fragment android:name="com.map.manager.DetailApp"
          android:id="@+id/detailapp_fragment"
          android:layout_weight="2"
          android:layout_width="0dp"
          android:layout_height="match_parent" />   

</LinearLayout>

i can see my fragments correcty but if i do this:

            DetailUser appFrag = (DetailUser)
                    getSupportFragmentManager().findFragmentById(R.id.detailuser_fragment);

            if (appFrag != null) {
                // If article frag is available, we're in two-pane layout...

                // Call a method in the ArticleFragment to update its content
                appFrag.updateAppointmentView(position);

            } 
            else {
            DetailUser dtuser = new DetailUser();
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.replace(R.id.detailapp_fragment, dtuser);
            transaction.commit();

i can replace fragments but i can see old fragment into backgroud.. i read THIS i can't replace static fragments but how add it dynamically? (more than one into horizontal mode..) Can i replace a fragment without knowing old fragment?

Community
  • 1
  • 1
fabio
  • 271
  • 5
  • 18

1 Answers1

0

Container for fragments:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:id="@+id/fragmentLayout"
    >

     <FrameLayout
        android:id="@+id/framelayout1"
        android:layout_width="313dp"
        android:layout_height="match_parent" >
     </FrameLayout>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/framelayout2" >
    </FrameLayout>

</LinearLayout>



//Adding Detailuser fragment to framelayout1:

    DetailUser dtuser = new DetailUser();
            FragmentTransaction transaction =    getSupportFragmentManager().beginTransaction();
            transaction.add(R.id.framelayout1, dtuser);
            transaction.commit();
//Adding another fragment to framelayout1:

    Fragment frag = new Fragment();
            FragmentTransaction transaction =    getSupportFragmentManager().beginTransaction();
            transaction.add(R.id.framelayout2, frag);
            transaction.commit();

//Replacing fragment in framelayout:

    Fragment2 frag2 = new Fragment2();
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.framelayout1, frag);
                transaction.commit();
user1888162
  • 1,735
  • 21
  • 27
  • no you replace i want both dtuser and frag. if i add dtuser and frag i have overlapping.. – fabio Feb 20 '13 at 19:51
  • Code fixed, now you have dynamically added two framents in same layout – user1888162 Feb 20 '13 at 20:07
  • Replace fragment that framelayout1 is currently containing. If framelayout contains dtuser Fragment, frag2 will replace it. – user1888162 Feb 20 '13 at 20:57
  • thanks! for replace fragments (and dynamic menu) is there an effective design pattern? (if i replace list modify menu for example) without the heavy use of variables.. thanks! – fabio Feb 22 '13 at 09:26