我尝试用导航抽屉将片段替换为另一个片段。但它只会增加旧片段。我研究了网站问题,但我仍然没有解决这个问题。它就像片段重叠一样。这是我的代码;
MainActivity;
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>