Back to project page Android-Navigation-Drawer-with-Tabs.
The source code is released under:
Apache License
If you think the Android project Android-Navigation-Drawer-with-Tabs listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.example.navigationdrawertest; /* w ww . j a va 2 s .c o m*/ import android.content.res.Configuration; import android.os.Bundle; import android.support.v4.app.ActionBarDrawerToggle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.widget.DrawerLayout; import android.view.Menu; import android.view.View; import android.view.WindowManager; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; public class MainActivity extends FragmentActivity { private DrawerLayout mDrawerLayout; private ListView mLeftDrawer; private ActionBarDrawerToggle mDrawerToggle; private CharSequence mDrawerTitle; private CharSequence mTitle; private String[] mFragmentTitles; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); mTitle = mDrawerTitle = getTitle(); mFragmentTitles = getResources().getStringArray(R.array.fragments); mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer); mLeftDrawer = (ListView) findViewById(R.id.left_drawer); mLeftDrawer.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mFragmentTitles)); mLeftDrawer.setOnItemClickListener(new DrawerItemClickListener()); getActionBar().setDisplayHomeAsUpEnabled(true); getActionBar().setHomeButtonEnabled(true); mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) { @Override public void onDrawerClosed(View v) { getActionBar().setTitle(mTitle); supportInvalidateOptionsMenu(); } @Override public void onDrawerOpened(View v) { getActionBar().setTitle(mDrawerTitle); supportInvalidateOptionsMenu(); } }; mDrawerLayout.setDrawerListener(mDrawerToggle); if (savedInstanceState == null) { selectItem(0); } } @Override protected void onResume() { super.onResume(); getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); } @Override protected void onDestroy() { super.onDestroy(); getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); } @Override protected void onStop() { super.onStop(); getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); } @SuppressWarnings("unused") @Override public boolean onPrepareOptionsMenu(Menu menu) { boolean drawerOpen = mDrawerLayout.isDrawerOpen(mLeftDrawer); return super.onPrepareOptionsMenu(menu); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } private class DrawerItemClickListener implements ListView.OnItemClickListener { @Override public void onItemClick(AdapterView<?> parent, View v, int position, long id) { selectItem(position); } } private void selectItem(int position) { Fragment newFragment = new FragmentMain(); FragmentManager fm = getSupportFragmentManager(); switch (position) { case 0: newFragment = new FragmentMain(); break; case 1: newFragment = new FragmentMain2(); break; } fm.beginTransaction().replace(R.id.content_frame, newFragment).commit(); mLeftDrawer.setItemChecked(position, true); setTitle(mFragmentTitles[position]); mDrawerLayout.closeDrawer(mLeftDrawer); } @Override public void setTitle(CharSequence title) { mTitle = title; getActionBar().setTitle(title); } @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); mDrawerToggle.syncState(); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); mDrawerToggle.onConfigurationChanged(newConfig); } }