2013-03-26 74 views
0

我为片段事务使用以下布局。我正在使用以下代码替换片段1 与新片段。我附加布局文件也供您参考。我为每个listitem点击执行下面的代码。Android片段替换问题

代码:

 Fragment newFragment = new Grades(); 
         android.app.FragmentTransaction transaction = getFragmentManager().beginTransaction(); 

          // Replace whatever is in the fragment_container view with this fragment 
         transaction.replace(R.id.fragment1, newFragment); 

          // Commit the transaction 
         transaction.commit(); 
Fragment newFragment = new Teachers(); 
         android.app.FragmentTransaction transaction = getFragmentManager().beginTransaction(); 

          // Replace whatever is in the fragment_container view with this fragment 
         transaction.replace(R.id.fragment1, newFragment); 

          // Commit the transaction 
         transaction.commit(); 
<LinearLayout 
       android:id="@+id/slidingPanel" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:gravity="left" 
       android:orientation="vertical" 
       android:background="@android:color/white" 

       > 

<fragment 
        android:id="@+id/fragment1" 
        android:name="com.example.slideoutmenu.Item3" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" /> 

</Linearlayout> 

我怀疑是我应该更换片段1每次当我展现出新的片段或者我应该取代现有的片段,如果这样我怎么能取代现有的片段。

回答

1

要更换片段: -

FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); 
YourFragment yourFragment = new YourFragment(); 
fragmentTransaction.replace(R.id.top_layout, yourFragment); // top_layout is parent layout/viewgroup where you want to place your new fragment 
fragmentTransaction.commit();