2017-04-05 97 views
-3

我有一個Android Studio的抽屜菜單。如何在點擊抽屜菜單項時更改活動?

我的菜單代碼:

 //Menü kodları// 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
        .setAction("Action", null).show(); 
     } 
    }); 

    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(); 

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
    navigationView.setNavigationItemSelectedListener(this); 
    navigationView.setItemIconTintList(null); 
} 

@Override 
public void onBackPressed() { 
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    if (drawer.isDrawerOpen(GravityCompat.START)) { 
     drawer.closeDrawer(GravityCompat.START); 
    } else { 
     super.onBackPressed(); 
    } 
} 

@Override 
public boolean onNavigationItemSelected(MenuItem item) { 
    int id = item.getItemId(); 
    if (id == R.id.menu_fbdown) { 

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

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

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

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

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

    } 

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

}

我要讓clicktable此菜單。我厭倦了這樣的代碼:

 Intent i = new Intent(FBDownActivity.this, FBDownActivity.class); 
     startActivity(i); 

像這樣:

@Override 
public boolean onNavigationItemSelected(MenuItem item) { 
    int id = item.getItemId(); 
    if (id == R.id.menu_fbdown) { 
     Intent i = new Intent(FBDownActivity.this, FBDownActivity.class); 
     startActivity(i); 
    } else if (id == R.id.menu_instadown) { 
     Intent i = new Intent(FBDownActivity.this, FBDownActivity.class); 
     startActivity(i); 
    } else if (id == R.id.menu_twitdown) { 

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

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

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

    } 

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

但它沒有工作。仍然無法點擊項目。 我能做些什麼?

謝謝。

+0

它確實有效。它開始了相同的活動 –

+0

代碼看起來正確。它應該工作。嘗試開始不同的活動以明顯注意到變化。 –

+0

如果你有時間,我給你團隊查看器ID並連接我,並期待項目?如果你有時間。 –

回答

0

您正在嘗試啓動與您相同的活動。更改代碼如下:

@Override 
public boolean onNavigationItemSelected(MenuItem item) { 
    int id = item.getItemId(); 
    if (id == R.id.menu_fbdown) { 
     Intent i = new Intent(YourCurrentActivty.this, ActivityToOpen.class); 
     startActivity(i); 
    } else if (id == R.id.menu_instadown) { 
     Intent i = new Intent(YourCurrentActivty.this, ActivityToOpen.class); 
     startActivity(i); 
    } else if (id == R.id.menu_twitdown) { 

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

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

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

    } 

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

我知道這只是我改變的例子,但仍然沒有進行新的活動。 –

相關問題