Java tutorial
/* * Copyright (C) 2015 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.martin.designlibrary; import android.os.Bundle; import android.support.design.widget.NavigationView; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.AppCompatActivity; import android.view.Menu; import android.view.MenuItem; import android.view.View; public class MainActivity extends AppCompatActivity { private DrawerLayout mDrawerLayout; private int mSelected, mCurrent; private NavigationView mNavigationView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (savedInstanceState != null) { mSelected = savedInstanceState.getInt("mSelected"); mCurrent = savedInstanceState.getInt("mCurrent"); } else { mCurrent = -1; mSelected = 0; } mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); mDrawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() { @Override public void onDrawerSlide(View drawerView, float slideOffset) { } @Override public void onDrawerOpened(View drawerView) { } @Override public void onDrawerStateChanged(int newState) { } @Override public void onDrawerClosed(View drawerView) { updateContainer(); } }); mNavigationView = (NavigationView) findViewById(R.id.nav_view); if (mNavigationView != null) { setupDrawerContent(mNavigationView); } selectDrawerItem(mSelected); } private void selectDrawerItem(int i) { mNavigationView.getMenu().getItem(i).setChecked(true); updateContainer(); } private void updateContainer() { FragmentManager fragmentManager = getSupportFragmentManager(); Fragment fragment = null; if (mSelected != mCurrent) { switch (mSelected) { case 0: fragment = TabFragment.newInstance(); break; case 1: fragment = CollapsingFragment.newInstance(); break; } mCurrent = mSelected; fragmentManager.beginTransaction().replace(R.id.container, fragment).commit(); } } private void setupDrawerContent(final NavigationView navigationView) { navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(MenuItem menuItem) { switch (menuItem.getItemId()) { case R.id.nav_home: mSelected = 0; break; case R.id.nav_messages: mSelected = 1; break; } menuItem.setChecked(true); mDrawerLayout.closeDrawers(); return true; } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: mDrawerLayout.openDrawer(GravityCompat.START); return true; } return super.onOptionsItemSelected(item); } @Override protected void onSaveInstanceState(Bundle outState) { outState.putInt("mSelected", mSelected); outState.putInt("mCurrent", mCurrent); super.onSaveInstanceState(outState); } }