title: Set Fragments with TabLayout and Navigation Drawer
body: <p>My project has a NavigationDrawer and a TabLayout. I'm just looking for setting a specific fragment in a specific menu in a specific tab.</p>
  
  <p>I mean, almost each menu item has 3 differents tabs (not specially the same) and I want to set a fragment who will be different in each tab for each menu item.</p>
  
  <p>The 3 tabs are the same in each menu item.</p>
  
  <p>What I have to do ? I though of about setContentView() but I cannot make it work properly.</p>
  
  <p>Here is screenshots of my app:</p>
  
  <p><a href="http://i.stack.imgur.com/9kYxH.png" rel="nofollow">Navigation Drawer with all menu items</a></p>
  
  <p><a href="http://i.stack.imgur.com/ZDP20.png" rel="nofollow">Sliding Tabs of a menu item</a></p>
  
  <p>Here is my MainActivity.java:</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity
      implements NavigationView.OnNavigationItemSelectedListener {
  
  /**
   * ATTENTION: This was auto-generated to implement the App Indexing API.
   * See https://g.co/AppIndexing/AndroidStudio for more information.
   */
  private GoogleApiClient client;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);
  
      // Get the ViewPager and set it's PagerAdapter so that it can display items
      ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
      viewPager.setAdapter(new SampleFragmentPagerAdapter(getSupportFragmentManager(),
              MainActivity.this));
  
      // Give the TabLayout the ViewPager
      TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
      tabLayout.setupWithViewPager(viewPager);
  
      findViewById(R.id.viewpager).setVisibility(View.GONE);
      findViewById(R.id.sliding_tabs).setVisibility(View.GONE);
  
  
      FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
      fab.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              Snackbar.make(view, "Pourquoi tu touches à ce bouton ?", Snackbar.LENGTH_SHORT)
                      .setAction("Action", null).show();
          }
      });
  
      /*
          Début Drawer Menu
      */
      DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
      ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
              this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
      drawer.setDrawerListener(toggle);
      toggle.syncState();
  
      NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
      navigationView.setNavigationItemSelectedListener(this);
      // ATTENTION: This was auto-generated to implement the App Indexing API.
      // See https://g.co/AppIndexing/AndroidStudio for more information.
      client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
      /*
          Fin Drawer Menu
      */
  }
  
  @Override
  public void onBackPressed() {
      DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
      if (drawer.isDrawerOpen(GravityCompat.START)) {
          drawer.closeDrawer(GravityCompat.START);
      } else {
          super.onBackPressed();
      }
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      int id = item.getItemId();
  
      //noinspection SimplifiableIfStatement
      if (id == R.id.action_settings) {
          return true;
      }
  
      return super.onOptionsItemSelected(item);
  }
  
  @SuppressWarnings("StatementWithEmptyBody")
  @Override
  public boolean onNavigationItemSelected(MenuItem item) {
      // Handle navigation view item clicks here.
      int id = item.getItemId();
  
  
      if (id == R.id.content_main) {
          findViewById(R.id.viewpager).setVisibility(View.GONE);
          findViewById(R.id.sliding_tabs).setVisibility(View.GONE);
      } else if (id == R.id.nav_equations) {
          findViewById(R.id.viewpager).setVisibility(View.VISIBLE);
          findViewById(R.id.sliding_tabs).setVisibility(View.VISIBLE);
      } else if (id == R.id.nav_lois) {
          findViewById(R.id.viewpager).setVisibility(View.VISIBLE);
          findViewById(R.id.sliding_tabs).setVisibility(View.VISIBLE);
      } else if (id == R.id.nav_stats) {
          findViewById(R.id.viewpager).setVisibility(View.VISIBLE);
          findViewById(R.id.sliding_tabs).setVisibility(View.VISIBLE);
      } else if (id == R.id.nav_tests) {
          findViewById(R.id.viewpager).setVisibility(View.VISIBLE);
          findViewById(R.id.sliding_tabs).setVisibility(View.VISIBLE);
      } else if (id == R.id.nav_partager) {
  
      } else if (id == R.id.nav_parametres) {
  
      }
  
      DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
      drawer.closeDrawer(GravityCompat.START);
      return true;
  }
  
  @Override
  public void onStart() {
      super.onStart();
  
      // ATTENTION: This was auto-generated to implement the App Indexing API.
      // See https://g.co/AppIndexing/AndroidStudio for more information.
      client.connect();
      Action viewAction = Action.newAction(
              Action.TYPE_VIEW, // TODO: choose an action type.
              "Main Page", // TODO: Define a title for the content shown.
              // TODO: If you have web page content that matches this app activity's content,
              // make sure this auto-generated web page URL is correct.
              // Otherwise, set the URL to null.
              Uri.parse("http://host/path"),
              // TODO: Make sure this auto-generated app deep link URI is correct.
              Uri.parse("android-app://com.example.slabre.mathix/http/host/path")
      );
      AppIndex.AppIndexApi.start(client, viewAction);
  }
  
  @Override
  public void onStop() {
      super.onStop();
  
      // ATTENTION: This was auto-generated to implement the App Indexing API.
      // See https://g.co/AppIndexing/AndroidStudio for more information.
      Action viewAction = Action.newAction(
              Action.TYPE_VIEW, // TODO: choose an action type.
              "Main Page", // TODO: Define a title for the content shown.
              // TODO: If you have web page content that matches this app activity's content,
              // make sure this auto-generated web page URL is correct.
              // Otherwise, set the URL to null.
              Uri.parse("http://host/path"),
              // TODO: Make sure this auto-generated app deep link URI is correct.
              Uri.parse("android-app://com.example.slabre.mathix/http/host/path")
      );
      AppIndex.AppIndexApi.end(client, viewAction);
      client.disconnect();
  } }
  </code></pre>
  
  <p>This is the SampleFragmentPagerAdapter.java, used to manage the fragments but only on each tabs :</p>
  
  <pre><code>public class SampleFragmentPagerAdapter extends FragmentPagerAdapter {
      final int PAGE_COUNT = 3;
      private String tabTitles[] = new String[] { "Calcul", "Résultat", "Infos" };
      private Context context;
  
      public SampleFragmentPagerAdapter(FragmentManager fm, Context context) {
          super(fm);
          this.context = context;
      }
  
      @Override
      public int getCount() {
          return PAGE_COUNT;
      }
  
      @Override
      public Fragment getItem(int position) {
          return PageFragment.newInstance(position);
      }
  
      @Override
      public CharSequence getPageTitle(int position) {
          // Generate title based on item position
          return tabTitles[position];
      }
  }
  </code></pre>
  
  <p>And so the PageFragment.java :</p>
  
  <pre><code>public class PageFragment extends Fragment{
      public static final String ARG_PAGE = "ARG_PAGE";
  
      private int mPage;
  
      public static PageFragment newInstance(int page) {
          Bundle args = new Bundle();
          args.putInt(ARG_PAGE, page);
          PageFragment fragment = new PageFragment();
          fragment.setArguments(args);
          return fragment;
      }
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          mPage = getArguments().getInt(ARG_PAGE);
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          View view = inflater.inflate(R.layout.fragment_page, container, false);
          TextView textView = (TextView) view;
          textView.setText("Fragment #" + mPage);
          return view;
      }
  }
  </code></pre>
  
  <p>Finally this is the app_bar_main.xml included by the activity_main.xml :</p>
  
  <pre><code>&lt;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.slabre.mathix.MainActivity"&gt;
  
      &lt;android.support.design.widget.AppBarLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:theme="@style/AppTheme.AppBarOverlay"&gt;
  
          &lt;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" /&gt;
  
          &lt;android.support.design.widget.TabLayout
              android:id="@+id/sliding_tabs"
              style="@style/MyCustomTabLayout"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              app:tabMode="scrollable" /&gt;
  
          &lt;android.support.v4.view.ViewPager
              android:id="@+id/viewpager"
              android:layout_width="match_parent"
              android:layout_height="0px"
              android:layout_weight="1"
              android:background="@android:color/white" /&gt;
  
      &lt;/android.support.design.widget.AppBarLayout&gt;
  
  
      &lt;include layout="@layout/content_main" /&gt;
  
      &lt;android.support.design.widget.FloatingActionButton
          android:id="@+id/fab"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="bottom|end"
          android:layout_margin="@dimen/fab_margin"
          android:src="@drawable/ic_add_black_24dp" /&gt;
  
  &lt;/android.support.design.widget.CoordinatorLayout&gt;
  </code></pre>
  
  <p>I really don't know if you need more files, just ask me if you want more.</p>