2017-05-24 104 views
-2

我在我的應用中實施了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)方法心不是叫這就是爲什麼我的抽屜裏是不開放。導航抽屜上的​​之後,該方法已成功調用。

請幫我找出問題所在。

+0

顯示對應於第一次點擊烏爾日誌詳細信息。 –

+1

請提供關於導航抽屜的完整代碼 –

+0

第一次只點擊'抽屜2點擊'顯示在日誌和刷卡後'抽屜3點擊'也顯示在日誌@Neo –

回答

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(); 
} 
+0

仍然沒有工作@ rafsanahmad007 –

0

只需創建採用了android工作室模板您的導航抽屜。

遵循這一流程:

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

這裏有一些參考。示範:

navigation 1

navigation 2

相關問題