【问题标题】:Android Navigation Drawer not being closed by dragging itAndroid Navigation Drawer 不会通过拖动来关闭
【发布时间】:2015-03-11 23:04:26
【问题描述】:

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

这是我的 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>

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

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

【问题讨论】:

  • 发布您的布局文件
  • 我编辑了我的第一篇文章。
  • 您的 DrawerLayout 设置有点偏离。应该首先列出主要内容视图。交换 TextView 和 ListView 的顺序。
  • 谢谢!抽屉现在可以按预期滑动,但是当它打开时,后退按钮仍然会杀死应用程序而不是关闭菜单。 this 是实现这种行为的好方法吗?
  • 是什么扼杀了应用程序?检查 logcat 中的特定异常。

标签: android android-ui navigation-drawer


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-18
    • 1970-01-01
    • 1970-01-01
    • 2021-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多