Android DrawerLayout帶有側(cè)滑功能的布局類(2)
ActionBarDrawerToggle:
在前一張中我們并沒有使用drawLayout.setDrawerListener();
對應(yīng)的參數(shù)對象就是DrawerLayout.DrawerListener:
public interface DrawerListener {
void onDrawerSlide(View var1, float var2);
void onDrawerOpened(View var1);
void onDrawerClosed(View var1);
void onDrawerStateChanged(int var1);
}
本文講一下drawLayout.setDrawerListener(toggle);方式,ActionBarDrawerToggle 就是實(shí)現(xiàn)了這個接口。他主要作用在于。
•改變ActionBar上的返回按鈕圖片(android.R.id.home)
•在打開和關(guān)閉Drawer的時候,ActionBar的返回圖標(biāo)會有動畫效果。
•監(jiān)聽側(cè)邊欄的打開和收起
在點(diǎn)擊側(cè)邊菜單選項的時候我們往往需要隱藏菜單來顯示整個菜單對應(yīng)的內(nèi)容。ActionBarDrawerToggle就是其中一種方法。
你也可以不用ActionBarDrawerToggle直接用import android.support.v4.widget.DrawerLayout.DrawerListener;
然后DrawerLayout相關(guān)在上一個文章中已經(jīng)介紹了就不一一說明了。就從DrawerLayout的監(jiān)聽開始。
我們今天用的包如下:
import android.support.v4.app.ActionBarDrawerToggle;
首先我們初始化一個ActionBarDrawerToggle :
toggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description for accessibility */
R.string.drawer_close /* "close drawer" description for accessibility */
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
初始化相關(guān)比較簡單看注釋就行。在監(jiān)聽的回調(diào)方法中我們用invalidateOptionsMenu通知activity重繪menu,然后activity就有機(jī)會在onPrepareOptionsMenu方法中更新menu元素的顯示與隱藏。
接下來需要設(shè)置一下ActionBar:
private void initActionBar() {
// enable ActionBar app icon to behave as action to toggle nav drawer
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
}
不難看出是顯示菜單鍵以及設(shè)置為可點(diǎn)擊使用的菜單鍵。
ActionBar相關(guān)設(shè)置:
•setHomeButtonEnabled //這個小于4.0版本的默認(rèn)值為true的。但是在4.0及其以上是false,該方法的作用:決定左上角的圖標(biāo)是否可以點(diǎn)擊。沒有向左的小圖標(biāo)。 true 圖標(biāo)可以點(diǎn)擊 false 不可以點(diǎn)擊。
•actionBar.setDisplayHomeAsUpEnabled(true) // 給左上角圖標(biāo)的左邊加上一個返回的圖標(biāo) 。對應(yīng)ActionBar.DISPLAY_HOME_AS_UP
•actionBar.setDisplayShowCustomEnabled(true) // 使自定義的普通View能在title欄顯示,即actionBar.setCustomView能起作用,對應(yīng)ActionBar.DISPLAY_SHOW_CUSTOM
•actionBar.setDisplayShowTitleEnabled(true) //對應(yīng)ActionBar.DISPLAY_SHOW_TITLE。
然后我們需要綁定此監(jiān)聽器:
mDrawerLayout.setDrawerListener(toggle);
之后我們需實(shí)現(xiàn)Acitivity的一下代碼才能使用:
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
toggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
toggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onOptionsIwotemSelected(MenuItem item) {
// The action bar home/up action should open or close the drawer.
// ActionBarDrawerToggle will take care of this.
if (toggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
在這里如果不去實(shí)現(xiàn)onOptionsIwotemSelected中的代碼那么點(diǎn)擊菜單是沒有效果的。
現(xiàn)在運(yùn)行代碼后可以看出如下效果:

在android.support.v7.app.ActionBarDrawerToggle;中的ActionBarDrawerToggle第三個參數(shù)
即:R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
更換為Toolbar對象,這樣一來你可以自定一個Toolbar做更為漂亮UI。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)右邊抽屜Drawerlayout效果
- Android使用DrawerLayout實(shí)現(xiàn)仿QQ雙向側(cè)滑菜單
- Android App中DrawerLayout抽屜效果的菜單編寫實(shí)例
- Android組件之DrawerLayout實(shí)現(xiàn)抽屜菜單
- Android Drawerlayout側(cè)拉欄事件傳遞問題的解決方法
- Android DrawerLayout帶有側(cè)滑功能的布局類(1)
- Android中DrawerLayout+ViewPager滑動沖突的解決方法
- Android DrawerLayout實(shí)現(xiàn)側(cè)拉菜單功能
- Android組件DrawerLayout仿網(wǎng)易新聞v4.4側(cè)滑菜單
- android側(cè)滑菜單控件DrawerLayout使用方法詳解
相關(guān)文章
Android 輕松實(shí)現(xiàn)語音識別詳解及實(shí)例代碼
這篇文章主要介紹了Android 輕松實(shí)現(xiàn)語音識別的相關(guān)資料,并附實(shí)例代碼,需要的朋友可以參考下2016-09-09
Android連接MySQL數(shù)據(jù)庫詳細(xì)教程
在Android應(yīng)用程序中連接 MySQL 數(shù)據(jù)庫可以幫助開發(fā)人員實(shí)現(xiàn)更豐富的數(shù)據(jù)管理功能,本教程將介紹如何在Android應(yīng)用程序中使用低版本的MySQL Connector/J驅(qū)動程序來連接MySQL數(shù)據(jù)庫,需要的朋友可以參考下2023-05-05
Kotlin startActivity跳轉(zhuǎn)Activity實(shí)現(xiàn)流程詳解
在Android當(dāng)中,Activity的跳轉(zhuǎn)有兩種方法,第一個是利用startActivity(Intent intent);的方法,第二個則是利用startActivityForResult(Intent intent,int requestCode);的方法,從字面上來看,這兩者之間的差別只在于是否有返回值的區(qū)別,實(shí)際上也確實(shí)只有這兩種區(qū)別2022-12-12
Android Support Annotations資料整理
這篇文章主要介紹了Android Support Annotations資料整理的相關(guān)資料,需要的朋友可以參考下2017-05-05
Android實(shí)現(xiàn)五子棋游戲(局域網(wǎng)版)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)局域網(wǎng)版的五子棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-05-05
Android解析相同接口返回不同格式j(luò)son數(shù)據(jù)的方法
這篇文章主要介紹了Android解析相同接口返回不同格式j(luò)son數(shù)據(jù)的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08

