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>
爲什麼我不能滑動抽屜進出?
您的佈局零'android.support.v4.widget.DrawerLayout's。也許這就是爲什麼。 – Shark
你可以發佈完整的activity.xml –
@Shark請參閱編輯xml –