4

I'm trying to implement a Navigation Drawer and I managed successfully to integrate it with the Action Bar: it opens and closes when the application button is pressed and it closes when I press on a blank part of the screen, but it does not allow to be dragged back to its original position. Once opened, I can't close it with a swipe (as in every application I have 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 the 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.

ADDENDUM: Additionally, the back button won't close the Drawer.

turlando
  • 174
  • 11

1 Answers1

1

I had the very same issue : the drawer could be opened by a swipe, but could only be closed by a click on the actionbar button.

After some research, it appeared that the underlying activity had several critical bugs : some fragments were added which were adding an un-initialized pager (with a null adapter)

Anyway, when I fixed these bugs (which had nothing to do with the drawer implementation), then behavior went correct.

Orabîg
  • 11,718
  • 6
  • 38
  • 58