2013-11-03 45 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

你可以标记为正确:) –