DrawerLayout来实现侧拉菜单效果

来源:互联网 发布:微信刷流量软件 编辑:程序博客网 时间:2024/04/27 19:43

DrawerLayout来实现侧拉菜单效果,先来看张效果图:



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

[java] view plaincopyprint?
  1. <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:id="@+id/drawerlayout"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent" >  
  6.   
  7.     <RelativeLayout  
  8.         android:id="@+id/fragment_layout"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="match_parent" >  
  11.   
  12.         <RelativeLayout  
  13.             android:id="@+id/title_bar"  
  14.             android:layout_width="match_parent"  
  15.             android:layout_height="48dp"  
  16.             android:background="#63B8FF" >  
  17.   
  18.             <ImageView  
  19.                 android:id="@+id/leftmenu"  
  20.                 android:layout_width="48dp"  
  21.                 android:layout_height="48dp"  
  22.                 android:padding="12dp"  
  23.                 android:src="@drawable/menu" />  
  24.   
  25.             <TextView  
  26.                 android:layout_width="wrap_content"  
  27.                 android:layout_height="48dp"  
  28.                 android:layout_centerInParent="true"  
  29.                 android:gravity="center"  
  30.                 android:text="Title Bar" />  
  31.   
  32.             <ImageView  
  33.                 android:id="@+id/rightmenu"  
  34.                 android:layout_width="48dp"  
  35.                 android:layout_height="48dp"  
  36.                 android:layout_alignParentRight="true"  
  37.                 android:padding="12dp"  
  38.                 android:src="@drawable/p_center" />  
  39.         </RelativeLayout>  
  40.   
  41.         <LinearLayout  
  42.             android:id="@+id/content"  
  43.             android:layout_width="match_parent"  
  44.             android:layout_height="match_parent"  
  45.             android:layout_below="@id/title_bar"  
  46.             android:orientation="vertical" />  
  47.     </RelativeLayout>  
  48.   
  49.     <RelativeLayout  
  50.         android:id="@+id/left"  
  51.         android:layout_width="200dp"  
  52.         android:layout_height="match_parent"  
  53.         android:layout_gravity="left"  
  54.         android:background="@android:color/white" >  
  55.   
  56.         <ListView  
  57.             android:id="@+id/left_listview"  
  58.             android:layout_width="match_parent"  
  59.             android:layout_height="match_parent" >  
  60.         </ListView>  
  61.     </RelativeLayout>  
  62.   
  63.     <RelativeLayout  
  64.         android:id="@+id/right"  
  65.         android:layout_width="260dp"  
  66.         android:layout_height="match_parent"  
  67.         android:layout_gravity="right"  
  68.         android:background="#BCEE68" >  
  69.   
  70.         <ImageView  
  71.             android:id="@+id/p_pic"  
  72.             android:layout_width="72dp"  
  73.             android:layout_height="72dp"  
  74.             android:layout_centerInParent="true"  
  75.             android:src="@drawable/p_pic" />  
  76.   
  77.         <TextView  
  78.             android:id="@+id/right_textview"  
  79.             android:layout_width="wrap_content"  
  80.             android:layout_height="48dp"  
  81.             android:layout_below="@id/p_pic"  
  82.             android:layout_centerHorizontal="true"  
  83.             android:layout_marginTop="12dp"  
  84.             android:text="个人中心"  
  85.             android:textColor="@android:color/black"  
  86.             android:textSize="14sp" />  
  87.     </RelativeLayout>  
  88.   
  89. </android.support.v4.widget.DrawerLayout>  

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


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

[java] view plaincopyprint?
  1. listView = (ListView) findViewById(R.id.left_listview);  
  2. initData();  
  3. adapter = new ContentAdapter(this, list);  
  4.         listView.setAdapter(adapter);  

初始化数据的方法:

[java] view plaincopyprint?
  1. private void initData() {  
  2.     list = new ArrayList<ContentModel>();  
  3.   
  4.     list.add(new ContentModel(R.drawable.doctoradvice2, "新闻"1));  
  5.     list.add(new ContentModel(R.drawable.infusion_selected, "订阅"2));  
  6.     list.add(new ContentModel(R.drawable.mypatient_selected, "图片"3));  
  7.     list.add(new ContentModel(R.drawable.mywork_selected, "视频"4));  
  8.     list.add(new ContentModel(R.drawable.nursingcareplan2, "跟帖"5));  
  9.     list.add(new ContentModel(R.drawable.personal_selected, "投票"6));  
  10. }  
ContentModel类:

