gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 23:40:24 25 4
gpt4 key购买 nike

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

当我替换搜索 fragment 时会发生什么,只有布局的 ListView 部分被替换;搜索 View 在屏幕上仍然可见并处于 Activity 状态。

这是我的搜索布局:

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

以及我替换搜索 fragment 的代码:

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

细节 fragment 创建:

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

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

谢谢!

最佳答案

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

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

我更改了它以获取 fragment 容器 View ID,并且成功了!代码如下:

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

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

关于Android替换 fragment 仍然显示一些被替换的 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12958555/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com