1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| public class NavigationViewActivity extends AppCompatActivity { private DrawerLayout drawerLayout; private Toolbar toolbar; private NavigationView navigationView;
@Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.drawlayout_activity); if(Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT){ getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); } setSupportActionBar(toolbar=(Toolbar)findViewById(R.id.toolbar)); drawerLayout=(DrawerLayout)findViewById(R.id.drawerlayout); navigationView=(NavigationView)findViewById(R.id.navigationView);
ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open, R.string.close); actionBarDrawerToggle.syncState(); drawerLayout.addDrawerListener(actionBarDrawerToggle);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener(){
@Override public boolean onNavigationItemSelected(MenuItem menuItem) { selectDrawerItem(menuItem); return true; } }); }
private void selectDrawerItem(MenuItem menuItem) { switch (menuItem.getItemId()){ default: Toast.makeText(NavigationViewActivity.this, "menu click", Toast.LENGTH_SHORT).show(); break; } menuItem.setChecked(true); drawerLayout.closeDrawers(); } }
|