ここでは、わかりやすいサンプルコードを示します。 まず)(OnCreateイベントでフラグメントとactivity_main.xmlの内容を置き換える:
FragmentManager fm = getFragmentManager();
fm.beginTransaction().replace(R.id.content_frame, new HomeFragment()).commit();
を次にonNavigationItemSelected(で他の人とこのHomeFragmentを置き換え):
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_home) {
// Handle the camera action
FragmentManager fm = getFragmentManager();
fm.beginTransaction().replace(R.id.content_frame, new HomeFragment()).commit();
} else if (id == R.id.nav_current_order) {
FragmentManager fm = getFragmentManager();
fm.beginTransaction().replace(R.id.content_frame, new MyCurrentOrderFragment(),"Tag").commit();
} else if (id == R.id.nav_received_images) {
FragmentManager fm = getFragmentManager();
fm.beginTransaction().replace(R.id.content_frame, new HistoryFragment()).commit();
} else if (id == R.id.nav_rate_app) {
FragmentManager fm = getFragmentManager();
fm.beginTransaction().replace(R.id.content_frame, new RateOurAppFragment()).commit();
} else if (id == R.id.nav_help) {
FragmentManager fm = getFragmentManager();
fm.beginTransaction().replace(R.id.content_frame, new HelpFragment()).commit();
} else if (id == R.id.nav_settings) {
FragmentManager fm = getFragmentManager();
fm.beginTransaction().replace(R.id.content_frame, new SettingsFragment()).commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
はあなたのcontent_main.xml中でframeLayoutを追加し、 idを "content_frame"に設定してください
<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="bd.com.chalo.dsstudio.MainActivity"
tools:showIn="@layout/app_bar_main">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:visibility="gone" />
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"></FrameLayout>
</RelativeLayout>
ビューを追加しましたか? –