2

当我尝试在滑动操作时显示导航抽屉时,有时它不会打开(您可以在链接的屏幕截图中看到它),当我慢慢滑动时它出现的频率更高。它也出现在 google 提供的应用程序示例中。

在此处输入图像描述

MenuActivity.java

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_menu);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    // Burger button
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);

    // Getting navigation view from activity_menu.xml
    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    // Getting Navigation Header resources
    View headerLayout = navigationView.getHeaderView(0);
    emailTextViewNavHeader = (TextView) headerLayout.findViewById(R.id.textViewEmailNavHeaderMenu);
    nameTextViewNavHeader = (TextView) headerLayout.findViewById(R.id.textViewNameNavHeaderMenu);
    imageViewProfileNavHeader = (CircularImageView) headerLayout.findViewById(R.id.imageViewProfileNavHeaderMenu);

    this.setViewResources();
    this.fillInReflexionMenuList();
}

活动菜单.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout         
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start"
android:background="#555555">

<include
    android:id="@+id/main"
    layout="@layout/app_bar_menu"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_menu"
    app:menu="@menu/activity_menu_drawer" />

</android.support.v4.widget.DrawerLayout>
4

1 回答 1

0

使用以下代码进行处理和检查:

首先,将其添加到上面。

private DrawerLayout drawerLayout;    

并且,在 OnCreate 方法中:

navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {

            if (menuItem.isChecked()) menuItem.setChecked(false);
            else menuItem.setChecked(true);
            drawerLayout.closeDrawers();
            switch (menuItem.getItemId()) {

                case R.id.home:
                    // do some stuffs
                    return true;

                default:
                    return true;
            }
        }
    });

也添加这个,我认为它会用这些代码修复:

    drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
            ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.openDrawer, R.string.closeDrawer) {

                @Override
                public void onDrawerClosed(View drawerView) {
                    super.onDrawerClosed(drawerView);
                }

                @Override
                public void onDrawerOpened(View drawerView) {
                    super.onDrawerOpened(drawerView);
                }
            };
      drawerLayout.setDrawerListener(actionBarDrawerToggle);
            actionBarDrawerToggle.syncState();
于 2016-01-13T15:05:58.857 回答