Android替换Fragment后仍显示部分被替换的Fragment

19

我有一个选项卡,其中包含一个线性布局,其中包含一个搜索视图和一个列表视图。 当用户单击搜索结果中的一个时,我希望用包含所选内容详细信息的另一个布局替换该布局。

当我替换搜索片段时,只有布局的列表视图部分被替换;搜索视图仍可在屏幕上显示和操作。

这是我的搜索布局:

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

这里是详细布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/detail_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:baselineAligned="false"
        android:orientation="vertical" >

        <Space
            android:layout_width="0dip"
            android:layout_height="20dip"
            android:layout_weight="1" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:baselineAligned="false"
        android:orientation="horizontal" >

        <Space
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight=".1" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".8"
            android:text="@string/label_Name" />

        <Space
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight=".1" />
    </LinearLayout>

    .........

</LinearLayout>

我用以下代码替换了搜索片段:

FragmentTransaction transaction = getFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack.
DetailFragment fragment = DetailFragment.newInstance(cal);
transaction.replace(R.id.search_layout, fragment);

transaction.addToBackStack(null);
transaction.commit();

详细片段创建:

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

我做错了什么?如何替换整个搜索布局。

谢谢!


你没有展示DetailFragment如何设置它的布局。 - Heiko Rupp
Heiko,我添加了那个信息... - sitecrawl
1
你是想删除原始搜索选项卡还是暂时放置一个布局来显示搜索结果? - Marco RS
我想用详情片段替换搜索片段(其中包含搜索视图和列表视图)。用户在查看和/或处理详细信息片段后,将返回到搜索片段。这个方案基本上是可行的。但是,在显示详细信息片段时,搜索片段中的搜索视图仍然可见(且可操作)。在调试过程中,我添加了一个文本视图,分别放在搜索视图之前和之后,以查看发生了什么。在这种情况下,当显示详细信息片段时,两个文本视图和搜索视图仍然可见。 - sitecrawl
1
你是否考虑过将DetailsFragment添加到所有内容的顶部,并使用不同的标签呢?例如, DetailFragment fragment = DetailFragment.newInstance(cal); transaction.add(android.R.id.content, fragment,"MyDetailsTaG"); transaction.addToBackStack(null); transaction.commit(); - Marco RS
我在这里发布了一个与此类似的问题的答案:https://dev59.com/OmQn5IYBdhLWcg3wr406#63683045 - Kishan Solanki
5个回答

18

我找到了问题所在。Marco 不完全正确,但他指出了我应该朝哪个方向去解决问题。

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

我将其更改为获取片段容器视图 ID,然后它就起作用了!以下是代码:

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

我在这里找到了获取fragment容器视图ID的答案,Get fragment's container view id.


1
+1 这行代码是指向哪个视图的?((ViewGroup)(getView().getParent())).getId() 请帮忙翻译。 - N Sharma
这行代码指向哪个视图 ((ViewGroup)(getView().getParent())).getId()。请帮忙。 - user3600801
这仍然没有解决我的问题。我仍然可以看到我的旧片段。 - AnilV

12

我使用了正确的ID,但它仍然没有消失。 原来你需要在新的片段布局中将背景颜色设置为所需的颜色,像这样。

结果是这样的:

我使用了正确的ID,但它仍然没有消失。原来,在新的Fragment布局中,您需要将背景颜色设置为所需的颜色,就像这样。

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

6
为了解决这个问题,请在片段布局文件中将 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>

在 Fragment_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>

1
我遇到了同样的问题,但是那个解决方案对我没有用。也许这会帮助到其他人...
我在活动布局xml文件中添加了一个片段,这引起了很多问题。
相反,您可以在xml文件中添加一个frame布局,然后通过编程方式添加和替换片段。这将导致整个片段被替换。

0

我曾经遇到过同样的问题,这是我的解决方案。 尝试使用FrameLayout而不是fragment在你的布局文件中。就像这样:

    <fragment android:id="@+id/your_fragment" />

然后,在活动方法onCreate()中添加您的默认片段。

getFragmentManager().beginTransaction()
.add(R.id.your_fragment, YourFragment)
.commit();

现在,旧的片段可以被正确地替换,看起来很棒。

这对我有用,希望对你也有帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接