我有一个带有 DrawerLayout 的 Activity,使用 http://developer.android.com/training/implementing-navigation/nav-drawer.html 中的指南。 .
当我点击一个drawerItem时,我将当前 View 替换为新的 fragment :
Fragment fragment;
Bundle args = new Bundle();
fragment = new NewsListFragment();
args.putInt("category", position);
// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment)
.commit();
mDrawerList.setItemChecked(position, true);
现在,有时旧 fragment 没有被替换,而是新 fragment 被放置在旧 fragment 之上:
http://img15.imageshack.us/img15/3179/1kqj.png
为什么会这样,以及如何解决这个问题?
相关的 XML:
<!-- The main content view -->
<LinearLayout
android:id="@+id/rlMain"
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="fill_parent">
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
</FrameLayout>
这种情况有时会发生,我还没有找到重现这种情况的流程。该应用程序不支持旋转,所以它不会在那里发生。
最佳答案
我们使用这个版本并没有收到任何投诉,所以我认为这是正确的答案:
在您的 onCreateView
方法中添加:
if (container != null) {
container.removeAllViews();
}
一定要检查容器是否为空!
关于android - fragment 相互重叠显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18309815/