侧拉菜单在android应用中非常常见,它的实现方式太多了,今天我们就说说使用Google提供的DrawerLayout来实现侧拉菜单效果,先来看张效果图:

DrawerLayout的实现其实非常简单,只要按照既有的规范来写即可,先来看看布局文件:

<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" > <RelativeLayout
android:id="@+id/fragment_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" > <RelativeLayout
android:id="@+id/title_bar"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#63B8FF" > <ImageView
android:id="@+id/leftmenu"
android:layout_width="48dp"
android:layout_height="48dp"
android:padding="12dp"
android:src="@drawable/menu" /> <TextView
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_centerInParent="true"
android:gravity="center"
android:text="Title Bar" /> <ImageView
android:id="@+id/rightmenu"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentRight="true"
android:padding="12dp"
android:src="@drawable/p_center" />
</RelativeLayout> <LinearLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/title_bar"
android:orientation="vertical" />
</RelativeLayout> <RelativeLayout
android:id="@+id/left"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="@android:color/white" > <ListView
android:id="@+id/left_listview"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</RelativeLayout> <RelativeLayout
android:id="@+id/right"
android:layout_width="260dp"
android:layout_height="match_parent"
android:layout_gravity="right"
android:background="#BCEE68" > <ImageView
android:id="@+id/p_pic"
android:layout_width="72dp"
android:layout_height="72dp"
android:layout_centerInParent="true"
android:src="@drawable/p_pic" /> <TextView
android:id="@+id/right_textview"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_below="@id/p_pic"
android:layout_centerHorizontal="true"
android:layout_marginTop="12dp"
android:text="个人中心"
android:textColor="@android:color/black"
android:textSize="14sp" />
</RelativeLayout> </android.support.v4.widget.DrawerLayout>

首先,根布局就是DrawerLayout,在根布局之后又主要分为三大块,第一块就是我们主界面的内容,第二块是左边拉出来的布局,第三块是右边拉出来的布局(不需要右边侧拉就不用写,这样的话整个布局就只分为两大块),那么系统怎么知道我们这个布局是主布局还是侧拉菜单的布局?请注意左边侧拉菜单布局android:layout_gravity="left"这个属性和右边菜单布局的android:layout_gravity="right"这个属性,哈哈,这下应该明白了吧,系统通过layout_gravity属性的值来判断这个布局是左边菜单的布局还是右边菜单的布局,如果没有这个属性,那不用说,肯定是主界面的布局。

好了,布局文件写好之后,我们的侧拉效果其实就已经可以实现了,但是你发现左边拉出来什么东西都没有,那是因为还没有数据,所以我们要在MainActivity中初始化左边布局的ListView,给ListView赋值这个想必大家都会,我就直接贴代码了:

listView = (ListView) findViewById(R.id.left_listview);
initData();
adapter = new ContentAdapter(this, list);
listView.setAdapter(adapter);

初始化数据的方法:

	private void initData() {
list = new ArrayList<ContentModel>(); list.add(new ContentModel(R.drawable.doctoradvice2, "新闻", 1));
list.add(new ContentModel(R.drawable.infusion_selected, "订阅", 2));
list.add(new ContentModel(R.drawable.mypatient_selected, "图片", 3));
list.add(new ContentModel(R.drawable.mywork_selected, "视频", 4));
list.add(new ContentModel(R.drawable.nursingcareplan2, "跟帖", 5));
list.add(new ContentModel(R.drawable.personal_selected, "投票", 6));
}

ContentModel类:

public class ContentModel {

	private int imageView;
private String text;
private int id; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public ContentModel() {
} public ContentModel(int imageView, String text, int id) {
this.imageView = imageView;
this.text = text;
this.id = id;
} public int getImageView() {
return imageView;
} public void setImageView(int imageView) {
this.imageView = imageView;
} public String getText() {
return text;
} public void setText(String text) {
this.text = text;
} }

数据适配器:

public class ContentAdapter extends BaseAdapter {

