Android fragment replace将fragment放在顶部,而不是';不要搬走旧的
我见过类似的问题,也尝试过一些方法,但都不奏效。我有一个Android fragment replace将fragment放在顶部,而不是';不要搬走旧的,android,android-fragments,Android,Android Fragments,我见过类似的问题,也尝试过一些方法,但都不奏效。我有一个main活动,其布局包含一个导航抽屉片段和一个FrameLayout,我用它来替换片段。当我单击导航抽屉片段上的一个项目时,我想替换FrameLayout中的第一个片段。问题是在替换操作之后,我的片段出现在旧片段的顶部,旧片段没有被删除 以下是启动MainActivity时添加第一个片段的代码: protected void onCreate(Bundle savedInstanceState) { sup
main活动
,其布局包含一个导航抽屉片段和一个FrameLayout
,我用它来替换片段。当我单击导航抽屉片段上的一个项目时,我想替换FrameLayout
中的第一个片段。问题是在替换操作之后,我的片段出现在旧片段的顶部,旧片段没有被删除
以下是启动MainActivity
时添加第一个片段的代码:
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
文件中进行的替换操作:
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>
我猜您正在混合常规Fragment
类和支持Fragment
类,因此您在NavigationDrawerFragment
中获得的FragmentManager
不是支持FragmentManager
,因此不会删除选项Fragment
,因为它不管理它。您在NavigationDrawerFragment
中导入了哪个Fragment
类?@MikeM。没错。我在我的NavigationDrawerFragment
类中扩展Fragment
而不是FragmentActivity
。是否有方法从main活动
访问支持片段
?我正在使用在internet上找到的一段代码作为导航抽屉,并扩展到FragmentActivity
会导致其他错误。不,您确实希望它扩展Fragment
,只需确保它是android.support.v4.app.Fragment
,而不是android.app.Fragment
。检查您的import
语句。哦,刚刚编辑到getSupportFragmentManager
。现在一切都很好。非常感谢@MikeM。可能的副本