2017-10-11 19 views
-1

J'utilise Navigation Drawer et le ActionBarDrawerToggle est synchronisé lorsque j'ouvre le tiroir en le faisant glisser, mais lorsque je clique sur l'icône Toolbar, il n'ouvre pas le tiroir. Qu'est-ce qui ne va pas dans mon code?Liaison du tiroir de navigation au bouton de la barre d'outils

actionBarDrawerToggle = new ActionBarDrawerToggle(Main2Activity.this, drawerLayout, toolbar, R.string.drawer_open, R.string.close_drawer) { 
     @Override 
     public void onDrawerClosed(View drawerView) { 
      super.onDrawerClosed(drawerView); 
     } 

     @Override 
     public void onDrawerOpened(View drawerView) { 
      super.onDrawerOpened(drawerView); 
     } 
    }; 
    drawerLayout.setDrawerListener(actionBarDrawerToggle); 
    drawerLayout.post(new Runnable() { 
     @Override 
     public void run() { 
      actionBarDrawerToggle.syncState(); 
     } 
    }); 

choses que j'ai donc essayer de:

actionBar.setDisplayHomeAsUpEnabled(true); 
    @Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case android.R.id.home: 
      drawerLayout.openDrawer(Gravity.START); 
      return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

Ceci est ma mise en page que j'ai utilisé. J'ai NavigationView et la disposition de tiroir.

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:ads="http://schemas.android.com/apk/res-auto" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/drawer" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:openDrawer="start"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fitsSystemWindows="true" 
     tools:context=".Main2Activity"> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       android:background="?attr/colorPrimary" 
       android:minHeight="?attr/actionBarSize" 
       app:popupTheme="@style/ThemeOverlay.AppCompat.ActionBar" 
       app:theme="@style/ToolbarColoredIcon" /> 
     <include layout="@layout/content_main2" /> 
    </RelativeLayout> 

    <android.support.design.widget.NavigationView 
     android:id="@+id/navigation_view" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="start"> 

     <LinearLayout 
      android:id="@+id/nav_layout" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 
      <!--header--> 
      <include 
       android:id="@+id/header" 
       layout="@layout/nav_header" 

       /> 
      <!--items--> 
      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" 
       android:paddingBottom="@dimen/dp20"> 

       <include 
        android:id="@+id/nav_menue_item_home" 
        layout="@layout/nav_menue_item" /> 

       <include 
        android:id="@+id/nav_menue_item_settings" 
        layout="@layout/nav_menue_item" /> 

       <include 
        android:id="@+id/nav_menue_item_more_apps" 
        layout="@layout/nav_menue_item" /> 

       <include 
        android:id="@+id/nav_menue_item_rate_us" 
        layout="@layout/nav_menue_item" /> 

       <include 
        android:id="@+id/nav_menue_item_privacy_policy" 
        layout="@layout/nav_menue_item" /> 
      </LinearLayout> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:background="#e3e4e3" 
       android:gravity="bottom" 
       android:orientation="vertical"> 

       <com.google.android.gms.ads.NativeExpressAdView 
        android:id="@+id/adViewNative" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentTop="true" 
        ads:adSize="@string/native_sm_ad_size" 
        ads:adUnitId="@string/native_sm_ad_unit_id" /> 

      </LinearLayout> 

     </LinearLayout> 
    </android.support.design.widget.NavigationView> 

</android.support.v4.widget.DrawerLayout> 

alors s'il vous plaît, où est le problème maintenant?

Grâce à tout ce que j'ai résoudre mon problème. Le problème n'est pas réellement dans le code mais le problème est dans la disposition où j'ajoute un tabHost qui ne permettant pas la barre d'outil pour le clic ainsi ceci est le problème réel.

+0

Si 'toolbar' n'est pas null, alors l'appel' setDisplayHomeAsUpEnabled() 'et la méthode' onOptionsItemSelected() 'ne sont pas nécessaires avec ce constructeur' ActionBarDrawerToggle'. Peut-être que quelque chose ne va pas avec la mise en page; par exemple, vous avez un 'View' couvrant la' Toolbar'. –

Répondre

1

Créer un toolbar.xml:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.Toolbar 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/toolbar" 
    android:minHeight="?attr/actionBarSize" 
    android:fitsSystemWindows="true" 
    > 

</android.support.v7.widget.Toolbar> 

Créer drawer_menu.xml:

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android"> 

    <group 
     android:checkableBehavior="single" 
     android:id="@+id/first_group"> 

     <item 
      android:id="@+id/first_item" 
      android:title="First Item" 
      android:icon="@drawable/first_item" 
      /> 

    </group> 

    <group 
     android:checkableBehavior="single" 
     android:id="@+id/second_group"> 
     <item 
      android:id="@+id/second_item" 
      android:title="Second Item" 
      android:icon="@drawable/second_item"> 

     </item> 
    </group> 

</menu> 

Ensuite, votre format activité devrait ressembler à ceci:

<?xml version="1.0" encoding="utf-8"?> 
    <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_parent" 
     android:layout_height="match_parent" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     tools:context=".MainActivity"> 

     <!--First child represents elements of activity in drawerLayout--> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" 
      android:id="@+id/relative_layout"> 

      <include 
       android:id="@+id/toolbar" 
       layout="@layout/toolbar" 
       /> 

     </RelativeLayout> 

     <!--Second child (navigation view) represents elements of navigation drawer in drawerLayout--> 

     <android.support.design.widget.NavigationView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:id="@+id/navigation_view" 
      android:layout_gravity="start" 
      android:background="@color/toolbar" 
      app:menu="@menu/drawer_menu"> 

     </android.support.design.widget.NavigationView> 

    </android.support.v4.widget.DrawerLayout> 

Hamburger Icône:

drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 

actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close); 

drawerLayout.addDrawerListener(actionBarDrawerToggle); 

Et puis créer une méthode:

@Override 
protected void onPostCreate(Bundle savedInstanceState) { 
    super.onPostCreate(savedInstanceState); 
    actionBarDrawerToggle.syncState(); 
} 

Laissez-moi savoir si ça aide.

+0

L'icône de hamburger fonctionne correctement mais ils n'ouvrent pas le tiroir ie ne répond pas à l'écouteur de clic –

+0

Ensuite, quelque chose doit être mal avec votre mise en page – Shaifu

+0

Vérifiez les modifications de mise en page @Shaifu –