2016-01-28 149 views
0

我想在導航欄打開時使用Google的動畫將導航圖標變爲箭頭,並將其返回到抽屜導航圖標。我也想知道,如果我可以在移動到其他活動時保持動作欄靜止不動。抽屜導航欄的DrawerArrowToggle動畫。

 menutitles = getResources().getStringArray(R.array.sidebar); 
      mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
      mDrawerList = (ListView) findViewById(R.id.slider_list); 

      mDrawerList.setAdapter(new ArrayAdapter<String>(this, 
        R.layout.list_tem, R.id.title, menutitles)); 
      // Set the list's click listener 
      mTitle = mDrawerTitle = getTitle(); 
      mDrawerList.setOnItemClickListener(new DrawerItemClickListener()); 
      mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, 
        R.mipmap.drawer, R.string.drawer_open, R.string.drawer_close) { 

       /** 
       * Called when a drawer has settled in a completely closed state. 
       */ 
       public void onDrawerClosed(View view) { 
        super.onDrawerClosed(view); 
        getActionBar().setTitle(mTitle); 
        invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() 
       } 

       /** 
       * Called when a drawer has settled in a completely open state. 
       */ 
       public void onDrawerOpened(View drawerView) { 
        super.onDrawerOpened(drawerView); 
        getActionBar().setTitle(mDrawerTitle); 
        invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() 
       } 
      }; 

      // Set the drawer toggle as the DrawerListener 
      mDrawerLayout.setDrawerListener(mDrawerToggle); 
      getActionBar().setDisplayHomeAsUpEnabled(true); 
      getActionBar().setHomeButtonEnabled(true); 

@Override 
    public boolean onPrepareOptionsMenu(Menu menu) { 
     // if nav drawer is opened, hide the action items 
     boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList); 

     return super.onPrepareOptionsMenu(menu); 
    } 

    /** * When using the ActionBarDrawerToggle, you must call it during * onPostCreate() and onConfigurationChanged()... */ 
    @Override 
    protected void onPostCreate(Bundle savedInstanceState) { 
     super.onPostCreate(savedInstanceState); 
     // Sync the toggle state after onRestoreInstanceState has occurred. 
     mDrawerToggle.syncState(); } 
    @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 
     // Pass any configuration change to the drawer toggles 
     mDrawerToggle.onConfigurationChanged(newConfig); 
    } 
    /** Swaps fragments in the main content view */ 
    private void selectItem(int position) { 
     switch (position){ 
      case 0: 
       Intent intent = new Intent(this, MainActivity.class); 
       startActivity(intent); 
       finish(); 
       break; 
      case 1: Intent intent2 = new Intent(this, Time.class); 
       startActivity(intent2); 
       finish(); 
       break; 
      case 2: Intent intent3 = new Intent(this, Temperature.class); 
       startActivity(intent3); 
       finish(); 
       break; 
      case 3: Intent intent4 = new Intent(this, Weight.class); 
       startActivity(intent4); 
       finish(); 
       break; 
      case 4: Intent intent5 = new Intent(this, Length.class); 
       startActivity(intent5); 
       finish(); 
       break; 
      case 5: Intent intent6 = new Intent(this, Money.class); 
       startActivity(intent6); 
       finish(); 
       break; 
      case 6: 
       Intent intent1 = new Intent(this, Metric.class); 
       startActivity(intent1); 
       finish(); 
       break; 
      case 7: 
       Intent intent7 = new Intent(this, Food.class); 
       startActivity(intent7); 
       finish(); 
       break; 
      case 8: 
       Intent intent8 = new Intent(this, Data.class); 
       startActivity(intent8); 
       finish(); 
       break; 
     } 
     // Create a new fragment and specify the planet to show based on position 


     // Highlight the selected item, update the title, and close the drawer 
     mDrawerList.setItemChecked(position, true); 
     setTitle("Converjz"); 
     mDrawerLayout.closeDrawer(mDrawerList); 
    } 

    @Override 
    public void setTitle(CharSequence title) { 
     mTitle = title; 
     getActionBar().setTitle(mTitle); 
    } 




    private class DrawerItemClickListener implements ListView.OnItemClickListener { 
     @Override 
     public void onItemClick(AdapterView parent, View view, int position, long id) { 
      selectItem(position); 
     } 
    } 

} 
+0

問題是什麼,究竟是什麼? –

+0

嗯,我一直在尋找Android的Material Design,其中導航抽屜可以通過動畫更改爲後退箭頭。我無法找到這個問題的直接答案,所以我個人問如何將它添加到我上面的代碼中。代碼沒有問題,我想輕鬆地將它添加到我的代碼中。 –

+0

您正在使用ActionBarDrawerToggle的v4支持版本。如果您需要漢堡箭頭動畫,則需要使用[v7 appcompat版本](http://developer.android.com/reference/android/support/v7/app/ActionBarDrawerToggle.html)。 –

回答

1

我想使用谷歌的動畫在導航欄的開度來導航圖標變成一個箭頭,並把它回抽屜導航圖標

望着構造函數調用,很明顯,您正在使用ActionBarDrawerToggle的v4支持版本。如果您想要漢堡包箭頭動畫,則需要使用v7 appcompat版本。

爲此,首先將v7 appcompat庫添加到您的項目中。然後,在ActionBarDrawerToggleimport語句中,將v4更改爲v7,並在構造函數調用中刪除參數R.mipmap.drawer

import android.support.v7.app.ActionBarDrawerToggle; 
... 

mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, 
    R.string.drawer_open, R.string.drawer_close) {...} 

我也想知道,如果移動到另一個活動時,我可以保持動作條固定。

如果你想使用抽屜式導航模式,你應該使用Fragment S爲不同的部分,而不是打開每個新Activity。鑑於代碼中的註釋,您所遵循的示例演示瞭如何執行此操作。

+0

我改變了一切,現在它可以工作,但我還有一個問題,我有一個活動,當它創建時,它會在操作欄中創建一個後退按鈕。當點擊一個單獨的電子郵件並返回時,我已經選擇了gmail中的動畫。我怎麼能做同樣的事情? –