2014-12-17 17 views
11

Ich versuche ActionBarDrawerToggle in meinem CustomDrawerLayout zu verwenden. Wenn ich versuche, import android.support.v4.app.ActionBarDrawerToggle; zu importieren, ist es veraltet. Wenn ich versuche, import android.support.v7.app.ActionBarDrawerToggle; zu importieren, akzeptiert der Konstruktor keine fünf Argumente und das Symbol wird in ActionBar nicht geändert. Meine Frage ist, sollte ich v4 veraltet oder v7 mit vier Argumenten verwenden?ActionBarDrawerToggle v7 oder v4?

hier.

v4

/**v4 works with 5 arguments in constructor and change icon ActionBar but it's deprecated*/ 
import android.support.v4.app.ActionBarDrawerToggle; 

private ActionBarDrawerToggle tg; 

//ActionBarDrawerToggle deprecated 
tg = new ActionBarDrawerToggle(this, dl, R.drawable.ic_launcher, R.string.drawer_open, R.string.drawer_close){ 
      public void onDrawerClosed(View view) { 
       ab.setTitle(tl); 
       supportInvalidateOptionsMenu(); 
      } 

      public void onDrawerOpened(View view) { 
       ab.setTitle(tlf); 
       supportInvalidateOptionsMenu(); 
      } 
     }; 

v7

/**v7 works with 4 arguments in constructor and not change icon of ActionBar*/ 
import android.support.v7.app.ActionBarDrawerToggle; 

private ActionBarDrawerToggle tg; 

tg = new ActionBarDrawerToggle(this, dl, R.drawable.ic_launcher, R.string.drawer_open){ 
      public void onDrawerClosed(View view) { 
       ab.setTitle(tl); 
       supportInvalidateOptionsMenu(); 
      } 

      public void onDrawerOpened(View view) { 
       ab.setTitle(tlf); 
       supportInvalidateOptionsMenu(); 
      } 
     }; 

CustomDrawerLayout

public class CustomDrawerLayout extends ActionBarActivity implements OnItemClickListener{ 
    private ActionBar ab; 
    private DrawerLayout dl; 
    private ListView lv; 
    private ActionBarDrawerToggle tg; 

    private List<ItensListView> fragments; 
    private CharSequence tl; //titulo principal 
    private CharSequence tlf; //titulo fragment 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_custom_drawerlayout); 
     getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.action_bar))); 
     init(); 

     if(savedInstanceState == null){ 
      selectedItem(0); 
     }  
    } 

    private void init(){ 
     //actionbar 
     onConfigActionBar(); 
     //listview 
     configItensListView(); 
     lv = (ListView)findViewById(R.id.lv);    
     lv.setAdapter(new DrawerLayoutListViewAdapter(this, fragments)); 
     lv.setOnItemClickListener(this);   
     //drawerlayout 
     dl = (DrawerLayout)findViewById(R.id.dl); 
     //actionbardrawertoggle 
     tg = new ActionBarDrawerToggle(this, dl, R.drawable.ic_launcher, R.string.drawer_open){ 
      public void onDrawerClosed(View view) { 
       ab.setTitle(tl); 
       supportInvalidateOptionsMenu(); 
      } 

      public void onDrawerOpened(View view) { 
       ab.setTitle(tlf); 
       supportInvalidateOptionsMenu(); 
      } 
     }; 

     dl.setDrawerListener(tg); 

     tl = tlf = getTitle(); 

    } 

    /** ativa actionbar e botao home na action bar */ 
    private void onConfigActionBar(){ 
     ab = getSupportActionBar(); 
     ab.setDisplayHomeAsUpEnabled(true); 
     ab.setHomeButtonEnabled(true); 
    } 




    @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 
     tg.onConfigurationChanged(newConfig); 
    } 

    /** necessario */ 
    @Override 
    protected void onPostCreate(Bundle savedInstanceState) { 
     super.onPostCreate(savedInstanceState); 
     tg.syncState(); 
    } 

    /** necessario */ 
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     FragmentTransaction ft; 
     Fragment frag; 

     if(item.getItemId() == R.id.action_chat){ 
      frag = new HelloBubblesActivity(); 
      ft = getSupportFragmentManager().beginTransaction(); 
      ft.replace(R.id.fl, frag, "HelloBubblesActivity"); 
      ft.addToBackStack("back"); 
      ft.commit(); 
     } 

     if (tg.onOptionsItemSelected(item)) { 
       return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 


    /** necessario */ 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.custom_drawer_layout, menu); 

     return true; 
    } 

    /** necessario */ 
    @Override 
    public boolean onPrepareOptionsMenu(Menu menu) { 
     boolean status = dl.isDrawerOpen(lv); 
     menu.findItem(R.id.action_settings).setVisible(!status); 
     return super.onPrepareOptionsMenu(menu); 
    } 
+2

Mit dem aktuellen 'ActionBarActivity', ich glaube, die einzige Wahl ist die' v7' Version von 'ActionBarDrawerToggle'. – CommonsWare

Antwort

18

In v7 Konstruktor, sollten Sie passieren diese als Parameter:

new ActionBarDrawerToggle(Activity activity, DrawerLayout drawerLayout, int openDrawerContentDescRes, int closeDrawerContentDescRes) 

Während der v4 Konstruktor

new ActionBarDrawerToggle(Activity activity, DrawerLayout drawerLayout, int drawerImageRes, int openDrawerContentDescRes, int closeDrawerContentDescRes) 

ist Ich glaube, in dir v7 Konstruktor Sie das drawerImageRes als dritten Parameter (ic_launcher) gehalten.

mit diesem stattdessen versuchen:

new ActionBarDrawerToggle(this, dl, R.string.drawer_open, R.string.drawer_close) 
Verwandte Themen