2013-02-20 20 views
0

我有一個應用程序與許多片段。這是在水平模式佈局:frangment風景如何正確替換

<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> 

我可以看到我的片段correcty,但如果我這樣做:

  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(); 

我可以取代片段,但我可以看到舊片段入化背景.. 我讀THIS我不能替代靜態片段,但如何動態添加它? (多於一個水平模式..)我可以替換一個片段而不知道舊的片段?

+0

可以使用的FrameLayout作爲片段容器。 – user1888162 2013-02-20 19:18:11

回答

0

的容器片段:

<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(); 
+0

沒有你替換我想要dtuser和frag。如果我添加dtuser和片段我有重疊.. – fabio 2013-02-20 19:51:23

+0

代碼固定,現在你已經動態地添加了兩個檔案在相同的佈局 – user1888162 2013-02-20 20:07:47

+0

爲什麼最後一次替換? framelayout .. – fabio 2013-02-20 20:48:59