2015-02-24 10 views
8

stavo cercando di implementare l'android.support.v4.app.ActionBarDrawerToggle nella mia app; poiché questa classe è deprecataobsolete ActionBarDrawerToggle

Questa classe è obsoleta. Utilizzare ActionBarDrawerToggle in support-v7-appcompat.

Sono passato a android.support.v7.app.ActionBarDrawerToggle.

Prima che potessi chiamare il costruttore in questo modo:

mDrawerToggle = 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() 
     } 
    }; 

ma dopo che ho passato alla libreria di supporto v7 più recente, ricevo l'errore

"ActionBarDrawerToggle() in ActionBarDrawerToggle cannot be applied to: 

toolbar: android.support.v7.widget.Toolbar 
Actual arguments: R.drawable.ic_drawer (int)" 

A quanto pare non sono l'introduzione una corretta barra degli strumenti nel costruttore, ma non sono sicuro di capire la differenza tra i due argomenti in conflitto. Come ottengo la barra degli strumenti richiesta?

+1

I secondo questa domanda. Ho trovato questo come soluzione parziale: getActionBar(). SetDisplayShowHomeEnabled (true); getActionBar(). SetIcon (R.drawable.ic_drawer); Il problema è che l'icona ha padding - come posso rimuoverlo? Inoltre, dove posso trovare un buon ic_drawer.png? – mikeesouth

+1

Nel frattempo questa domanda ha avuto risposta in http://stackoverflow.com/questions/26439619/how-to-replace-deprecated-android-support-v4-app-actionbardrawertoggle – k3b

risposta

17

ho risolto il mio problema importando il più recente android.support.v7.app.ActionBarDrawerToggle e utilizzando la RecyclerView al posto del ListView, come mostrato in questo esempio: How to make Material Design Navigation Drawer With Header View:

private ActionBarDrawerToggle mDrawerToggle; 
//... ... 
mDrawerToggle = new ActionBarDrawerToggle(
      this, 
      mDrawerLayout, 
      toolbar, 
      R.string.drawer_open, R.string.drawer_close){ 
        @Override 
        public void onDrawerOpened(View drawerView) { 
         super.onDrawerOpened(drawerView); 
         // code here will execute once the drawer is opened 
         getSupportActionBar().setTitle(mTitle); 
         invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() 
        } 
        @Override 
        public void onDrawerClosed(View drawerView) { 
         super.onDrawerClosed(drawerView); 
         // Code here will execute once drawer is closed 
         getSupportActionBar().setTitle(mDrawerTitle); 
         invalidateOptionsMenu(); 
    }; 

Se avete ancora problemi di controllo qui: How to replace deprecated android.support.v4.app.ActionBarDrawerToggle

Problemi correlati