Android Navigation drawer does not close by dragging it
I am trying to implement a navigation drawer and I have been able to successfully integrate it with the action bar: it opens and closes when the app button is pressed and closes when I click on a blank part of the screen, but this does not allow me to return to its original position. Once opened, I can't close it with a swipe (as in every app I've seen so far).
Here is my Java code:
package com.example.notificationswhisperer;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
public class MainActivity
extends Activity {
private DrawerLayout drawerLayout;
private ListView drawerList;
private ActionBarDrawerToggle drawerToggle;
private List<AppDrawerItem> drawerItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notifications_whisperer);
drawerItems = new ArrayList<AppDrawerItem>();
drawerItems.add(
new AppDrawerItem("Settings",
getResources().getDrawable(R.drawable.ic_action_settings)));
drawerItems.add(
new AppDrawerItem("Test",
getResources().getDrawable(R.drawable.ic_action_settings)));
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
drawerLayout = (DrawerLayout) findViewById(R.id.layout_app_drawer);
drawerToggle = new ActionBarDrawerToggle(
this, // Context
drawerLayout, // DrawerLayout object
R.drawable.ic_drawer, // Drawer icon
0, // Open Drawer description
0) { // Closed Drawer description
public void onDrawerOpened(View view) {
super.onDrawerOpened(view);
invalidateOptionsMenu();
}
public void onDrawerSlide(View view, float offset) {
if (offset > .5) {
onDrawerOpened(view);
} else {
onDrawerClosed(view);
}
}
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
invalidateOptionsMenu();
}
};
drawerLayout.setDrawerListener(drawerToggle);
drawerLayout.setDrawerShadow(R.drawable.drawer_shadow, Gravity.START);
drawerList = (ListView) findViewById(R.id.list_app_drawer);
drawerList.setAdapter(new AppDrawerAdapter(this, drawerItems));
drawerList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
drawerList.setItemChecked(position, true);
drawerLayout.closeDrawer(drawerList);
}
});
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
drawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration config) {
super.onConfigurationChanged(config);
drawerToggle.onConfigurationChanged(config);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.notifications_whisperer, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (drawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
This is my layout for the activity:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout_app_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.notificationswhisperer.NotificationsWhisperer" >
<ListView
android:id="@+id/list_app_drawer"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/black"
android:dividerHeight="0.5dp"
android:background="@android:color/white" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</android.support.v4.widget.DrawerLayout>
And this is for list entries:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="48dp"
android:orientation="vertical" >
<ImageView
android:id="@+id/list_item_app_drawer_image"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginLeft="8dp"
android:layout_centerVertical="true" />
<TextView
android:id="@+id/list_item_app_drawer_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/list_item_app_drawer_image"
android:textSize="18sp" />
</RelativeLayout>
Am I missing something? Thank you in advance.
ADDITION: Also, the Back button does not close the drawer.
source to share
I had the same problem: the drawer can be opened with a swipe, but it can only be closed by clicking on the action bar button.
After some research, it turned out that the basic action had several critical bugs: some snippets were added that added an uninitialized pager (with a null adapter)
Anyway, when I fixed those bugs (which had nothing to do with the box implementation) the behavior went right.
source to share