ActionBarDrawerToggle的onOptionsItemSelected方法
2016-06-02 11:27
344 查看
原文地址:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/0925/1713.html
如何让app图标点击的时候能够展开或者隐藏侧边菜单。
一般的想法是在activity的onOptionsItemSelected方法中判断点击事件是否来自于app图标,然后用DrawerLayout.closeDrawer和DrawerLayout.openDrawer来隐藏与展开(参见第4点:在代码中主动展开与隐藏侧边菜单)。但是drawerLayout提供了更优雅的方式:使用ActionBarDrawerToggle的onOptionsItemSelected方法。该方法activity的onOptionsItemSelected方法中根据传递进来的menu
item做了上面我们在“一般想法”中提到的事情。用官方的说法是”ActionBarDrawerTogglewill take care of this”。我们只需这样做就ok了:
@Override
public booleanonOptionsItemSelected(MenuItem item) {
// The action bar home/up actionshould open or close the drawer.
// ActionBarDrawerToggle will takecare of this.
if(mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
……….//处理其他菜单点击事件
returnsuper.onOptionsItemSelected(item);
}如果不仔细阅读官方文档,估计我们很难看出(mDrawerToggle.onOptionsItemSelected(item)在这里的作用。这也是我刚开始最疑惑的地方。
如何让app图标点击的时候能够展开或者隐藏侧边菜单。
一般的想法是在activity的onOptionsItemSelected方法中判断点击事件是否来自于app图标,然后用DrawerLayout.closeDrawer和DrawerLayout.openDrawer来隐藏与展开(参见第4点:在代码中主动展开与隐藏侧边菜单)。但是drawerLayout提供了更优雅的方式:使用ActionBarDrawerToggle的onOptionsItemSelected方法。该方法activity的onOptionsItemSelected方法中根据传递进来的menu
item做了上面我们在“一般想法”中提到的事情。用官方的说法是”ActionBarDrawerTogglewill take care of this”。我们只需这样做就ok了:
@Override
public booleanonOptionsItemSelected(MenuItem item) {
// The action bar home/up actionshould open or close the drawer.
// ActionBarDrawerToggle will takecare of this.
if(mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
……….//处理其他菜单点击事件
returnsuper.onOptionsItemSelected(item);
}如果不仔细阅读官方文档,估计我们很难看出(mDrawerToggle.onOptionsItemSelected(item)在这里的作用。这也是我刚开始最疑惑的地方。
相关文章推荐
- 深入理解Linux 内核 chp 2 内存寻址
- RHEL6中ssh-copy-id出现Ambiguous output redirect.错误
- Linux下编译C++程序遇到错误:undefined reference to `*::*
- HADOOP 开发环境的配置
- 【linux】man和--help
- Linux系统(Ubuntu/Debian/RedHat/CentOS)超级简单的samba配置文件smb.conf
- MQ消息队列系列(5)ActiviteMQ Getting Started Guide(apache官网翻译)
- 在git与tortoisegit中使用openSSH与PuTTY
- Shader Properties
- .vimrc的配置
- Hadoop的InputFormats和OutputFormats
- Orocos OperationCaller 解析
- Linux下进度条
- tomcat安装配置
- centos6系列python2.7安装
- 用以生产环境的Hadoop版本比较
- linux中的set命令: "set -e" 与 "set -o pipefail"
- linux几种查看日志的方法
- AOPR软件可切换哪几种语言?
- 基于opencv的人脸识别程序