2016-03-24 20 views
0

Implementierung einer NavigationView-Schublade - Ich kann die Schublade erstellen und sehen, aber nicht mit Wischen schließen. Darüber hinaus scheint der NavigationItemSelectedListener nicht korrekt eingerichtet zu sein, da ich Click-Ereignisse auf den Elementen nicht erkennen kann.Android NavigationView-Schublade schließt nicht

MainActivity.java

private NavigationView navigationView; 
private DrawerLayout navDrawerLayout; 
private ListView navDrawerList; 
private ArrayList<NavDrawerItem> navDrawerItemList; 
private ActionBarDrawerToggle navDrawerToggle; 

    private void setupNavDrawer() { 
    this.navigationView = (NavigationView) findViewById(R.id.navigation_view); 
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { 
     @Override 
     public boolean onNavigationItemSelected(MenuItem item) { 
      Utility.showDebugToast(String.valueOf(item.getItemId())); 
      if(item.isChecked()) { 
       item.setChecked(false); 
      } else { 
       item.setChecked(true); 
      } 
      navDrawerLayout.closeDrawers(); 
      return true; 
     } 
    }); 
    this.navDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
    this.navDrawerList = (ListView) findViewById(R.id.left_drawer); 
    this.navDrawerItemList = new ArrayList<>(); 
    this.navDrawerItemList.add(new NavDrawerItem("NAME", NavDrawerItemType.NAME)); 
    this.navDrawerItemList.add(new NavDrawerItem("Create Mesh", NavDrawerItemType.CREATE_MESH)); 
    this.navDrawerList.setAdapter(new NavDrawerAdapter(this, R.layout.drawer_item, R.id.drawer_tab_text, this.navDrawerItemList)); 

    this.navDrawerToggle = new ActionBarDrawerToggle(this, navDrawerLayout, R.string.accept, R.string.accept) { 

     @Override 
     public void onDrawerClosed(View view) { 
      super.onDrawerClosed(view); 
     } 
     @Override 
     public void onDrawerOpened(View view) { 
      super.onDrawerOpened(view); 
     } 
    }; 

    this.navDrawerLayout.addDrawerListener(navDrawerToggle); 
    navDrawerToggle.syncState(); 
} 

activity.xml

<android.support.v4.widget.DrawerLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/drawer_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 
<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/purpose_background" > 

    <FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <android.support.design.widget.AppBarLayout 
      android:id="@+id/app_bar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@color/appbar_grey" 
      > 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       /> 

      <android.support.design.widget.TabLayout 
       android:id="@+id/tab_layout" 
       style="@style/LobbyTabLayout" 
       android:layout_width="match_parent" 
       android:layout_height="44dp" 
       app:layout_scrollFlags="scroll|enterAlways" 
       app:tabMode="scrollable"/> 

      <LinearLayout 
       android:id="@+id/search_container" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="horizontal" 
       app:layout_scrollFlags="scroll|enterAlways" > 

       <EditText 
        android:id="@+id/search_box" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:layout_margin="4dp" 
        android:layout_weight="3" 
        android:lines="1" 
        android:textSize="16sp" 
        android:hint="@string/search_editText_placeholder"/> 

       <Button 
        android:id="@+id/action_search_button" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:layout_margin="4dp" 
        android:layout_weight="1" 
        android:text="@string/search_button"/> 

      </LinearLayout> 

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

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:id="@+id/main_lobby_container"> 

      <LinearLayout 
       android:id="@+id/lobby_search_area" 
       android:orientation="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       tools:context=".Lobby" 
       android:focusableInTouchMode="true" 
       app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

       <FrameLayout 
        android:layout_width="match_parent" 
        android:layout_height="0dp" 
        android:layout_weight="1" 
        android:id="@+id/grid_fragment"/> 
      </LinearLayout> 

      <com.nhaarman.supertooltips.ToolTipRelativeLayout 
       android:id="@+id/mesh_list_tooltip" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"/> 
     </RelativeLayout> 

    </FrameLayout> 

    <FrameLayout 
     android:id="@+id/dialog_fragment_container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:visibility="invisible"/> 

    <include android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      layout="@layout/spinner_overlay"/> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/toggle_fragment_button" 
     android:background="@color/background_white" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|right" 
     android:layout_margin="20dp" 
     android:alpha=".9"/> 

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

     <ListView android:id="@+id/left_drawer" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:choiceMode="singleChoice" 
        android:divider="@android:color/transparent" 
        android:dividerHeight="0dp" 
        android:background="#000" 
        android:alpha="0.7" 
      /> 

    </android.support.design.widget.NavigationView> 
    </android.support.design.widget.CoordinatorLayout> 
</android.support.v4.widget.DrawerLayout> 

drawer_item.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/drawer_tab" 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 
<de.hdodenhof.circleimageview.CircleImageView 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/profile_image" 
    android:layout_width="76dp" 
    android:layout_height="76dp" 
    android:src="@drawable/logo_icon_statusbar" 
    app:border_color="#FF000000" 
    android:layout_marginLeft="24dp" 
    android:layout_centerVertical="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_marginStart="24dp" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:gravity="left" 
    android:paddingBottom="4dp" 
    android:id="@+id/drawer_tab_text" 
    android:layout_alignLeft="@+id/profile_image" 
    android:layout_alignStart="@+id/profile_image" /> 

</LinearLayout> 

Warum kann nicht verrutschen ich die Schublade in die und aus?

+0

Sie haben null 'android.support.v4.widget.DrawerLayout's in Ihrem Layout. Vielleicht deshalb. – Shark

+0

könntest du die volle activity.xml posten –

+0

@Shark bitte bearbeitexml –

Antwort

3

In Bezug auf das Schubladenschließverhalten erwarte ich, dass dies mit der Struktur Ihres activity.xml zu tun hat.

Per the guidance here:

Um ein DrawerLayout zu verwenden, positionieren Sie die primäre Inhaltsansicht als erstes Kind mit einer Breite und Höhe von match_parent. Fügen Sie Schubladen als untergeordnete Ansichten nach der Hauptinhaltsansicht hinzu und legen Sie layout_gravity entsprechend fest.

Das ist also sagen, dass in Ihrem Fall, in dem Sie einen Inhaltsbereich und eine Schublade haben, sollte der DrawerLayout zwei Kinder hat; zuerst der Inhalt, dann die Schublade. In Ihrem Xml hat derzeit die DrawerLayout nur ein Kind, alles ist in der CoordinatorLayout.

Versuchen Sie, Ihre NavigationView außerhalb der CoordinatorLayout so Ihre Tätigkeit sieht wie folgt aus etwas bewegen:

<DrawerLayout> 
    <CoordinatorLayout> 
     content 
    </CoordinatorLayout> 

    <NavigationView> 
     drawer stuff 
    </NavigationView> 
</DrawerLayout>