[java] view plaincopyprint?
  1. public class ContentModel {  
  2.   
  3.     private int imageView;  
  4.     private String text;  
  5.     private int id;  
  6.       
  7.       
  8.     public int getId() {  
  9.         return id;  
  10.     }  
  11.   
  12.     public void setId(int id) {  
  13.         this.id = id;  
  14.     }  
  15.   
  16.     public ContentModel() {  
  17.     }  
  18.   
  19.     public ContentModel(int imageView, String text, int id) {  
  20.         this.imageView = imageView;  
  21.         this.text = text;  
  22.         this.id = id;  
  23.     }  
  24.   
  25.     public int getImageView() {  
  26.         return imageView;  
  27.     }  
  28.   
  29.     public void setImageView(int imageView) {  
  30.         this.imageView = imageView;  
  31.     }  
  32.   
  33.     public String getText() {  
  34.         return text;  
  35.     }  
  36.   
  37.     public void setText(String text) {  
  38.         this.text = text;  
  39.     }  
  40.   
  41. }  

数据适配器:

[java] view plaincopyprint?
  1. public class ContentAdapter extends BaseAdapter {  
  2.   
  3.     private Context context;  
  4.     private List<ContentModel> list;  
  5.   
  6.     public ContentAdapter(Context context, List<ContentModel> list) {  
  7.         super();  
  8.         this.context = context;  
  9.         this.list = list;  
  10.     }  
  11.   
  12.     @Override  
  13.     public int getCount() {  
  14.         if (list != null) {  
  15.             return list.size();  
  16.         }  
  17.         return 0;  
  18.     }  
  19.   
  20.     @Override  
  21.     public Object getItem(int position) {  
  22.         if (list != null) {  
  23.             return list.get(position);  
  24.         }  
  25.         return null;  
  26.     }  
  27.   
  28.     @Override  
  29.     public long getItemId(int position) {  
  30.         return list.get(position).getId();  
  31.     }  
  32.   
  33.     @Override  
  34.     public View getView(int position, View convertView, ViewGroup parent) {  
  35.         ViewHold hold;  
  36.         if (convertView == null) {  
  37.             hold = new ViewHold();  
  38.             convertView = LayoutInflater.from(context).inflate(  
  39.                     R.layout.content_item, null);  
  40.             convertView.setTag(hold);  
  41.         } else {  
  42.             hold = (ViewHold) convertView.getTag();  
  43.         }  
  44.   
  45.         hold.imageView = (ImageView) convertView  
  46.                 .findViewById(R.id.item_imageview);  
  47.         hold.textView = (TextView) convertView.findViewById(R.id.item_textview);  
  48.   
  49.         hold.imageView.setImageResource(list.get(position).getImageView());  
  50.         hold.textView.setText(list.get(position).getText());  
  51.         return convertView;  
  52.     }  
  53.   
  54.     class ViewHold {  
  55.         public ImageView imageView;  
  56.         public TextView textView;  
  57.     }  
  58.   
  59. }  

每个Item的布局文件:

[java] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="horizontal"   
  6.     android:paddingTop="20dp"  
  7.     android:paddingBottom="20dp"  
  8.     android:gravity="center">  
  9.       
  10.     <ImageView   
  11.         android:id="@+id/item_imageview"  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:src="@drawable/ic_launcher"/>  
  15.       
  16.     <TextView   
  17.         android:id="@+id/item_textview"  
  18.         android:layout_width="wrap_content"  
  19.         android:layout_height="wrap_content"  
  20.         android:text="aa"  
  21.         android:textSize="20dp"/>  
  22.   
  23. </LinearLayout>  


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

先将原有的标题栏隐藏:

[java] view plaincopyprint?
  1. getActionBar().hide();  

然后:

[java] view plaincopyprint?
  1. leftMenu = (ImageView) findViewById(R.id.leftmenu);  
  2. leftMenu.setOnClickListener(new OnClickListener() {  
  3.   
  4.             @Override  
  5.             public void onClick(View v) {  
  6.                 drawerLayout.openDrawer(Gravity.LEFT);  
  7.             }  
  8.         });  

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

[java] view plaincopyprint?
  1. listView.setOnItemClickListener(new OnItemClickListener() {  
  2.   
  3.     @Override  
  4.     public void onItemClick(AdapterView<?> parent, View view,  
  5.             int position, long id) {  
  6.         FragmentTransaction bt = fm.beginTransaction();  
  7.         switch ((int) id) {  
  8.         case 1:  
  9.             bt.replace(R.id.content, new NewsFragment());  
  10.             break;  
  11.         case 2:  
  12.             bt.replace(R.id.content, new SubscriptionFragment());  
  13.             break;  
  14.   
  15.         default:  
  16.             break;  
  17.         }  
  18.         bt.commit();  
  19.         drawerLayout.closeDrawer(Gravity.LEFT);  
  20.     }  
  21. });  

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


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

本文参考:

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

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


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

0 0