从左侧滑动时导航抽屉未打开

Navigation Drawer is not opening when sliding from left

正如 Android 开发人员所说的页面

The user can bring the navigation drawer onto the screen by swiping from the left edge of the screen or by touching the application icon on the action bar.

但奇怪的是,我 activity 上的导航抽屉不响应滑动操作。它仅在触摸操作栏上的图标时切换。下面是我对导航抽屉的实现

 mNavigationDrawerFragment = (NavigationDrawerFragment)
            getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);

    // Set up the drawer.
    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mNavigationDrawerFragment.setUp(
            R.id.navigation_drawer,
            drawerLayout);

对此有什么可能的解释吗?我怀疑我的 activity 默认情况下具有其片段之一的布局。那么是这个原因吗?

编辑:我的布局文件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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainPage">

<!-- The main ocntent view -->
<FrameLayout android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</FrameLayout>


<!-- The navigation drawer-->
<fragment android:id="@+id/navigation_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:divider="@android:color/transparent"
    android:name="com.example.android.handsfree.NavigationDrawerFragment"
    tools:layout="@layout/fragment_navigation_drawer" />

你应该使用:

drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);

<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:background="@color/white" >

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<ListView
    android:id="@+id/listview_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@color/drawer"
    android:choiceMode="singleChoice" />

在你的 activity

drawlayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    listData = (ListView) findViewById(R.id.listview_drawer);

    drawlayout.setDrawerShadow(R.drawable.drawer_shadow,
            GravityCompat.START);

在您的 activity class 中添加这些字段:

private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;

然后,在你的 activity 里面:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ...
    // enabling action bar app icon and behaving it as toggle button
    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true)

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.drawable.ic_drawer, //nav menu toggle icon
                R.string.app_name, // nav drawer open - description for accessibility
                R.string.app_name // nav drawer close - description for accessibility
        ){
            public void onDrawerClosed(View view) {
                // calling onPrepareOptionsMenu() to show action bar icons
                invalidateOptionsMenu();
            }

            public void onDrawerOpened(View drawerView) {
                // calling onPrepareOptionsMenu() to hide action bar icons
                invalidateOptionsMenu();
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // toggle nav drawer on selecting action bar app icon/title
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * When using the ActionBarDrawerToggle, you must call it during
 * onPostCreate() and onConfigurationChanged()...
 */

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    // Pass any configuration change to the drawer toggls
    mDrawerToggle.onConfigurationChanged(newConfig);
}

ic_drawer 是位于可绘制资源中的 .png 图像。您可以在 Google.

上搜索此图片

API要使用的等级是11+。

希望对你有所帮助。

您的“NavigationDrawerFragment”已经在其“setUp”方法中为您创建了抽屉切换。

您应该在“主页”中创建一个新的 Activity。


备注: 您可以在 NavigationDrawerFragment.

中使用 android.support.v7.app.ActionBarDrawerToggle 而不是 v4

更新:

问题似乎已经解决了。有 2 个问题:

  1. OP 在 MainPage - Activity 中创建了第二个抽屉开关,但在 NavigationDrawerFragment 的 setUp 方法中已经创建了一个,它由 MainPage 在为了设置抽屉。 (这基本上是将抽屉中的一些东西外包[ecoding]到抽屉片段中。)
  2. OP 通过调用将 DrawerLockMode 设置为 LOCK_MODE_LOCKED_CLOSED 的方法将抽屉锁定在 onCreateOptionsMenu 内。他从未撤销此更改。

1- 将标签替换为:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainPage">

    <!-- The main ocntent view -->
    <FrameLayout android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>


    <!-- The navigation drawer-->
    <ListView android:id="@+id/navigation_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:divider="@android:color/transparent" />
    </android.support.v4.widget.DrawerLayout>

2- 在 onCreate 函数中使用此代码,您可以使用 v4 ActionBarDrawerToggle 或 v7,而我使用的是 v4:

private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;

mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

mDrawerToggle = new ActionBarDrawerToggle(this,    mDrawerLayout,R.drawable.ic_drawer,
R.string.app_name,R.string.app_name) {
                    public void onDrawerClosed(View view) {
                        getActionBar().setTitle(mTitle);
                        invalidateOptionsMenu();
                    }

                    public void onDrawerOpened(View drawerView) {
                        getActionBar().setTitle(mDrawerTitle);
                        invalidateOptionsMenu();
                    }
                };
                mDrawerLayout.setDrawerListener(mDrawerToggle);