Android片段替换功能将片段放在最前面,不会删除旧的片段 [英] Android fragment replace puts fragment on top, doesn't remove old one
问题描述
我看到了类似的问题,尝试了一些方法,但没有一个起作用.我有一个 MainActivity
,其布局包含一个导航抽屉片段和一个 FrameLayout
,可用来替换我的片段.当我单击导航抽屉片段上的一个项目时,我想替换 FrameLayout
中的第一个片段.问题是替换操作后,我的片段出现在旧片段的顶部,旧片段没有被移除.
I have seen similar issues and tried a few things but none of that worked. I have a MainActivity
with a layout containing a Navigation Drawer fragment and a FrameLayout
, which I use to replace my fragments. When I click an item on the Navigation Drawer fragment, I want to replace the first fragment in FrameLayout
. The problem is after the replace operation, my fragment appears on top of the old one, the old one isn't removed.
这是在 MainActivity
启动时添加第一个片段的代码:
Here is the code for adding the first fragment when MainActivity
launches:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OptionsFragment mFragment = new OptionsFragment();
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.container, new OptionsFragment(), OPTIONS_TAG).commit();
这是 NavigationDrawerFragment
文件中发生的替换操作:
Here is the replace operation taking place in the NavigationDrawerFragment
file:
public void onNavigationDrawerItemSelected(int position) {
Fragment fragment;
fragment = getFragmentManager().findFragmentByTag(MainActivity.OPTIONS_TAG);
if(fragment != null)
getFragmentManager().beginTransaction().remove(fragment).commit();
switch (position) {
case 0:
fragment = getFragmentManager().findFragmentByTag(MainActivity.LOGIN_TAG);
if (fragment == null) {
fragment = new LoginFragment();
}
getFragmentManager().beginTransaction().replace(R.id.container, fragment, MainActivity.LOGIN_TAG).commit();
closeDrawer();
break;
}
}
这是XML布局:
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main_activity">
<include
android:id="@+id/toolbar_actionbar"
layout="@layout/toolbar_default"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/toolbar_actionbar">
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true" />
<!-- android:layout_marginTop="?android:attr/actionBarSize"-->
<fragment
android:id="@+id/fragment_drawer"
android:name="com.kristmiha.myportal2.NavigationDrawerFragment"
android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="@layout/fragment_navigation_drawer"
tools:layout="@layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
推荐答案
我希望它能起作用: https://stackoverflow.com/a/38470075/6291068
android:background="#ffffff"
这篇关于Android片段替换功能将片段放在最前面,不会删除旧的片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!