android:替换CoordinatorLayout中的片段容器 [英] android: Replace fragment container inside a CoordinatorLayout
问题描述
我有以下XML:
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<!--I use this include as container with the FrameLayout below-->
<!--<include layout="@layout/content_main" />-->
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.improvemybrand.MainActivity"
tools:showIn="@layout/app_bar_main">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</FrameLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_input_add" />
问题很简单:
当我尝试从协调器替换容器FrameLayout时,它不起作用,它显示了新片段,但仍保留了旧片段,在我的简单示例中,将保留带有Hello world的TextView.
When I try to replace the container FrameLayout from my Coordinator, it does not work, it shows the new fragment but also keeps the old one, in my simple example, the TextView with Hello world will remains.
要替换,我使用以下代码:
To replace, I'm using the following code:
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.content_main, fragment);
transaction.commit();
有什么想法吗?
推荐答案
FragmentTransaction
仅处理Fragment
和它们的View
.您在布局中定义的TextView
不是Fragment
的View
的一部分,因此它不会受到FragmentTransaction
的影响,并且代码段中的Fragment
在其顶部添加.
FragmentTransaction
s deal only with Fragment
s and their View
s. The TextView
you've defined in your layout is not (part of) a Fragment
's View
, so it will not be affected by a FragmentTransaction
, and the Fragment
in your snippet will just be added on top of it.
您有一些选择.您可以在执行交易时自行隐藏或删除TextView
.如果您最初只需要简单的TextView
,并且将其粘贴在Fragment
中可能会过大,那么这可能是更可取的.您也可以简单地在Fragment
的布局上设置不透明的背景,这样可以有效地隐藏TextView
.
You have a few options. You could hide or remove the TextView
yourself when performing the transaction. This might be preferable, if that simple TextView
is all you need initially, and sticking it in a Fragment
could be overkill. You could also simply set an opaque background on the Fragment
's layout, which will effectively hide the TextView
.
但是,最好的选择是将TextView
放在另一个Fragment
的布局中,该布局在启动时加载.随后的事务将按照您的期望进行替换/删除.请注意,您希望在运行时替换/删除的任何Fragment
必须动态加载到代码中.也就是说,不能在Activity
布局的<fragment>
元素中定义它们.
The probably best option, however, is to put the TextView
in a layout for another Fragment
which is loaded at startup. Subsequent transactions will then replace/remove it as you're expecting. Do note that any Fragment
you wish to replace/remove at runtime must be loaded dynamically in your code. That is, they cannot be defined in <fragment>
elements in your Activity
's layout.
这篇关于android:替换CoordinatorLayout中的片段容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!