Android抽屉菜单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" > <!-- The main content view --> <FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout> <!-- The navigation view --> <ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#ffffcc"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp" >
</ListView> </android.support.v4.widget.DrawerLayout>

fragment_content.xml

<?

xml version="1.0" encoding="utf-8"?

>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25sp" /> </LinearLayout>

(2)主要类文件

package com.xuliugen.drawerlayout;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView; public class MainActivity extends Activity implements OnItemClickListener { private DrawerLayout mDrawerLayout; // 设置的是左側的抽屉菜单
private ListView mDrawerList;
private ArrayList<String> menuLists;
private ArrayAdapter<String> adapter;
private ActionBarDrawerToggle mDrawerToggle;// actionBar打开关闭的
private String mTitle; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); mTitle = (String) getTitle(); mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
menuLists = new ArrayList<String>();
for (int i = 0; i < 5; i++) {
menuLists.add("item" + i);
}
// 初始化适配器
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, menuLists);
// 为左側的抽屉设置了数据
mDrawerList.setAdapter(adapter); // 左側滑动菜单的监听事件
mDrawerList.setOnItemClickListener(this); // 设置抽屉被打开关闭的对象
mDrawerToggle = new ActionBarDrawerToggle(this, //
mDrawerLayout,//
R.drawable.ic_drawer,//
R.string.drawer_open,//
R.string.drawer_close) {
// 被打开的时候
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getActionBar().setTitle("请选择"); // 设置actionBar的文字
invalidateOptionsMenu(); // Call onPrepareOptionsMenu()
} // 被关闭的时候
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
getActionBar().setTitle(mTitle);
invalidateOptionsMenu();// 又一次绘制actionBar上边的菜单项
}
}; // 设置滑动菜单的 打开关闭事件
mDrawerLayout.setDrawerListener(mDrawerToggle); // 开启ActionBar上APP ICON的功能:点击打开和点击关闭7
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true); } @Override
public boolean onPrepareOptionsMenu(Menu menu) {
boolean isDrawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.action_websearch).setVisible(!isDrawerOpen);
return super.onPrepareOptionsMenu(menu);
} /**
* 菜单项的设置
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
} /**
* 设置actionBar上边图标的点击事件
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// 将ActionBar上的图标与Drawer结合起来
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
switch (item.getItemId()) {
case R.id.action_websearch:
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
Uri uri = Uri.parse("http://blog.csdn.net/xlgen157387");
intent.setData(uri);
startActivity(intent);
break;
}
return super.onOptionsItemSelected(item);
} /**
* 依据官方文档提示的信息
*
* 将mDrawerToggle.syncState();放入到onPostCreate中
*/
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// 须要将ActionDrawerToggle与DrawerLayout的状态同步
// 将ActionBarDrawerToggle中的drawer图标,设置为ActionBar中的Home-Button的Icon
mDrawerToggle.syncState();
} /**
* 当屏幕发生选装的时候也须要进行对应的设置
*/
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
} /**
* 监听事件的实现
*
*
* 当点击菜单条中的item的时候切换对应的fragment界面
*/
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// 动态插入一个Fragment到FrameLayout其中
Fragment contentFragment = new ContentFragment();
Bundle bundle = new Bundle();
bundle.putString("text", menuLists.get(position));
contentFragment.setArguments(bundle); // fragment创建好了之后须要交给fragmentManager来替换到对应的视图中
FragmentManager fm = getFragmentManager();
fm.beginTransaction().replace(R.id.content_frame, contentFragment)
.commit(); mDrawerLayout.closeDrawer(mDrawerList);
} }

ContentFragment.java

package com.xuliugen.drawerlayout;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView; /**
* 用于填充界面的fragment
*
* @author xuliugen
*
*/
public class ContentFragment extends Fragment { private TextView textView; @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_content, container,false);
textView = (TextView) view.findViewById(R.id.textView); // 获得传入的參数
String text = getArguments().getString("text");
textView.setText(text); return view;
} }

(3)项目演示效果

Android抽屉菜单DrawerLayout的实现案例

method=Preview.outputPic&qid=228182871&fname=/CSDNgif%E5%9B%BE%E7%89%87/DrawerLayoutUsing/11.gif&fhash=23a2b5e84531316de1c64077841400405c7e6ff7&dt=63.c6b3499d4530681069aa12466ae4d94e&v=1.0.1&rtick=14262974591665&open_app_id=0&devtype=web&sign=5314321d43eb0e79dbb558b463d09786&" alt="这里写图片描写叙述" title="">

(4)项目源码和Google參考文档下载:http://yunpan.cn/cZZ7RVRY96yWe (提取码:2981)

上一篇:Android抽屉(SlidingDrawer --类似android通知栏下拉效果)


下一篇:Android 抽屉导航