2017-12-27 432 views
-1

我想在關閉切換按鈕後關閉導覽器。現在,只要打開切換鍵,它就會打開抽屜。選擇一個項目後,我可以關閉抽屜。再次按下Drawtoggle後關閉抽屜

是否可以執行我的方法來關閉抽屜?

這是我的方法:

@Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     return true; 
    } 
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
      case android.R.id.home: 
       mDrawerLayout.openDrawer(GravityCompat.START); //THIS OPENS THE DRAWER 
       return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    private void setupDrawerContent(NavigationView navigationView) { 
     navigationView.setNavigationItemSelectedListener(
       new NavigationView.OnNavigationItemSelectedListener() { 
        @Override 
        public boolean onNavigationItemSelected(MenuItem menuItem) { 
         menuItem.setChecked(true); 
         mDrawerLayout.closeDrawers();  //THIS CLOSES THE DRAWER; AFTER A ITEM IS SELECTED 
         return true; 
        } 
       }); 
    } 

回答

1

易我猜。只要保持它是否是開放或不引入新public boolean isDrawerOpen成員的軌道

讓我再嘗試這樣的事情

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case android.R.id.home: 
      if (!isDrawerOpen) { 
       mDrawerLayout.openDrawer(GravityCompat.START); 
       isDrawerOpen = true; 
      } else { 
       mDrawerLayout.closeDrawers(); 
       isDrawerOpen = false; 
      } 
      return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

private void setupDrawerContent(NavigationView navigationView) { 
    navigationView.setNavigationItemSelectedListener(
      new NavigationView.OnNavigationItemSelectedListener() { 
       @Override 
       public boolean onNavigationItemSelected(MenuItem menuItem) { 
        menuItem.setChecked(true); 
        mDrawerLayout.closeDrawers();  //THIS CLOSES THE DRAWER; AFTER A ITEM IS SELECTED 
        isDrawerOpen = false; 
        return true; 
       } 
      }); 
} 
+0

「isDrawerOpen」不可用:/ –

+0

像我在帖子本身說,'通過引入一個新的isDrawerOpen成員/變量' – Shark

+0

我用「mdrawerLayout.isDrawerOpen(null)」。但遺憾的是它沒有工作 –

相關問題