2016-09-17 65 views
-1

我正在開發一個應用程序,其中包含導航和滑動兩者。但是在導航中,我無法在需要點擊偵聽器的位置編寫代碼,因此當我點擊該導航部件項目時,它會響應。 下面是我的代碼:如何在導航項上設置OnClickListiner

private void init_navigator(){ 
     // Navigation Drawer 
     mDrawerLayout = (DrawerLayout) findViewById(R.id.main_activity_DrawerLayout); 
     mDrawerLayout.setStatusBarBackgroundColor(getResources().getColor(R.color.primaryDark)); 
     mScrimInsetsFrameLayout = (ScrimInsetsFrameLayout) findViewById(R.id.main_activity_navigation_drawer_rootLayout); 

     mActionBarDrawerToggle = new ActionBarDrawerToggle 
       (
         this, 
         mDrawerLayout, 
         toolbar, 
         R.string.navigation_drawer_opened, 
         R.string.navigation_drawer_closed 
       ) 

     { 
      @Override 
      public void onDrawerSlide(View drawerView, float slideOffset) 
      { 
       // Disables the burger/arrow animation by default 
       super.onDrawerSlide(drawerView, 0); 
      } 
     }; 


     mDrawerLayout.setDrawerListener(mActionBarDrawerToggle); 

     if (getSupportActionBar() != null) 
     { 
      getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
      getSupportActionBar().setDisplayShowHomeEnabled(true); 
      getSupportActionBar().setHomeButtonEnabled(true); 
     } 

     mActionBarDrawerToggle.syncState(); 

     // Navigation Drawer layout width 
     int possibleMinDrawerWidth = UtilsDevice.getScreenWidth(this) - 
       UtilsMiscellaneous.getThemeAttributeDimensionSize(this, android.R.attr.actionBarSize); 
     int maxDrawerWidth = getResources().getDimensionPixelSize(R.dimen.navigation_drawer_max_width); 

     mScrimInsetsFrameLayout.getLayoutParams().width = Math.min(possibleMinDrawerWidth, maxDrawerWidth); 
     // Set the first item as selected for the first time 
     getSupportActionBar().setTitle(R.string.toolbar_title_home); 


    } 
} 

回答

0

實現此接口的活動

NavigationView.OnNavigationItemSelectedListener 

在OnCreate中

navigationView.setNavigationItemSelectedListener(this); 

手柄下面的部分代碼在你加入這個活動

@Override 
    public boolean onNavigationItemSelected(MenuItem item) { 
    int id = item.getItemId(); 
    switch(id){ 
    case R.id.settings: 
    break; 
} 
} 
0

什麼是您使用的導航itemlist:ListView,recyclerview或只是使用字符串數組顯示項目標籤。 後置代號爲ITEMLIST 簡單的項目 點擊捕獲通過:

@Override 
    public boolean onNavigationItemSelected(MenuItem item) { 
    int id = item.getItemId(); 
    switch(id){ 
    case R.id.settings: 
    break; 
     } 
    } 
0

使用此代碼在烏拉圭回合主要活動

@Override 
    public boolean onNavigationItemSelected(MenuItem item) { 
     // Handle navigation view item clicks here. 
     int id = item.getItemId(); 

     if (id == R.id.nav_camera) { 
      // Handle the camera action 
     } else if (id == R.id.nav_gallery) { 
      Intent intent = new Intent(this, proShearch.class); 
      startActivityForResult(intent,2); 

     } else if (id == R.id.nav_slideshow) { 

     } else if (id == R.id.nav_manage) { 

     } else if (id == R.id.nav_share) { 

     } else if (id == R.id.nav_send) { 

     } 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 
     return true; 
    } 
0

您需要使用您的活動一起實現這個

implements NavigationView.OnNavigationItemSelectedListener 

並把ClickListener這樣放入

navigationView.setNavigationItemSelectedListener(this); 

和處理單擊這樣

@SuppressWarnings("StatementWithEmptyBody") 
    @Override 
    public boolean onNavigationItemSelected(MenuItem item) { 
     // Handle navigation view item clicks here. 
     int id = item.getItemId(); 
    } 
相關問題