public boolean navigationDrawer(final int itemId) { // <https://stackoverflow.com/q/30752713/> if (!alive() || transitioning()) { LogHelper.warning("Switching Fragments too early"); return false; } transitioning(true); // Clears any loading state and capability refreshable(false, null); // Switch Fragment final SkeletonFragment skeletonFragment = getFragment(itemId); if (skeletonFragment == null) { LogHelper.warning("SkeletonFragment was NULL"); transitioning(false); return false; } getSupportFragmentManager() .beginTransaction() .replace(R.id.sk_frameLayout, skeletonFragment) .addToBackStack(null) .commit(); // Updates NavigationDrawer final MenuItem menuItem = mNavigationView.getMenu().findItem(itemId); if (menuItem != null) { menuItem.setChecked(true); } closeNavigationDrawer(); onNavigationDrawerClosed(); return true; }
@Override protected void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.sk_navigationdraweractivity); home(true); mDrawerLayout = (DrawerLayout) findViewById(R.id.sk_drawerLayout); if (mDrawerLayout == null) { LogHelper.warning("DrawerLayout was NULL"); return; } mDrawerToggle = new ActionBarDrawerToggle( SkeletonNavigationDrawerActivity.this, mDrawerLayout, android.R.string.ok, android.R.string.cancel) { @Override public void onDrawerOpened(@NonNull final View view) { onNavigationDrawerOpened(); } @Override public void onDrawerClosed(@NonNull final View view) { onNavigationDrawerClosed(); } }; mDrawerLayout.setDrawerListener(mDrawerToggle); mNavigationView = (NavigationView) findViewById(R.id.sk_navigationView); if (mNavigationView == null) { LogHelper.warning("NavigationView was NULL"); return; } if (AndroidHelper.api() >= AndroidHelper.API_21) { mNavigationView.setPadding(0, (int) getResources().getDimension(R.dimen.statusBar), 0, 0); } mNavigationView.setNavigationItemSelectedListener( new NavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull final MenuItem menuItem) { navigationDrawer(menuItem.getItemId()); return true; } }); }