ActionBarDrawerToggle:
在前一張中我們並沒有使用drawLayout.setDrawerListener();
對應的參數對象就是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 就是實現了這個介面。他主要作用在於。
•改變ActionBar上的返回按鈕圖片(android.R.id.home)
•在開啟和關閉Drawer的時候,ActionBar的返回表徵圖會有動畫效果。
•監聽側邊欄的開啟和收合
在點擊側邊菜單選項的時候我們往往需要隱藏菜單來顯示整個菜單對應的內容。ActionBarDrawerToggle就是其中一種方法。
你也可以不用ActionBarDrawerToggle直接用import android.support.v4.widget.DrawerLayout.DrawerListener;
然後DrawerLayout相關在上一個文章中已經介紹了就不一一說明了。就從DrawerLayout的監聽開始。
我們今天用的包如下:
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() }};
初始化相關比較簡單看注釋就行。在監聽的回調方法中我們用invalidateOptionsMenu通知activity重繪menu,然後activity就有機會在onPrepareOptionsMenu方法中更新menu元素的顯示與隱藏。
接下來需要設定一下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); }
不難看出是顯示菜單鍵以及設定為可點擊使用的菜單鍵。
ActionBar相關設定:
•setHomeButtonEnabled //這個小於4.0版本的預設值為true的。但是在4.0及其以上是false,該方法的作用:決定左上方的表徵圖是否可以點擊。沒有向左的小表徵圖。 true 表徵圖可以點擊 false 不可以點擊。
•actionBar.setDisplayHomeAsUpEnabled(true) // 給左上方表徵圖的左邊加上一個返回的表徵圖 。對應ActionBar.DISPLAY_HOME_AS_UP
•actionBar.setDisplayShowCustomEnabled(true) // 使自訂的普通View能在title欄顯示,即actionBar.setCustomView能起作用,對應ActionBar.DISPLAY_SHOW_CUSTOM
•actionBar.setDisplayShowTitleEnabled(true) //對應ActionBar.DISPLAY_SHOW_TITLE。
然後我們需要綁定此監聽器:
mDrawerLayout.setDrawerListener(toggle);
之後我們需實現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); }
在這裡如果不去實現onOptionsIwotemSelected中的代碼那麼點擊菜單是沒有效果的。
現在運行代碼後可以看出如下效果:
在android.support.v7.app.ActionBarDrawerToggle;中的ActionBarDrawerToggle第三個參數
即:R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
更換為Toolbar對象,這樣一來你可以自定一個Toolbar做更為漂亮UI。
以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。