欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android DrawerLayout帶有側(cè)滑功能的布局類(2)

 更新時間:2016年07月15日 09:57:16   作者:金洪光  
這篇文章主要為大家詳細(xì)介紹了Android DrawerLayout帶有側(cè)滑功能的布局類,感興趣的小伙伴們可以參考一下

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 就是實現(xiàn)了這個接口。他主要作用在于。
 •改變ActionBar上的返回按鈕圖片(android.R.id.home)
 •在打開和關(guān)閉Drawer的時候,ActionBar的返回圖標(biāo)會有動畫效果。
 •監(jiān)聽側(cè)邊欄的打開和收起 

在點擊側(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就有機會在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è)置為可點擊使用的菜單鍵。

ActionBar相關(guān)設(shè)置:
 •setHomeButtonEnabled //這個小于4.0版本的默認(rèn)值為true的。但是在4.0及其以上是false,該方法的作用:決定左上角的圖標(biāo)是否可以點擊。沒有向左的小圖標(biāo)。 true 圖標(biāo)可以點擊  false 不可以點擊。
 •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);
之后我們需實現(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);
  }

在這里如果不去實現(xiàn)onOptionsIwotemSelected中的代碼那么點擊菜單是沒有效果的。

現(xià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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論