2013-02-20 87 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