汉堡图标单击无法在抽屉布局中

时间:2017-05-24 07:43:27

标签: android navigation-drawer

我正在我的应用中实施Navigation drawer。一切都很好。单击Hamburger icon导航抽屉未打开时,我遇到一个问题。之后,如果我滑动,导航抽屉就会成功打开。向后滑动后,我可以成功关闭导航抽屉。现在汉堡图标点击工作正常,我可以通过点击图标打开导航抽屉。

所以我接受了Log

的帮助

这里有一些代码

 toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                if (drawer.isDrawerOpen(GravityCompat.START)) {
                    drawer.closeDrawers();
                    Log.e("drawer 1","clicked");
                    return;
                } else {
                    Log.e("drawer 2","clicked");
                    drawer.openDrawer(GravityCompat.START);
                }
            }
        });



 private void setUpNavigationView() {
        int[][] states = new int[][]{
                new int[]{android.R.attr.state_enabled}, // enabled
                new int[]{-android.R.attr.state_enabled}, // disabled
                new int[]{-android.R.attr.state_checked}, // unchecked
                new int[]{android.R.attr.state_pressed},
                new int[]{android.R.attr.state_selected}  // pressed
        };

        int[] colors = new int[]{
                Color.BLACK,
                Color.RED,
                Color.GREEN,
                Color.BLUE,
                Color.WHITE

        };

        ColorStateList csl = new ColorStateList(states, colors);

        navigationView.setItemTextColor(csl);
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {


            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {

                switch (menuItem.getItemId()) {

                    case R.id.nav_home:

                        drawer.closeDrawers();
                        break;
                    case R.id.nav_fav:
                        if (sharedPreferences.contains("favHotelId")) {

                            searchNearHotel("favHtl");
                        }
                            else
                                Toast.makeText(getApplicationContext(),"No favorite hotels",Toast.LENGTH_SHORT).show();


                        break;

                    case R.id.nav_sign_out:                        drawer.closeDrawers();
                        break;
                }
                if (menuItem.isChecked()) {
                    menuItem.setChecked(false);
                } else {
                    menuItem.setChecked(true);
                }
                menuItem.setChecked(true);

                loadHomeFragment();

                return true;
            }
        });

        final ActionBarDrawerToggle mDrawerToggle  = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.openDrawer, R.string.closeDrawer) {

            @Override
            public void onDrawerClosed(View drawerView) {
                // Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank
                super.onDrawerClosed(drawerView);
                invalidateOptionsMenu();

            }

            @Override
            public void onDrawerOpened(View drawerView) {
               // Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank
                super.onDrawerOpened(drawerView);
                drawer.openDrawer(drawerView);
                Log.e("drawer 3","clicked");
                invalidateOptionsMenu();
            }
        };

        //Setting the actionbarToggle to drawer layout
        drawer.setDrawerListener(mDrawerToggle);
        mDrawerToggle.syncState();

        //calling sync state is necessary or else your hamburger icon wont show up


        View header = navigationView.getHeaderView(0);
         tv_email = (TextView) header.findViewById(R.id.drawer_userName);
        drawer_userNameEmail = (TextView) 
        }
    }

当我第一次点击图标时,public void onDrawerOpened(View drawerView)方法没有被调用,这就是我的抽屉没有打开的原因。在导航抽屉上swipe gesture之后,成功调用该方法。

请帮我找出问题所在。

2 个答案:

答案 0 :(得分:0)

Activity

中覆盖此方法
 @Override
public boolean onOptionsItemSelected(MenuItem item) {
    //return true for ActionBarToggle to handle the touch event
    if (mDrawerToggle.onOptionsItemSelected(item)) {
      return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    mDrawerToggle.syncState();
}

答案 1 :(得分:0)

只需使用android studio模板创建导航抽屉。

遵循以下流程:

 fileMenu-->new-->newProject-->configure your new project-->select navigation drawer activity-->next/finish

这是一些参考。用于演示:

navigation 1

navigation 2