Android DrawerLayout 高仿QQ5.2双向侧滑菜单
1、概述
之前写了一个Android 高仿 QQ5.0 侧滑菜单效果 自定义控件来袭 ,恰逢QQ5.2又加了一个右侧菜单,刚好看了下DrawerLayout,一方面官方的东西,我都比较感兴趣;另一方面,这玩意用起来的确方便,于是简单写了个demo,高仿QQ5.2双向侧滑,分享给大家。
首先看看效果图:
DrawerLayout用起来真的很方便,下面一起看看用法~
2、DrawerLayout的使用
直接将DrawerLayout作为根布局,然后其内部第一个View为内容区域,第二个View为左侧菜单,第三个View为右侧侧滑菜单,当前第三个是可选的。
第一个View的宽高应当设置为match_parent,当然了,这也理所当然。
第二、三个View需要设置android:layout_gravity="left",和android:layout_gravity="right"且一搬高度设置为match_parent,宽度为固定值,即侧滑菜单的宽度。
按照上面的描述写个布局文件,然后设置给Activity就添加好了左右侧滑了,是不是很简单~~~
比如我们的布局文件:
- <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/id_drawerLayout"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@drawable/img_frame_background" >
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@drawable/qq" >
- <Button
- android:layout_width="40dp"
- android:layout_height="30dp"
- android:layout_marginTop="10dp"
- android:layout_alignParentRight="true"
- android:background="@drawable/youce"
- android:onClick="OpenRightMenu" />
- </RelativeLayout>
- <fragment
- android:id="@+id/id_left_menu"
- android:name="com.zhy.demo_zhy_17_drawerlayout.MenuLeftFragment"
- android:layout_width="200dp"
- android:layout_height="match_parent"
- android:layout_gravity="left"
- android:tag="LEFT" />
- <fragment
- android:id="@+id/id_right_menu"
- android:name="com.zhy.demo_zhy_17_drawerlayout.MenuRightFragment"
- android:layout_width="100dp"
- android:layout_height="match_parent"
- android:layout_gravity="right"
- android:tag="RIGHT" />
- </android.support.v4.widget.DrawerLayout>
这里我们的主内容区域为RelativeLayout
菜单用的两个Fragment,左侧为200dp,右侧为100dp;
好了,看了我们的布局文件,接下来看下我们的详细代码。
3、代码是最好的老师
1、MenuLeftFragment
- package com.zhy.demo_zhy_17_drawerlayout;
- import android.os.Bundle;
- import android.support.v4.app.Fragment;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- public class MenuLeftFragment extends Fragment
- {
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState)
- {
- return inflater.inflate(R.layout.layout_menu, container, false);
- }
- }
对应的布局文件:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="#00000000" >
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_centerVertical="true"
- android:orientation="vertical" >
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content" >
- <ImageView
- android:id="@+id/one"
- android:layout_width="50dp"
- android:layout_height="50dp"
- android:layout_centerVertical="true"
- android:layout_marginLeft="20dp"
- android:layout_marginTop="20dp"
- android:src="@drawable/img_1" />
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_centerVertical="true"
- android:layout_marginLeft="20dp"
- android:layout_toRightOf="@id/one"
- android:text="第1个Item"
- android:textColor="#f0f0f0"
- android:textSize="20sp" />
- </RelativeLayout>
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content" >
- <ImageView
- android:id="@+id/two"
- android:layout_width="50dp"
- android:layout_height="50dp"
- android:layout_centerVertical="true"
- android:layout_marginLeft="20dp"
- android:layout_marginTop="20dp"
- android:src="@drawable/img_2" />
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_centerVertical="true"
- android:layout_marginLeft="20dp"
- android:layout_toRightOf="@id/two"
- android:text="第2个Item"
- android:textColor="#f0f0f0"
- android:textSize="20sp" />
- </RelativeLayout>
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content" >
- <ImageView
- android:id="@+id/three"
- android:layout_width="50dp"
- android:layout_height="50dp"
- android:layout_centerVertical="true"
- android:layout_marginLeft="20dp"
- android:layout_marginTop="20dp"
- android:src="@drawable/img_3" />
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_centerVertical="true"
- android:layout_marginLeft="20dp"
- android:layout_toRightOf="@id/three"
- android:text="第3个Item"
- android:textColor="#f0f0f0"
- android:textSize="20sp" />
- </RelativeLayout>
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content" >
- <ImageView
- android:id="@+id/four"
- android:layout_width="50dp"
- android:layout_height="50dp"
- android:layout_centerVertical="true"
- android:layout_marginLeft="20dp"
- android:layout_marginTop="20dp"
- android:src="@drawable/img_4" />
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_centerVertical="true"
- android:layout_marginLeft="20dp"
- android:layout_toRightOf="@id/four"
- android:text="第4个Item"
- android:textColor="#f0f0f0"
- android:textSize="20sp" />
- </RelativeLayout>
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content" >
- <ImageView
- android:id="@+id/five"
- android:layout_width="50dp"
- android:layout_height="50dp"
- android:layout_centerVertical="true"
- android:layout_marginLeft="20dp"
- android:layout_marginTop="20dp"
- android:src="@drawable/img_5" />
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_centerVertical="true"
- android:layout_marginLeft="20dp"
- android:layout_toRightOf="@id/five"
- android:text="第5个Item"
- android:textColor="#f0f0f0"
- android:textSize="20sp" />
- </RelativeLayout>
- </LinearLayout>
- </RelativeLayout>
其实就是堆出来的布局~~没撒意思~
2、MenuRightFragment
- package com.zhy.demo_zhy_17_drawerlayout;
- import android.os.Bundle;
- import android.support.v4.app.Fragment;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- public class MenuRightFragment extends Fragment
- {
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState)
- {
- return inflater.inflate(R.layout.menu_layout_right, container, false);
- }
- }
对应布局文件:
- <?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:gravity="center_vertical"
- android:orientation="vertical" >
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_centerVertical="true"
- android:layout_gravity="center_vertical"
- android:layout_marginBottom="20dp"
- android:orientation="vertical" >
- <ImageView
- android:layout_width="60dp"
- android:layout_height="60dp"
- android:layout_gravity="center"
- android:src="@drawable/wode" />
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:text="扫一扫"
- android:textColor="#ffffff" />
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_centerVertical="true"
- android:layout_gravity="center_vertical"
- android:layout_marginBottom="20dp"
- android:orientation="vertical" >
- <ImageView
- android:layout_width="60dp"
- android:layout_height="60dp"
- android:layout_gravity="center"
- android:src="@drawable/saoma" />
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:text="讨论组"
- android:textColor="#ffffff" />
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_centerVertical="true"
- android:layout_gravity="center_vertical"
- android:layout_marginBottom="20dp"
- android:orientation="vertical" >
- <ImageView
- android:layout_width="60dp"
- android:layout_height="60dp"
- android:layout_gravity="center"
- android:src="@drawable/wode" />
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:text="扫一扫"
- android:textColor="#ffffff" />
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_centerVertical="true"
- android:layout_gravity="center_vertical"
- android:layout_marginBottom="20dp"
- android:orientation="vertical" >
- <ImageView
- android:layout_width="60dp"
- android:layout_height="60dp"
- android:layout_gravity="center"
- android:src="@drawable/saoma" />
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:text="讨论组"
- android:textColor="#ffffff" />
- </LinearLayout>
- </LinearLayout>
依旧很简单,除了图标比较难找以外~~
3、MainActivity
MainActivity的布局文件已经贴过了~~
- package com.zhy.demo_zhy_17_drawerlayout;
- import android.os.Bundle;
- import android.support.v4.app.FragmentActivity;
- import android.support.v4.widget.DrawerLayout;
- import android.support.v4.widget.DrawerLayout.DrawerListener;
- import android.view.Gravity;
- import android.view.View;
- import android.view.Window;
- import com.nineoldandroids.view.ViewHelper;
- public class MainActivity extends FragmentActivity
- {
- private DrawerLayout mDrawerLayout;
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- setContentView(R.layout.activity_main);
- initView();
- initEvents();
- }
- public void OpenRightMenu(View view)
- {
- mDrawerLayout.openDrawer(Gravity.RIGHT);
- mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED,
- Gravity.RIGHT);
- }
- private void initEvents()
- {
- mDrawerLayout.setDrawerListener(new DrawerListener()
- {
- @Override
- public void onDrawerStateChanged(int newState)
- {
- }
- @Override
- public void onDrawerSlide(View drawerView, float slideOffset)
- {
- View mContent = mDrawerLayout.getChildAt(0);
- View mMenu = drawerView;
- float scale = 1 - slideOffset;
- float rightScale = 0.8f + scale * 0.2f;
- if (drawerView.getTag().equals("LEFT"))
- {
- float leftScale = 1 - 0.3f * scale;
- ViewHelper.setScaleX(mMenu, leftScale);
- ViewHelper.setScaleY(mMenu, leftScale);
- ViewHelper.setAlpha(mMenu, 0.6f + 0.4f * (1 - scale));
- ViewHelper.setTranslationX(mContent,
- mMenu.getMeasuredWidth() * (1 - scale));
- ViewHelper.setPivotX(mContent, 0);
- ViewHelper.setPivotY(mContent,
- mContent.getMeasuredHeight() / 2);
- mContent.invalidate();
- ViewHelper.setScaleX(mContent, rightScale);
- ViewHelper.setScaleY(mContent, rightScale);
- } else
- {
- ViewHelper.setTranslationX(mContent,
- -mMenu.getMeasuredWidth() * slideOffset);
- ViewHelper.setPivotX(mContent, mContent.getMeasuredWidth());
- ViewHelper.setPivotY(mContent,
- mContent.getMeasuredHeight() / 2);
- mContent.invalidate();
- ViewHelper.setScaleX(mContent, rightScale);
- ViewHelper.setScaleY(mContent, rightScale);
- }
- }
- @Override
- public void onDrawerOpened(View drawerView)
- {
- }
- @Override
- public void onDrawerClosed(View drawerView)
- {
- mDrawerLayout.setDrawerLockMode(
- DrawerLayout.LOCK_MODE_LOCKED_CLOSED, Gravity.RIGHT);
- }
- });
- }
- private void initView()
- {
- mDrawerLayout = (DrawerLayout) findViewById(R.id.id_drawerLayout);
- mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED,
- Gravity.RIGHT);
- }
- }
嗯,代码基本没什么注释~~维撒呢?是因为的确没什么好注释的。
提几点:
1、为了模拟QQ的右侧菜单需要点击才能出现,所以在初始化DrawerLayout的时候,使用了mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED,Gravity.RIGHT);意思是只有编程才能将其弹出。
然后在弹出以后,需要让手势可以滑动回去,所以在OpenRightMenu中又编写了:
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED,Gravity.RIGHT); UNLOCK了一下。
最后在onDrawerClosed回调中,继续设置mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED,Gravity.RIGHT);
2、动画效果
动画用了nineoldandroids,关于动画各种偏移量、缩放比例的计算请参考Android 高仿 QQ5.0 侧滑菜单效果 自定义控件来袭 基本是一致的,唯一的不同的地方,给Content设置了ViewHelper.setTranslationX(mContent, mMenu.getMeasuredWidth() * (1 - scale));让Content在菜单的右侧,默认情况下Menu在菜单之上,所以我们根据菜单划出的距离给Content设置X方向的偏移量。
好了,其实看到可以这么做,基本上任何的侧滑菜单效果都能写出来了。有兴趣的话,可以拿DrawerLayout实现这篇博客的所有效果:Android 实现形态各异的双向侧滑菜单 自定义控件来袭 。
3、setDrawerListener
通过代码也能看出来,可以使用setDrawerListener监听菜单的打开与关闭等等。这里对于当前操作是哪个菜单的判断是通过TAG判断的,我觉得使用gravity应该也能判断出来~~
好了,没撒了,由于DrawerLayout默认只能从边界划出菜单,但是QQ划出菜单的手势区域比较大,大家有兴趣,可以重写Activity的onTouchEvent,在里面判断,如果是左右滑动手势神马的,弹出菜单,应该不麻烦~~~
Android DrawerLayout 高仿QQ5.2双向侧滑菜单的更多相关文章
- Android 高仿QQ5.2双向側滑菜单DrawerLayout实现源代码
Android 高仿QQ5.2双向側滑菜单DrawerLayout实现源代码 左右側滑效果图 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a ...
- Android 自定义View修炼-仿QQ5.0 的侧滑菜单效果的实现
有一段时间没有写博客了,最近比较忙,没什么时间写,刚好今天有点时间, 我就分享下,侧滑菜单的实现原理,一般android侧滑的实现原理和步骤如下:(源码下载在下面最后给出哈) 1.使用ViewGrou ...
- Android 实现形态各异的双向侧滑菜单 自定义控件来袭
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/39670935,本文出自:[张鸿洋的博客] 1.概述 关于自定义控件侧滑已经写了两 ...
- android版高仿淘宝客户端源码V2.3
android版高仿淘宝客户端源码V2.3,这个版本我已经更新到2.3了,源码也上传到源码天堂那里了,大家可以看一下吧,该应用实现了我们常用的购物功能了,也就是在手机上进行网购的流程的,如查看产品(浏 ...
- Android 实现形态各异的双向侧滑菜单 自定义控件来袭(转载)
1.概述 关于自定义控件侧滑已经写了两篇了~~今天决定把之前的单向改成双向,当然了,单纯的改动之前的代码也没意思,今天不仅会把之前的单向改为双向,还会多添加一种侧滑效果,给大家带来若干种形态各异的双向 ...
- Android 高仿QQ滑动弹出菜单标记已读、未读消息
在上一篇博客<Android 高仿微信(QQ)滑动弹出编辑.删除菜单效果,增加下拉刷新功能>里,已经带着大家学习如何使用SwipeMenuListView这一开源库实现滑动列表弹出菜单,接 ...
- Android实现高仿QQ附近的人搜索展示
本文主要实现了高仿QQ附近的人搜索展示,用到了自定义控件的方法 最终效果如下 1.下面展示列表我们可以使用ViewPager来实现(当然如果你不觉得麻烦,你也可以用HorizontalScrollVi ...
- Android高仿qq及微信底部菜单的几种实现方式
最近项目没那么忙,想着开发app的话,有很多都是重复,既然是重复的,那就没有必要每次都去写,所以就想着写一个app通用的基本框架,这里说的框架不是什么MVC,MVP,MVVM这种,而是app开发的通用 ...
- Android开发——使用高级的RecyclerView实现侧滑菜单删除功能(SwipeRecyclerView)
使用之前,先简单介绍一下这个SwipeRecyclerView,这是严大(严振杰)基于RecyclerView的进行修改和封装的高级RecyclerView,其可以实现像QQ聊天界面的侧滑删除菜单,和 ...
随机推荐
- C++ 四种强制类型转换
来自csdn:http://blog.csdn.net/hgl868/article/details/46619399 C风格的强制类转换(Type Cast)很简单,不管什么类型的转换统统是: TY ...
- ASP.NET中利用DataList实现图片无缝滚动
这个问题之前也困扰我,后来解决了,拿出来分享下,以后用也方便,代码很容易看懂,不多说什么了 <div id="demo" style="overflow: hidd ...
- Sixth scrum meeting - 2015/10/31
概述 今天是周末,我们小组由于之前拖延的比较久,所以今天仍然在努力的开发…… 目前开发已经到了中期阶段,今天遇到了一个问题就是,由于小组的某些同学对git的使用不太熟悉,导致在git push的时候遇 ...
- SGU-169 Numbers(找规律)
题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=169 解题报告: P(n)定义为n的所有位数的乘积,例如P(1243)=1*2*3* ...
- bmob
移动后台: bmob http://baike.baidu.com/link?url=GHdwJY3cGygcfQDdzosckQnhVy1pvIGZA2Ws0K26lSSFGu7QRX4R1wlo6 ...
- MySQL之扩展(触发器,存储过程等)
视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,并可以将其当作表来使用. SELECT * FROM ( SEL ...
- php 二分查找
<?php /**二分查找:查找一个值在数组中的位置 * @$arr:操作的数组,前提是按顺序排列 * @$val:查找的值 * @$start:查找的起始位置,默认从数组的第一个数找起 * @ ...
- c/c++指针总结[pointer summary]
[本文链接] http://www.cnblogs.com/hellogiser/p/pointer-summary.html 1.指针注意事项 (1). 指针类型字符串不容许修改 char *str ...
- 15.含有指针成员的类的拷贝[ClassCopyConstructorWithPointerMember]
[题目] 下面是一个数组类的声明与实现.请分析这个类有什么问题,并针对存在的问题提出几种解决方案. C++ Code 123456789101112131415161718192021222324 ...
- (转)SQL SERVER的锁机制(一)——概述(锁的种类与范围)
锁定:通俗的讲就是加锁.锁定是 Microsoft SQL Server 数据库引擎用来同步多个用户同时对同一个数据块的访问的一种机制. 定义:当有事务操作时,数据库引擎会要求不同类型的锁定,如相关数 ...