Android导航抽屉没有被拖动关闭

时间:2014-08-10 10:37:34

标签: android android-ui navigation-drawer

我正在尝试实现导航抽屉,我成功地将其与操作栏集成:当按下应用程序按钮时它会打开和关闭,当我按下屏幕的空白部分时它会关闭,但它不允许被拖回原来的位置。打开后,我无法通过滑动关闭它(就像我到目前为止看到的每个应用程序一样)。

这是我的Java代码:

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);
  }

}

这是我的活动布局:

<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>

这是列表条目:

<?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>

我错过了什么吗?提前谢谢。

ADDENDUM:此外,后退按钮不会关闭抽屉。

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题:可以通过滑动打开抽屉,但只能通过单击操作栏按钮来关闭。

经过一些研究,似乎底层活动有几个关键错误:添加了一些片段,这些片段添加了一个未初始化的寻呼机(使用空适配器)

无论如何,当我修复这些错误(与抽屉实现无关)时,行为就变得正确了。