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

标签 android android-fragments navigation-drawer

正如 Android Developers 上的页面所说

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 默认具有其 fragment 之一的布局。那么是这个原因吗?

编辑:我的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" />

最佳答案

您的“NavigationDrawerFragment”已经在其“setUp”方法中为您创建了一个抽屉开关。

您不应该在“MainPage” Activity 中创建一个新的。


注意事项: 您可以在 NavigationDrawerFragment 中使用 android.support.v7.app.ActionBarDrawerToggle 而不是 v4


更新:

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

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

关于android - 从左侧滑动时抽屉导航未打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28251623/

相关文章:

java - 使用 for 循环使用 TextView 创建线性布局

java - 带有 imageview 的 Android 抽屉导航

android - 如何检查除法的结果是int还是float

android - 访问 GATT 错误代码而不是 int

Android PlusClient 实现 & 获取 token

android - FragmentStatePagerAdapter 处理 getItem() 位置

android - 如何更改android中OptionsMenu的默认图标?

java - fragment 中的后退在主 Activity 中运行函数

android - 在 Android App Bundle 中添加 SwitchCompat 后找不到资源错误 res/drawable/abc_switch_thumb_material.xml

android - 双工具栏显示在 fragment 上