	private Context context;
private List<ContentModel> list; public ContentAdapter(Context context, List<ContentModel> list) {
super();
this.context = context;
this.list = list;
} @Override
public int getCount() {
if (list != null) {
return list.size();
}
return 0;
} @Override
public Object getItem(int position) {
if (list != null) {
return list.get(position);
}
return null;
} @Override
public long getItemId(int position) {
return list.get(position).getId();
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHold hold;
if (convertView == null) {
hold = new ViewHold();
convertView = LayoutInflater.from(context).inflate(
R.layout.content_item, null);
convertView.setTag(hold);
} else {
hold = (ViewHold) convertView.getTag();
} hold.imageView = (ImageView) convertView
.findViewById(R.id.item_imageview);
hold.textView = (TextView) convertView.findViewById(R.id.item_textview); hold.imageView.setImageResource(list.get(position).getImageView());
hold.textView.setText(list.get(position).getText());
return convertView;
} class ViewHold {
public ImageView imageView;
public TextView textView;
} }

每个Item的布局文件:

<?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="horizontal"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:gravity="center"> <ImageView
android:id="@+id/item_imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"/> <TextView
android:id="@+id/item_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="aa"
android:textSize="20dp"/> </LinearLayout>

好了,给左边的侧拉菜单中的ListView赋值之后,在把它拉出来,这下就全都有数据了,但是我除了想通过滑动让侧拉菜单出来之外,我还希望在App的标题栏上有一个按钮,点击之后左边的侧拉菜单也会出来,这要怎么实现?看代码:

先将原有的标题栏隐藏:

getActionBar().hide();

然后:

leftMenu = (ImageView) findViewById(R.id.leftmenu);
leftMenu.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
drawerLayout.openDrawer(Gravity.LEFT);
}
});

其实很简单,直接调用DrawerLayout的openDrawer方法,参数传Gravity.LEFT表示让左边的侧拉菜单出来,参数如果传Gravity.RIGHT,则表示让右边的侧拉菜单出来。好了,当左边的侧拉菜单出来之后,我希望点击菜单的每一个item,主界面都会有所反应,即当我点击“新闻”,主界面显示新闻内容,当我点击”订阅“,主界面显示订阅的内容,这个也很好实现,首先,点击事件不用说,就是ListView的setOnItemClickListener,点击之后,我们的主界面会显示相应的Fragment,即,如果点击了新闻,则注解卖弄显示新闻的Fragment,如果点击了订阅,则主界面显示订阅的Fragment,看代码:

		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
FragmentTransaction bt = fm.beginTransaction();
switch ((int) id) {
case 1:
bt.replace(R.id.content, new NewsFragment());
break;
case 2:
bt.replace(R.id.content, new SubscriptionFragment());
break; default:
break;
}
bt.commit();
drawerLayout.closeDrawer(Gravity.LEFT);
}
});

每次点击之后,就用相应的Fragment替换主界面的LinearLayout,当然,替换完成之后要记得关闭左边的侧拉菜单,传入的参数为Gravity.LEFT表示关闭左边的侧拉菜单,如果传入的菜单为Gravity.RIGHT表示关闭右边的侧拉菜单。这里两个Fragment都很简单,我就不贴源码了,大家一会直接下载Demo看。

右边的侧拉菜单和左边一样,我就不赘述了,大家有什么问题欢迎留言讨论。

本文参考:

1.Android 抽屉效果的导航菜单实现

2.Android 使用Drawerlayout仿网易新闻客户端抽屉模式

Demo下载https://github.com/lenve/DrawerLayout

