【问题标题】:Android replace fragment still displays some of the replaced fragmentAndroid 替换 Fragment 仍然显示一些被替换的 Fragment
【发布时间】:2012-10-18 15:45:30
【问题描述】:

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

当我替换搜索片段时会发生什么,只有布局的列表视图部分被替换;搜索视图在屏幕上仍然可见并处于活动状态。

这是我的搜索布局:

<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,我添加了那个信息……
  • 那么您是要删除原来的搜索标签,还是要在其上临时放置一个布局以显示您的搜索结果??
  • 后者。我想用详细信息片段替换搜索片段(包含搜索视图和列表视图)。然后,用户将在查看详细信息片段和/或对其进行操作之后返回到搜索片段。这是工作 - 几乎。显示详细信息片段时,搜索片段中的搜索视图仍然可见(并且可以正常工作)。在调试中,我在 searchview 之前和之后添加了一个 textview 以查看会发生什么。在这种情况下,文本视图和搜索视图都保持可见,并显示详细信息片段。
  • 您是否考虑过在所有内容之上添加 detailsfragment 并为其使用不同的标签。例如,DetailFragment 片段 = DetailFragment.newInstance(cal); transaction.add(android.R.id.content, fragment,"MyDetailsTaG"); transaction.addToBackStack(null); transaction.commit();

标签: android android-fragments


【解决方案1】:

我发现了我的问题。 Marco 并不完全正确,但他指出了我正确的方向。

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

我更改了它以获取片段容器视图 ID,并且成功了!代码如下:

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

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

【讨论】:

  • +1 这行指向什么视图((ViewGroup)(getView().getParent())).getId()请帮忙
  • 这条线指向什么视图 ((ViewGroup)(getView().getParent())).getId() 请帮忙-
  • 这仍然不能解决我的问题。我仍然可以看到我的旧片段。
【解决方案2】:

我使用了正确的 ID,然后它也没有消失。事实证明,您需要在新的 Fragment 布局中将 Background color 设置为您需要的颜色,如下所示。

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

【讨论】:

    【解决方案3】:

    要解决此问题,请在片段布局文件中将 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>
    

    【讨论】:

      【解决方案4】:

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

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

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

      【讨论】:

        【解决方案5】:

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

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

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

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

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

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

        【讨论】:

          猜你喜欢
          • 2012-12-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-04-07
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多