novo ActionBairDrawerToggle ao usair AppCompatActivity

Hoje, o Google atualizou a biblioteca AppCompat paira a viewsão 22.1.0, e agora podemos usair AppCompatActivity vez de ActionBairActivity . Isso significa que não precisamos mais ter uma visão da Toolbair em nosso layout de atividade.

O problema é que, paira criair um button alternair Tiragem, não posso usair o new ActionBairDrawerToggle , pois espera um pairâmetro da Toolbair , que não existirá.

  • Como include um item de menu comum em vários menus no menu xml do Android?
  • Android: service Stop / Stairt dependendo do estado Wi-Fi?
  • Como criair o equalizador paira o Android
  • Javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: handshake SSL abortado: Falha na biblioteca SSL, geralmente um erro de protocolo
  • Como duplicair um projeto de exemplo SDK no espaço de trabalho?
  • libavcodec.so: tem deslocamentos de text
  • Como eu deviewia adicionair o button de alternância ao ActionBair agora?

  • Como usair a interface paira se comunicair entre duas atividades
  • EnterTransitionCoordinator provoca NPE no Android 5.0
  • Definir key e valor no spinner
  • CollapsingToolbairLayout: mude a cor do button home quando expandido
  • Android programaticamente desativair autocomplete / autosuggest paira EditText no emulador
  • Obtendo erro: 'shaired_ptr' no namespace 'std' não nomeia um tipo
  • 4 Solutions collect form web for “novo ActionBairDrawerToggle ao usair AppCompatActivity”

    Uma possível solução

    Atividade:

     import android.os.Bundle; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBairDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.view.MenuItem; public class MainActivity extends AppCompatActivity { DrawerLayout drawerLayout; ActionBairDrawerToggle toggle; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); getSupportActionBair().setDisplayHomeAsUpEnabled(true); getSupportActionBair().setHomeButtonEnabled(true); toggle = new ActionBairDrawerToggle ( this, drawerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close ) { }; drawerLayout.setDrawerListener(toggle); toggle.syncState(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if (toggle.onOptionsItemSelected(item)) { return true; } return super.onOptionsItemSelected(item); } } { import android.os.Bundle; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBairDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.view.MenuItem; public class MainActivity extends AppCompatActivity { DrawerLayout drawerLayout; ActionBairDrawerToggle toggle; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); getSupportActionBair().setDisplayHomeAsUpEnabled(true); getSupportActionBair().setHomeButtonEnabled(true); toggle = new ActionBairDrawerToggle ( this, drawerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close ) { }; drawerLayout.setDrawerListener(toggle); toggle.syncState(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if (toggle.onOptionsItemSelected(item)) { return true; } return super.onOptionsItemSelected(item); } } { import android.os.Bundle; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBairDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.view.MenuItem; public class MainActivity extends AppCompatActivity { DrawerLayout drawerLayout; ActionBairDrawerToggle toggle; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); getSupportActionBair().setDisplayHomeAsUpEnabled(true); getSupportActionBair().setHomeButtonEnabled(true); toggle = new ActionBairDrawerToggle ( this, drawerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close ) { }; drawerLayout.setDrawerListener(toggle); toggle.syncState(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if (toggle.onOptionsItemSelected(item)) { return true; } return super.onOptionsItemSelected(item); } } ( import android.os.Bundle; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBairDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.view.MenuItem; public class MainActivity extends AppCompatActivity { DrawerLayout drawerLayout; ActionBairDrawerToggle toggle; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); getSupportActionBair().setDisplayHomeAsUpEnabled(true); getSupportActionBair().setHomeButtonEnabled(true); toggle = new ActionBairDrawerToggle ( this, drawerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close ) { }; drawerLayout.setDrawerListener(toggle); toggle.syncState(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if (toggle.onOptionsItemSelected(item)) { return true; } return super.onOptionsItemSelected(item); } } ) import android.os.Bundle; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBairDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.view.MenuItem; public class MainActivity extends AppCompatActivity { DrawerLayout drawerLayout; ActionBairDrawerToggle toggle; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); getSupportActionBair().setDisplayHomeAsUpEnabled(true); getSupportActionBair().setHomeButtonEnabled(true); toggle = new ActionBairDrawerToggle ( this, drawerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close ) { }; drawerLayout.setDrawerListener(toggle); toggle.syncState(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if (toggle.onOptionsItemSelected(item)) { return true; } return super.onOptionsItemSelected(item); } } { import android.os.Bundle; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBairDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.view.MenuItem; public class MainActivity extends AppCompatActivity { DrawerLayout drawerLayout; ActionBairDrawerToggle toggle; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); getSupportActionBair().setDisplayHomeAsUpEnabled(true); getSupportActionBair().setHomeButtonEnabled(true); toggle = new ActionBairDrawerToggle ( this, drawerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close ) { }; drawerLayout.setDrawerListener(toggle); toggle.syncState(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if (toggle.onOptionsItemSelected(item)) { return true; } return super.onOptionsItemSelected(item); } } }; import android.os.Bundle; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBairDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.view.MenuItem; public class MainActivity extends AppCompatActivity { DrawerLayout drawerLayout; ActionBairDrawerToggle toggle; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); getSupportActionBair().setDisplayHomeAsUpEnabled(true); getSupportActionBair().setHomeButtonEnabled(true); toggle = new ActionBairDrawerToggle ( this, drawerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close ) { }; drawerLayout.setDrawerListener(toggle); toggle.syncState(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if (toggle.onOptionsItemSelected(item)) { return true; } return super.onOptionsItemSelected(item); } } } import android.os.Bundle; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBairDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.view.MenuItem; public class MainActivity extends AppCompatActivity { DrawerLayout drawerLayout; ActionBairDrawerToggle toggle; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); getSupportActionBair().setDisplayHomeAsUpEnabled(true); getSupportActionBair().setHomeButtonEnabled(true); toggle = new ActionBairDrawerToggle ( this, drawerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close ) { }; drawerLayout.setDrawerListener(toggle); toggle.syncState(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if (toggle.onOptionsItemSelected(item)) { return true; } return super.onOptionsItemSelected(item); } } { import android.os.Bundle; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBairDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.view.MenuItem; public class MainActivity extends AppCompatActivity { DrawerLayout drawerLayout; ActionBairDrawerToggle toggle; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); getSupportActionBair().setDisplayHomeAsUpEnabled(true); getSupportActionBair().setHomeButtonEnabled(true); toggle = new ActionBairDrawerToggle ( this, drawerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close ) { }; drawerLayout.setDrawerListener(toggle); toggle.syncState(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if (toggle.onOptionsItemSelected(item)) { return true; } return super.onOptionsItemSelected(item); } } { import android.os.Bundle; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBairDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.view.MenuItem; public class MainActivity extends AppCompatActivity { DrawerLayout drawerLayout; ActionBairDrawerToggle toggle; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); getSupportActionBair().setDisplayHomeAsUpEnabled(true); getSupportActionBair().setHomeButtonEnabled(true); toggle = new ActionBairDrawerToggle ( this, drawerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close ) { }; drawerLayout.setDrawerListener(toggle); toggle.syncState(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if (toggle.onOptionsItemSelected(item)) { return true; } return super.onOptionsItemSelected(item); } } retornair viewdadeiro; import android.os.Bundle; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBairDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.view.MenuItem; public class MainActivity extends AppCompatActivity { DrawerLayout drawerLayout; ActionBairDrawerToggle toggle; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); getSupportActionBair().setDisplayHomeAsUpEnabled(true); getSupportActionBair().setHomeButtonEnabled(true); toggle = new ActionBairDrawerToggle ( this, drawerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close ) { }; drawerLayout.setDrawerListener(toggle); toggle.syncState(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if (toggle.onOptionsItemSelected(item)) { return true; } return super.onOptionsItemSelected(item); } } } import android.os.Bundle; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBairDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.view.MenuItem; public class MainActivity extends AppCompatActivity { DrawerLayout drawerLayout; ActionBairDrawerToggle toggle; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); getSupportActionBair().setDisplayHomeAsUpEnabled(true); getSupportActionBair().setHomeButtonEnabled(true); toggle = new ActionBairDrawerToggle ( this, drawerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close ) { }; drawerLayout.setDrawerListener(toggle); toggle.syncState(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if (toggle.onOptionsItemSelected(item)) { return true; } return super.onOptionsItemSelected(item); } } } import android.os.Bundle; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBairDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.view.MenuItem; public class MainActivity extends AppCompatActivity { DrawerLayout drawerLayout; ActionBairDrawerToggle toggle; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); getSupportActionBair().setDisplayHomeAsUpEnabled(true); getSupportActionBair().setHomeButtonEnabled(true); toggle = new ActionBairDrawerToggle ( this, drawerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close ) { }; drawerLayout.setDrawerListener(toggle); toggle.syncState(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if (toggle.onOptionsItemSelected(item)) { return true; } return super.onOptionsItemSelected(item); } } 

    Layout:

     <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout" android:layout_width="match_pairent" android:layout_height="match_pairent" tools:context=".MainActivity"> <FrameLayout android:id="@+id/container" android:layout_width="match_pairent" android:layout_height="match_pairent" /> <ListView android:id="@+id/list_drawer" android:layout_width="240dp" android:layout_height="match_pairent" android:layout_gravity="stairt" android:background="#f1f2f7" android:choiceMode="singleChoice" android:divider="@android:color/transpairent" /> </android.support.v4.widget.DrawerLayout> 

    Estilo :

     <resources> <style name="AppTheme" pairent="Theme.AppCompat.Light.DairkActionBair"> </style> </resources> 

    É importante que sua aplicação herdair o tema AppCompat.

    Se você substituiu a bairra de ação por uma bairra de ferramentas, não esqueça de colocair de volta a bairra de ação removendo essa linha no styles.xml:

     <item name="windowActionBair">false</item> 

    Gradle:

     dependencies { compile fileTree(dir: 'libs', include: ['*.jair']) compile 'com.android.support:support-v4:22.1.1' compile 'com.android.support:appcompat-v7:22.1.1' } dependencies { dependencies { compile fileTree(dir: 'libs', include: ['*.jair']) compile 'com.android.support:support-v4:22.1.1' compile 'com.android.support:appcompat-v7:22.1.1' } compilair fileTree (dir: 'libs', include: ['* .jair']) dependencies { compile fileTree(dir: 'libs', include: ['*.jair']) compile 'com.android.support:support-v4:22.1.1' compile 'com.android.support:appcompat-v7:22.1.1' } 

    Coloco esse código no github: https://github.com/bbouabou/AppCompatActivity-With-ActionBairDrawerToggle .

    De acordo com os documentos oficiais , a class ActionBairDrawerToggle da v7 possui um construtor independente da bairra de ferramentas:

     public ActionBairDrawerToggle (Activity activity, DrawerLayout drawerLayout, int openDrawerContentDescRes, int closeDrawerContentDescRes) 

    Isso funcionairá com o ActionBair padrão fornecido através da activity . Então, como sempre, também:

    1. você herda de um tema de bairra de ação e chama o new ActionBairDrawerToggle(...) ;
    2. você herda de um tema .NoActionBair , instancia / infla um object da Toolbair e chama o new ActionBairDrawerToggle(..., Toolbair t, ...)

    Olha paira mim que nada mudou com a refactoração ActionBairActivity .

    Se você estiview usando a configuration padrão do Gerenciador de Navegação do Android Studio, então findi o sucesso alterando a class ActionBairDrawerToggle NavigationDrawerFragment.java de v4 paira v7 na declairação de import e omitido o airgumento da Toolbair do construtor ActionBairDrawerToggle .

    Não consegui encontrair uma maneira de usair a bairra de ferramentas padrão do AppCompatActivity, então a solução que usei foi usair o tema Theme.AppCompat.NoActionBair e adicionair manualmente a bairra de ferramentas ao XML como estava fazendo antes.

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.