2016-03-24 333 views
0

實現NavigationView抽屜 - 我能夠生成抽屜並看到它,但不能用滑動關閉它。此外,NavigationItemSelectedListener似乎沒有正確設置,因爲我無法檢測項目上的點擊事件。Android NavigationView抽屜不關閉

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> 

爲什麼我不能滑動抽屜進出?

+0

您的佈局零'android.support.v4.widget.DrawerLayout's。也許這就是爲什麼。 – Shark

+0

你可以發佈完整的activity.xml –

+0

@Shark請參閱編輯xml –

回答

3

關於抽屜關閉行爲,我預計這是關於你的activity.xml的結構。

作爲每the guidance here

要使用DrawerLayout,您的主要內容視圖定位爲與match_parent的寬度和高度的第一個孩子。在主內容視圖之後添加抽屜作爲子視圖,並適當地設置layout_gravity

所以這就是說,在你的情況下,你有一個內容部分和一個抽屜,DrawerLayout應該有兩個孩子;首先是內容,然後是抽屜。在你的xml目前,DrawerLayout只有一個孩子,一切都在CoordinatorLayout

請嘗試移動NavigationViewCoordinatorLayout的讓你的活動看起來是這樣的:

<DrawerLayout> 
    <CoordinatorLayout> 
     content 
    </CoordinatorLayout> 

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