Android 片段事务替换()不工作
我试图用导航抽屉将一个片段替换为另一个片段。但它只增加了旧的片段。我研究了网站上的问题,但我仍然没有解决这个问题。就像碎片重叠一样。这是我的密码 主要活动Android 片段事务替换()不工作,android,android-fragments,Android,Android Fragments,我试图用导航抽屉将一个片段替换为另一个片段。但它只增加了旧的片段。我研究了网站上的问题,但我仍然没有解决这个问题。就像碎片重叠一样。这是我的密码 主要活动 public boolean onNavigationItemSelected(MenuItem item) { // Handle navigation view item clicks here. int id = item.getItemId(); android.support.v4.app.Fragment
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
android.support.v4.app.Fragment fragment = null;
if (id == R.id.nav_camera) {
Toast.makeText(getApplicationContext(),
"Kamera click", Toast.LENGTH_LONG).show();
fragment = new PuanDurumuFragment();
} else if (id == R.id.nav_gallery) {
Toast.makeText(getApplicationContext(),
"Galeri click", Toast.LENGTH_LONG).show();
} else if (id == R.id.nav_slideshow) {
Toast.makeText(getApplicationContext(),
"3.click", Toast.LENGTH_LONG).show();
}
else if (id == R.id.nav_manage) {
Toast.makeText(getApplicationContext(),
"Fikstür click", Toast.LENGTH_LONG).show();
}
if (fragment != null) {
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_main, fragment).addToBackStack(null).commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
// update selected item and title, then close the drawer
navigationView.setCheckedItem(id);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
return true;
}
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/content_main"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/app_bar_main"
tools:context="com.example.faksu.myapplication.MainActivity">
<!-- android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"-->
<WebView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/webView" />
</FrameLayout>
下面是app_bar_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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:fitsSystemWindows="true"
tools:context="com.example.faksu.myapplication.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
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>
<include layout="@layout/content_main"/>
</android.support.design.widget.CoordinatorLayout>
添加片段是什么意思?你在把碎片添加到backbackback中,这是什么意思?“但它只添加了旧片段。”我无法添加图像。我的意思是旧片段仍在屏幕上,新片段与旧片段一起显示。这就像是添加而不是替换。我的屏幕上有两个片段。旧的是背景,新的是前景,你说在片段上添加是什么意思?你在把碎片添加到backbackback中,这是什么意思?“但它只添加了旧片段。”我无法添加图像。我的意思是旧片段仍在屏幕上,新片段与旧片段一起显示。这就像是添加而不是替换。我的屏幕上有两个片段。旧的是背景,新的是前景