基于Android官方DrawerLayout实现抽屉导航菜单



基于Android官方DrawerLayout实现抽屉导航菜单


我之前写了一篇文章,基于第三方开源框架Android SlidingMenu实现抽屉导航菜单,文章链接地址:http://blog.csdn.net/zhangphil/article/details/44078805
事实上实现Android抽屉导航菜单的技术选型方案不少,其中还有一种方案就是基于Android官方的DrawerLayout。
如图1,图2所示就是基于Android官方DrawerLayout实现抽屉导航菜单。
图1


图2


Android DrawerLayout是Android在android-support-v4中引入的一个新的View组件,其本身继承自ViewGroup。使用DrawerLayout首先需要在项目代码中引入android-support-v4.jar包,好在现在基本上android-support-v4.jar包在Eclipse工程中已经默认添加。
开始写DrawerLayout代码实现抽屉导航菜单。
(1) 首先写一个布局文件activity_main.xml。

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="100dip"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:background="#42a5f5"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="5dip" />

</android.support.v4.widget.DrawerLayout>

在这本布局文件中,最上面的android:id="@+id/content_frame"的FrameLayout即为要打算盛放具体页面和内容容器。
而android:id="@+id/left_drawer"的ListView即为将要滑入滑出的导航菜单。为此ListView设置android:layout_gravity="left",表示告知Android系统导航菜单要从左边滑入滑出。如果为“right”则表示要从右边滑入滑出。


(2)布局文件写完后,接下来就可以写代码实现。

package zhangphil.navigationdrawer;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	private static Activity activity;

	private DrawerLayout mDrawerLayout;
	private ListView mDrawerList;
	private ActionBarDrawerToggle mDrawerToggle;

	private CharSequence mDrawerTitle;
	private CharSequence mTitle;
	private static String[] mTitles = { "A", "B", "C", "D", "E" };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		activity = this;
		setContentView(R.layout.activity_main);

		mTitle = mDrawerTitle = this.getTitle();

		mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
		mDrawerList = (ListView) findViewById(R.id.left_drawer);

		// mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
		// GravityCompat.START);

		mDrawerList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mTitles));
		mDrawerList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				selectItem(position);
			}
		});

		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 view) {
				getActionBar().setTitle(mTitle);
				Toast.makeText(activity, "抽屉关闭", Toast.LENGTH_SHORT).show();
				// invalidateOptionsMenu();
			}

			@Override
			public void onDrawerOpened(View drawerView) {
				getActionBar().setTitle(mDrawerTitle);
				Toast.makeText(activity, "抽屉打开", Toast.LENGTH_SHORT).show();
				// invalidateOptionsMenu();
			}
		};

		mDrawerLayout.setDrawerListener(mDrawerToggle);

		if (savedInstanceState == null) {
			selectItem(0);
		}
	}

	private void selectItem(int position) {
		Fragment fragment = new TextFragment();
		Bundle args = new Bundle();
		args.putInt(TextFragment.NUMBER, position);
		fragment.setArguments(args);

		FragmentManager fragmentManager = getFragmentManager();
		fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

		mDrawerList.setItemChecked(position, true);
		setTitle(mTitles[position]);
		mDrawerLayout.closeDrawer(mDrawerList);
	}

	@Override
	public void setTitle(CharSequence title) {
		mTitle = title;
		getActionBar().setTitle(mTitle);
	}

	// @Override
	// public boolean onCreateOptionsMenu(Menu menu) {
	// return super.onCreateOptionsMenu(menu);
	// }

	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);
		mDrawerToggle.syncState();
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		mDrawerToggle.onConfigurationChanged(newConfig);
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		// boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (mDrawerToggle.onOptionsItemSelected(item)) {
			return true;
		}

		return super.onOptionsItemSelected(item);
	}

	public static class TextFragment extends Fragment {

		public static final String NUMBER = "number";

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

			int i = getArguments().getInt(NUMBER);
			String title = mTitles[i];

			TextView text = new TextView(activity);
			text.setText("内容:" + title);
			text.setTextSize(50.0f);
			text.setGravity(Gravity.CENTER);

			activity.setTitle("标题:" + title);

			return text;
		}
	}
}

DrawerLayout可以设置一个监听事件类setDrawerListener(new ActionBarDrawerToggle())监听抽屉导航菜单的打开、关闭状态。


我将此例的全部工程代码压缩成一个zip文件,上传到CSDN供有兴趣的朋友下载,下载链接:http://download.csdn.net/detail/zhangphil/9136213

此压缩包下载后直接作为Android Eclipse的工程导入即可跑起来。

©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页