首頁  >  文章  >  Java  >  Android DrawerLayout帶有側滑功能的佈局類別(2)

Android DrawerLayout帶有側滑功能的佈局類別(2)

高洛峰
高洛峰原創
2017-01-07 14:28:131104瀏覽

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);
 }

DrawerToggle 就是實現了這個接口。他主要作用在於。

 •改變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 .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);
  }

  決定這裡的程式碼是沒有效果的。


現在運行程式碼後可以看出以下效果:



在android.support.v7.app.ActionBarDrawerToggle;中的ActionBarDrawerToggle第三個參數? to replace 'Up' caret */ 

更換為Toolbar對象,這樣一來你可以自訂一個Toolbar做更為漂亮UI。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支PHP中文網。


更多Android DrawerLayout帶有側滑功能的佈局類(2)相關文章請關注PHP中文網!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn