我正在尝试使用新的导航抽屉模式将一个片段替换为另一个片段。它似乎有效,但是当我从抽屉中选择另一个选项时,新片段已加载,但两个片段都是可见的。我没有使用静态片段布局,所以我不知道问题出在哪里。
片段是通过在我的活动上onItemClick
实现的方法加载的。AdapterView.OnItemClickListener
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Fragment fragmentToShow = null;
// Load desired fragment
switch (position) {
case 0: // Authors
if (fragmentAuthors == null) fragmentAuthors = new FragmentAuthors();
fragmentToShow = fragmentAuthors;
break;
case 1: // Books
if (fragmentBooks == null) fragmentBooks = new FragmentBooks();
fragmentToShow = fragmentBooks;
break;
}
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.ActivityMain_Drawer_FrameMain, fragmentToShow);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
mDrawerLayout.closeDrawers();
}
布局
<!-- The main content view -->
<FrameLayout
android:id="@+id/ActivityMain_Drawer_FrameMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/background_light" />
<!-- The navigation drawer -->
<FrameLayout
android:id="@+id/ActivityMain_Drawer_FrameMenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/background_light">
<ListView
android:id="@+id/ActivityMain_Drawer_FrameMenu_List"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp" />
</FrameLayout>
截图
https://dl.dropboxusercontent.com/u/1763308/Screenshots/device-2013-05-27-113128.png https://dl.dropboxusercontent.com/u/1763308/Screenshots/device-2013-05-27 -113139.png