片段彼此重叠显示 [英] Fragments displayed over each other

查看:109
本文介绍了片段彼此重叠显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了 http://developer中的指南,具有DrawerLayout的Activity .android.com/training/implementing-navigation/nav-drawer.html .

当我单击一个抽屉项时,我用新的片段替换了当前视图:

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);

现在,有时不替换旧片段,而是将新片段放在旧片段的顶部:

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();
}

请确保检查容器是否为空!

感谢 https://stackoverflow.com/users/2677588/lia-pronina

I've got an Activity with a DrawerLayout, using the guidelines from http://developer.android.com/training/implementing-navigation/nav-drawer.html.

When I click on an drawerItem, I replace the current view with the new 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);

Now, sometimes the old fragment is not replaced but the new fragment is placed on top of the old one:

http://img15.imageshack.us/img15/3179/1kqj.png

Why is this, and how to solve this problem?

Relevant 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>

This only happens sometimes, and I haven't found a flow to reproduce this yet. The app doesn't support rotating, so it won't happen there.

解决方案

We went live with this version and havent received any complaints about this, so I will assume this was the correct answer:

in your onCreateView method add:

if (container != null) {
    container.removeAllViews();
}

Be sure to check if container is not null!

Thanks https://stackoverflow.com/users/2677588/lia-pronina!

这篇关于片段彼此重叠显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