欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android中DrawerLayout实现侧滑菜单效果

程序员文章站 2023-11-23 21:00:16
众所周知,android里面我们很熟悉的一个功能,侧滑菜单效果在以前我们大部分都是用的slidingmenu这个开源框架,自从谷歌官方新出的一个drawerlayout控件...

众所周知,android里面我们很熟悉的一个功能,侧滑菜单效果在以前我们大部分都是用的slidingmenu这个开源框架,自从谷歌官方新出的一个drawerlayout控件之后,越来越多的应用开始使用谷歌的官方的控件写这个效果了。
话不多说,先来发图以表我滴诚意:

Android中DrawerLayout实现侧滑菜单效果

开始写代码

drawerlayout 是v4包里面的,所以项目里面需要添加v4包,具体怎么添加就不多说了,
navigationview需要在build.gradle里面添加compile ‘com.android.support:design:25.1.0'

activity_main.xml

<android.support.v4.widget.drawerlayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/drawer_layout"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:fitssystemwindows="true"
 tools:opendrawer="start">

 <!-- 右边视图 -->
 <include
 layout="@layout/app_bar_main"
 android:layout_width="match_parent"
 android:layout_height="match_parent" />

 <!-- 左边菜单 -->
 <android.support.design.widget.navigationview
 android:id="@+id/nav_view"
 android:layout_width="wrap_content"
 android:layout_height="match_parent"
 android:layout_gravity="start"
 android:fitssystemwindows="true"
 app:headerlayout="@layout/nav_header_main"
 app:menu="@menu/activity_main_drawer" />

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

然后在res文件夹里面添加一个menu菜单文件夹,已经添加的就不用添加了

上面的代码里面navigationview里面有个 app:menu=”@menu/activity_main_drawer”对应如下:
添加一个菜单文件,名字随意,我的是activity_main_drawer.xml

activity_main_drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

 <group android:checkablebehavior="single">
 <item
  android:id="@+id/nav_camera"
  android:icon="@drawable/ic_menu_camera"
  android:title="第一行" />
 <item
  android:id="@+id/nav_gallery"
  android:icon="@drawable/ic_menu_gallery"
  android:title="第二行" />
 <item
  android:id="@+id/nav_slideshow"
  android:icon="@drawable/ic_menu_slideshow"
  android:title="第三行" />
 <item
  android:id="@+id/nav_manage"
  android:icon="@drawable/ic_menu_manage"
  android:title="第四行" />
 </group>

 <item android:title="communicate">
 <menu>
  <item
  android:id="@+id/nav_share"
  android:icon="@drawable/ic_menu_share"
  android:title="第五行" />
  <item
  android:id="@+id/nav_send"
  android:icon="@drawable/ic_menu_send"
  android:title="第六行" />
 </menu>
 </item>

</menu>

再添加一个布局

上面的代码里面app:headerlayout=”@layout/nav_header_main”对应如下:
添加一个布局,名字是nav_header_main.xml

nav_header_main.xml

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:layout_width="match_parent"
 android:layout_height="@dimen/nav_header_height"
 android:background="@drawable/side_nav_bar"
 android:gravity="bottom"
 android:orientation="vertical"
 android:paddingbottom="@dimen/activity_vertical_margin"
 android:paddingleft="@dimen/activity_horizontal_margin"
 android:paddingright="@dimen/activity_horizontal_margin"
 android:paddingtop="@dimen/activity_vertical_margin"
 android:theme="@style/themeoverlay.appcompat.dark">

 <imageview
 android:id="@+id/imageview"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:paddingtop="@dimen/nav_header_vertical_spacing"
 app:srccompat="@android:drawable/sym_def_app_icon" />

 <textview
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:paddingtop="@dimen/nav_header_vertical_spacing"
 android:text="哈哈哈"
 android:textappearance="@style/textappearance.appcompat.body1" />

 <textview
 android:id="@+id/textview"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="123456789@qq.com" />

</linearlayout>

mainactivity:

public class mainactivity extends baseactivity implements navigationview.onnavigationitemselectedlistener {

 private linearlayout right;
 private navigationview left;
 private boolean isdrawer=false;
 private drawerlayout drawer;



 @override
 public int getlayoutid() {
 return r.layout.activity_main;
 }

 @override
 public void initpresenter() {

 }

 @override
 public void initview() {
 drawer = (drawerlayout) findviewbyid(r.id.drawer_layout);


 right = (linearlayout) findviewbyid(r.id.right);
 left = (navigationview) findviewbyid(r.id.nav_view);

 navigationview navigationview = (navigationview) findviewbyid(r.id.nav_view);
 navigationview.setnavigationitemselectedlistener(this);

 right.setontouchlistener(new view.ontouchlistener() {
  @override
  public boolean ontouch(view view, motionevent motionevent) {
  if(isdrawer){
   return left.dispatchtouchevent(motionevent);
  }else{
   return false;
  }
  }
 });
 drawer.setdrawerlistener(new drawerlayout.drawerlistener() {
  @override
  public void ondrawerslide(view drawerview, float slideoffset) {
  isdrawer=true;
  //获取屏幕的宽高
  windowmanager manager = (windowmanager) getsystemservice(context.window_service);
  display display = manager.getdefaultdisplay();
  //设置右面的布局位置 根据左面菜单的right作为右面布局的left 左面的right+屏幕的宽度(或者right的宽度这里是相等的)为右面布局的right
  right.layout(left.getright(), 0, left.getright() + display.getwidth(), display.getheight());
  }
  @override
  public void ondraweropened(view drawerview) {}
  @override
  public void ondrawerclosed(view drawerview) {
  isdrawer=false;
  }
  @override
  public void ondrawerstatechanged(int newstate) {}
 });


 }



 @override
 public void onbackpressed() {
 if (drawer.isdraweropen(gravitycompat.start)) {
  drawer.closedrawer(gravitycompat.start);
 } else {
  super.onbackpressed();
 }
 }




 @suppresswarnings("statementwithemptybody")
 @override
 public boolean onnavigationitemselected(menuitem item) {
 // handle navigation view item clicks here.
 int id = item.getitemid();

 if (id == r.id.nav_camera) {
  // handle the camera action
 } else if (id == r.id.nav_gallery) {

 } else if (id == r.id.nav_slideshow) {

 } else if (id == r.id.nav_manage) {

 } else if (id == r.id.nav_share) {

 } else if (id == r.id.nav_send) {

 }

 drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout);
 drawer.closedrawer(gravitycompat.start);
 return true;
 }


drawer.opendrawer(gravitycompat.start);//打开滑动菜单

drawer.closedrawer(gravitycompat.start);//关闭滑动菜单

drawer.isdraweropen(gravitycompat.start);//判断滑动菜单是否打开

具体源码demo下载地址

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。