Android replace fragment仍然显示一些被替换的片段

Android replace fragment仍然显示一些被替换的片段,android,android-fragments,Android,Android Fragments,我有一个包含线性布局的选项卡,其中包含一个searchview和一个listview。当用户单击其中一个搜索结果时,我想用另一个包含选择详细信息的布局替换该布局 当我只替换布局的listview部分而替换搜索片段时会发生什么;searchview在屏幕上仍然可见并处于活动状态 以下是我的搜索布局: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/sea

我有一个包含线性布局的选项卡,其中包含一个searchview和一个listview。当用户单击其中一个搜索结果时,我想用另一个包含选择详细信息的布局替换该布局

当我只替换布局的listview部分而替换搜索片段时会发生什么;searchview在屏幕上仍然可见并处于活动状态

以下是我的搜索布局:

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

    <SearchView
        android:id="@+id/public_calendar_search"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:iconifiedByDefault="false"
    </SearchView>

    <ListView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1.0"
        android:choiceMode="singleChoice" />

    <TextView
        android:id="@android:id/empty"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:gravity="center"/>

</LinearLayout>
详细信息片段创建:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.detail_layout, container, false);
    return v;
}

static DetailFragment newInstance(String cal) {
    DetailFragment d = new DetailFragment();
    return d;
}
我做错了什么?如何替换整个搜索布局


谢谢

我解决了我的问题。马可并不完全正确,但他为我指明了正确的方向

问题是,我传递给replace方法的容器ID是我要替换的片段的ID,而不是片段容器的ID。这似乎解释了为什么在替换之后仍然保留了一些原始片段控件-整个片段没有被替换

我更改了它以获得片段容器视图ID,这就成功了!代码如下:

transaction.replace(((ViewGroup)(getView().getParent())).getId(), fragment); 

我在这里找到了获取片段的容器视图ID的答案。

我使用了正确的ID,然后它也没有消失。在新的片段布局中,您需要将背景色设置为所需的颜色,如下所示

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:background="@color/white_color">

要解决此问题,请在片段布局文件中将android:background属性设置为“?android:windowBackground”

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?android:windowBackground"
    tools:context=".MainFragment">

    ...

</LinearLayout>

...
在片段2中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?android:windowBackground"
    tools:context=".SomeFragment">

    ...

</LinearLayout>

...

我也遇到了同样的问题,解决方案对我不起作用。也许这会帮助某人

我在活动布局xml文件中添加了一个片段。这引起了很多问题


相反,在xml文件中添加框架布局,然后以编程方式添加和替换片段。这会导致整个片段被替换。

您没有显示DetailFragment如何设置其layoutHeiko,我添加了该信息……因此,您是在尝试删除原始搜索选项卡,还是在其上临时放置布局以显示搜索结果??后者。我想用details片段替换搜索片段(它包含searchview和listview)。用户在查看和/或操作详细信息片段后将返回搜索片段。这是工作-几乎。显示详细信息片段时,搜索片段中的searchview仍然可见(且功能正常)。在调试过程中,我在searchview之前和之后都添加了一个textview,以查看发生了什么。在这种情况下,文本视图和搜索视图都保持可见,并且显示了详细信息片段。您是否考虑过将详细信息片段添加到所有内容的顶部,并为其使用不同的标记。例如,DetailFragment=DetailFragment.newInstance(cal);add(android.R.id.content,片段,“MyDetailsTaG”);transaction.addToBackStack(空);commit()+1此行指向什么视图((视图组)(getView().getParent())。getId()请帮助此行指向什么视图((视图组)(getView().getParent())。getId()请帮助–这仍然不能解决我的问题。我仍然可以看到我的旧碎片。
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?android:windowBackground"
    tools:context=".MainFragment">

    ...

</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?android:windowBackground"
    tools:context=".SomeFragment">

    ...

</LinearLayout>