2013-11-03 99 views
0

我使用導航抽屜和FrameLayout創建了一個活動。此框架佈局應包含與所選導航項目相對應的片段。所以我遵循了android教程中的說明,但它不起作用。片段未被正確替換

問題: 在我看來,片段只是被添加和不被替換。我究竟做錯了什麼?

enter image description here

我的活動XML:

<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/mainViewDrawerLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
> 

<FrameLayout 
     android:id="@+id/mainViewContentFrame" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginLeft="@dimen/drawer_content_padding" > 
</FrameLayout> 


<ListView android:id="@+id/mainViewDrawerList" 
    android:layout_width="@dimen/drawer_size" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:choiceMode="none" 
    android:divider="@android:color/transparent" 
    android:dividerHeight="0dp" 
    android:background="@color/drawer_background" 
    /> 
</android.support.v4.widget.DrawerLayout> 

我的第一個片段的xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:paddingLeft="@dimen/activity_vertical_margin" 
    android:paddingRight="@dimen/activity_vertical_margin" 
    android:paddingTop="@dimen/activity_horizontal_margin" 
    android:paddingBottom="@dimen/activity_horizontal_margin" 
    android:id="@id/fragmentProfile"> 

    <TextView 
      style="@android:style/TextAppearance.Small" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/mainViewProfileGivenNameLabel" 
      /> 
    <TextView 
      android:id="@+id/profileGivenNameTextView" 
      style="@android:style/TextAppearance.Large" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 

      /> 
</LinearLayout> 

我的第二個片段的xml:

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

    <ListView android:id="@+id/protocolTableListView" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:transcriptMode="alwaysScroll" 
       android:cacheColorHint="#00000000" 
       android:listSelector="@android:color/transparent"/> 
</LinearLayout> 

我的過渡方法在活動:

private void switchToFragment(int navigationId, boolean addToBackStack){ 
    FragmentManager fragmentManager = getFragmentManager(); 

    String fragmentTag = null; 
    boolean fragmentCreated = false; 
    switch(navigationId) { 
     case NavigationItemProvider.NAVIGATIONITEM_PROFILE: 
      fragmentTag = NAVIGATIONTAG_PROFILE; 
      break; 
     case NavigationItemProvider.NAVIGATIONITEM_PROTOCOL: 
      fragmentTag = NAVIGATIONTAG_PROTOCOL; 
      break; 
    } 

    // determine if the fragment is already instanciated otherwise create 
    FragmentBase fragment = (FragmentBase)fragmentManager.findFragmentByTag(fragmentTag); 
    if (fragment == null){ 
     if (fragmentTag == NAVIGATIONTAG_PROFILE) { 
      fragment = new ProfileFragment(); 
      fragmentCreated = true; 
     } 
     else if(fragmentTag == NAVIGATIONTAG_PROTOCOL) { 
      fragment = new ProtocolFragment(); 
      fragmentCreated = true; 
     } 
    } 

    // switch to fragment 
    if (fragment.isVisible()){ 
     return; 
    } 

    FragmentTransaction transaction = fragmentManager.beginTransaction(); 
    if (fragmentCreated)   { 
     transaction.replace(R.id.mainViewContentFrame, fragment, fragmentTag); 
    } 
    else{ 
     transaction.show(fragment); 
    } 

    if(addToBackStack) 
    { 
     transaction.addToBackStack(null); 
    } 
    transaction.commit(); 
} 
+0

轉變是複雜..請仔細閱讀http://stackoverflow.com/questions/12529499/problems-with-android-fragment-back-stack/14295368#14295368 –

+0

解決了這個問題: 在我的onCreate返回的結果的super.onCreate而不是Layoutinfalter.inflate的結果,另外我沒有用最後一個參數設置爲false來調用inflate。 –

回答

1

解決了這個問題: 在我的onCreate返回super.onCreate的結果,而不是Layoutinfalter.inflate的結果,除了我沒有叫設置爲false最後一個參數膨脹。

+0

你可以標記爲正確:) –