MainActivity.java
package com.ct9_navigationdrawer;
import java.util.Locale;
import android.os.Bundle;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.res.Configuration;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
public class MainActivity extends Activity {
// 抽屉菜单跟控件
private DrawerLayout mDrawerLayout;
// 抽屉菜单左边菜单
private ListView mDrawerList;
// 抽屉菜单应用图标指示抽屉开关
private ActionBarDrawerToggle mDrawerToggle;
private String mDrawerTitle;
//抽屉左边菜单的数组集合
private static String[] mPlanetTitle;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViews();
setListener();
initdata();
if(savedInstanceState==null){
selectItem(0);
}
}
private void findViews() {
// TODO Auto-generated method stub
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerlayout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, 1,
1){
@Override
public void onDrawerClosed(View drawerView) {
// TODO Auto-generated method stub
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu();
}
@Override
public void onDrawerOpened(View drawerView) {
// TODO Auto-generated method stub
super.onDrawerOpened(drawerView);
invalidateOptionsMenu();
}
};
}
private void setListener() {
// TODO Auto-generated method stub
mDrawerList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
selectItem(position);
}
});
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
public void selectItem(int position) {
// TODO Auto-generated method stub
Fragment fragment=new PlanetFragment();
Bundle bundle=new Bundle();
bundle.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
fragment.setArguments(bundle);
//事物提交
FragmentTransaction manager=getFragmentManager().beginTransaction();
manager.replace(R.id.frame_content, fragment);
manager.commit();
mDrawerList.setItemChecked(position, true);
setTitle(mPlanetTitle[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
private void initdata(){
mDrawerTitle=getTitle().toString();//菜单标题
//mPlanetTitle=new String[]{"水星","金星","地球","火星","木星","土星","天王星","海王星"};// 左边菜单数据集合
// 左边菜单数据集合
mPlanetTitle = getResources().getStringArray(R.array.planets_array);
//设置拉出导航菜单时阴影,官方示例不明显,可把图片背景修改一下
mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item,mPlanetTitle));
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
}
@Override
public void setTitle(CharSequence title) {
// TODO Auto-generated method stub
mDrawerTitle=title.toString();
getActionBar().setTitle(mDrawerTitle);
}
public static class PlanetFragment extends Fragment{
public static final String ARG_PLANET_NUMBER="planet_number";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view=inflater.inflate(R.layout.fragment_planet, container,false);
int i=getArguments().getInt(ARG_PLANET_NUMBER);
String planet=mPlanetTitle[i];
int imgId=getResources().getIdentifier(planet.toLowerCase(Locale.getDefault()), "drawable", getActivity().getPackageName());
ImageView img=(ImageView) view.findViewById(R.id.image);
img.setImageResource(imgId);
getActivity().setTitle(planet);
return view;
}
}
@Override
public boolean onMenuOpened(int featureId, Menu menu) {
// TODO Auto-generated method stub
if(mDrawerLayout.isDrawerOpen(mDrawerList)){
mDrawerLayout.closeDrawer(mDrawerList);
}else{
mDrawerLayout.openDrawer(mDrawerList);//当打开菜单时
}
return super.onMenuOpened(featureId, menu);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
activity_main.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawerlayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="@+id/frame_content"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="@+id/left_drawer"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="1dp" />
</android.support.v4.widget.DrawerLayout>
fragment_planet.xml
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:gravity="center"
android:padding="32dp" />
drawer_list_item.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textColor="#fff"
android:background="?android:attr/activatedBackgroundIndicator"
android:minHeight="?android:attr/listPreferredItemHeightSmall"/>
strings.xml
<resources>
<string name="app_name">com.ct9_navigationdrawer</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
<string name="drawer_open">Open navigation drawer</string>
<string name="drawer_close">Close navigation drawer</string>
<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
<item>Jupiter</item>
<item>Saturn</item>
<item>Uranus</item>
<item>Neptune</item>
</string-array>
</resources>
要注意的是在drawable下放几张图片如图: