android navigationdrawer抽屉菜单

阅读更多
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(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


    
    
    




fragment_planet.xml




drawer_list_item.xml




strings.xml



    com.ct9_navigationdrawer
    Hello world!
    Settings
    MainActivity
    Open navigation drawer
    Close navigation drawer
    
        Mercury
        Venus
        Earth
        Mars
        Jupiter
        Saturn
        Uranus
        Neptune
    




要注意的是在drawable下放几张图片如图:


android navigationdrawer抽屉菜单_第1张图片


  • android navigationdrawer抽屉菜单_第2张图片
  • 大小: 6.1 KB
  • com.ct9_navigationdrawer.rar (2.2 MB)
  • 下载次数: 6
  • 查看图片附件

你可能感兴趣的:(抽屉菜单,navigation)