Android NavigationView抽屉未关闭

时间:2016-03-24 15:15:24

标签: java android xml navigation-drawer android-navigationview

实现NavigationView抽屉 - 我能够生成抽屉并看到它,但无法通过滑动关闭它。此外,NavigationItemSelectedListener似乎没有正确设置,因为我无法检测项目上的点击事件。

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>

为什么我不能将抽屉滑入和拉出?

1 个答案:

答案 0 :(得分:3)

关于抽屉关闭行为,我希望这与您activity.xml的结构有关。

根据the guidance here

  

要使用DrawerLayout,请将主要内容视图定位为宽度和高度为match_parent的第一个子视图。在主内容视图后添加抽屉作为子视图,并相应地设置layout_gravity

所以这就是说,在你有一个内容部分和一个抽屉的情况下,DrawerLayout应该有两个孩子;首先是内容,然后是抽屉。在您的xml中,DrawerLayout只有一个孩子,所有内容都在CoordinatorLayout

尝试将NavigationView移到CoordinatorLayout之外,以便您的活动如下所示:

<DrawerLayout>
    <CoordinatorLayout>
        content
    </CoordinatorLayout>

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