使用DrawerLayout实现侧拉菜单的更多相关文章

  1. 使用DrawerLayout实现QQ5.0侧拉菜单效果

    在上一篇文章中,我们介绍了怎么使用DrawerLayout来实现一个简单的侧拉菜单(使用DrawerLayout实现侧拉菜单),也就是我们常说的抽屉效果,GitHub上类似效果的实现方式非常多,实现出 ...

  2. DragLayout: QQ5.0侧拉菜单的新特效

    一.项目概要 1.1 项目效果如图: 1.2 需要使用到的技术   ViewDragHelper: 要实现和QQ5.0侧滑的特效,需要借助谷歌在2013年I/O大会上发布的ViewDragHelper ...

  3. android 5.X Toolbar+DrawerLayout实现抽屉菜单

    前言  android5.X新增的一个控件Toolbar,这个控件比ActionBar更加自由,可控,因为曾经的ActionBar的灵活性比較差,所以google逐渐使用Toolbar替代Action ...

  4. 仿qq的侧拉菜单效果

    自定义控件 import android.animation.ArgbEvaluator; import android.animation.FloatEvaluator; import androi ...

  5. Android5.0之NavigationView的使用

    导航菜单的制作方式多种多样,网上也有各种炫酷效果的具体实现方式,那么今天我主要是想来说说Google在Android5.0之后推出的NavigationView的具体使用方式. NavigationV ...

  6. [UI]抽屉菜单DrawerLayout分析(一)

    本文转载于:http://www.cnblogs.com/avenwu/archive/2014/04/16/3669367.html 侧拉菜单作为常见的导航交互控件,最开始在没有没有android官 ...

  7. AndroidUI 侧滑菜单 DrawerLayout的使用

    直接上代码: activity_main.xml: <android.support.v4.widget.DrawerLayout xmlns:android="http://sche ...

  8. android 应用架构随笔五(ActionBar与侧滑菜单DrawerLayout)

    ActionBar(V7)的添加非常简单,只需要在AndroidManifest.xml中指定Application或Activity的theme是Theme.Holo或其子类就可以了,在Androi ...

  9. android L 新控件侧滑菜单DrawerLayout 使用教程

    介绍 drawerLayout是Support Library包中实现了侧滑菜单效果的控件,可以说drawerLayout是因为第三方控件如MenuDrawer等的出现之后,google借鉴而出现的产 ...

随机推荐

  1. Android 常用UI控件之TabHost(3)在4.0不显示图标的解决方案

    1,自定义 TabWidget 上每个tab的view 2,用多个图片

  2. Toad for Oracle 12 download link

    Toad for Oracle 12 download link x64-bit http://us-downloads.quest.com/Repository/support.quest.com/ ...

  3. BZOJ_1611_[Usaco2008_Feb]_Meteor_Shower流星雨_(bfs)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1611 网格图起始位置(0,0),不同时间会有流星落下,导致之后的时间里,该点以及周围四个点都不 ...

  4. 五个你可能闻所未闻的出色的Ubuntu替代发行版

      你在使用Ubuntu,可是希望桌面体验……来得更眩目一点.虽说你总是可以添加新的桌面背景,或者索性切换桌面,但是你还有这个选择:换成一种全然不同的发行版. 本文就介绍了五个极其出色的Ubuntu替 ...

  5. POJ 2253 Frogger(最小生成树)

    青蛙跳跃,题意大概是:青蛙从起点到终点进行一次或多次的跳跃,多次跳跃中肯定有最大的跳跃距离.求在所有的跳跃中,最小的最大跳跃距离SF-_-(不理解?看题目吧). 可以用最小生成树完成.以起点为根,生成 ...

  6. 【转】下载太慢?简单设置让iTunes提速十几倍

    原文网址:http://www.startos.com/mac/ipad/tips/2010120713291.html 今年可以说是苹果欢笑的一年,ipad的发布,iphone4的成功,让用苹果设备 ...

  7. [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.1.3

    Use the QR decomposition to prove Hadamard's inequality: if $X=(x_1,\cdots,x_n)$, then $$\bex |\det ...

  8. HDU-1238 Substrings

    Substrings Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...

  9. Windows平台网站图片服务器架构的演进[转]

    构建在Windows平台之上的网站,往往会被业内众多架构师认为很“保守”.很大部分原因,是由于微软技术体系的封闭和部分技术人员的短视造成 的.由于长期缺乏开源支持,所以只能“闭门造车”,这样很容易形成 ...

  10. flume服务的搭建

    搭建前先统一时间,关闭防火墙,使用的jar包版本是1.6.0的 服务配置有两种方式 第一种:具体步骤如下: 1.将jar包传至node1上,解压至根目录 2.更改目录名,使用如下命令:mv apach ...