Android Open Source - AndroidNavigationDrawerSample Abstract Base Activity






From Project

Back to project page AndroidNavigationDrawerSample.

License

The source code is released under:

Apache License

If you think the Android project AndroidNavigationDrawerSample listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * #%L//from   w w w  . j a  va 2  s. com
 * Navigation Drawer Sample Application
 * %%
 * Copyright (C) 2014 deveth0
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
package de.dev.eth0.android.sample.navigationDrawer.ui;

import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import de.dev.eth0.android.sample.navigationDrawer.view.slidingtab.SlidingTabLayout;
import de.dev.eth0.android.sample.navigationDrawer.NavigationDrawerApplication;
import de.dev.eth0.android.sample.navigationDrawer.R;
import de.dev.eth0.android.sample.navigationDrawer.navigation.NavigationDrawerPagerAdapter;

/**
 * Base-class for all activities with navigation-drawer
 *
 * @author deveth0
 */
public abstract class AbstractBaseActivity extends ActionBarActivity {



  /**
   * A {@link ViewPager} which will be used in conjunction with the {@link SlidingTabLayout} above.
   */
  private NavigationDrawerApplication application;

  /**
   * The drawer layout from the activities layout
   */
  private DrawerLayout drawerLayout;
  /**
   * The content of the drawer from acitivtie's layout
   */
  private LinearLayout drawerContent;
  /**
   * Listview used in drawer
   */
  private ListView listView;
  /**
   * Toogle in actionbar
   */
  private ActionBarDrawerToggle actionBarDrawerToggle;

  /**
   * the actionbar
   */
  private Toolbar toolbar;
  /**
   * String with all items
   */
  private String[] navigationDrawerItems;
  /**
   * The PagerAdapter used to find select a fragment if the user clicks a drawer item
   */
  private NavigationDrawerPagerAdapter navigationDrawerPagerAdapter;


  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    application = (NavigationDrawerApplication)getApplication();

    // all activities use the same basic layout
    setContentView(R.layout.activity_base);

    // load toolbar and other stuff
    toolbar = (Toolbar)findViewById(R.id.toolbar);
    navigationDrawerPagerAdapter = new NavigationDrawerPagerAdapter(getSupportFragmentManager());
    navigationDrawerItems = getResources().getStringArray(R.array.navigation_drawer_items);
    drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
    listView = (ListView)findViewById(R.id.drawer_list);
    drawerContent = (LinearLayout)findViewById(R.id.drawer_content);

    // configure actionbar
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);

    // configure drawer layout (e.g. we want a shadow to be displayed when the drawer opens)
    drawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

    // configure listview to contain all items and listen on clicks
    listView.setAdapter(new ArrayAdapter<>(this, R.layout.drawer_list_item, navigationDrawerItems));
    listView.setOnItemClickListener(new AbstractBaseActivity.DrawerItemClickListener());

    // configure toogle
    actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.app_name, R.string.app_name);
    drawerLayout.setDrawerListener(actionBarDrawerToggle);

    // if this is the first usage of the activity, we select the first item
    if (savedInstanceState == null) {
      selectItem(0);
    }
    // configure about button to start an activity
    Button aboutButton = (Button)findViewById(R.id.drawer_about);
    aboutButton.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View v) {
        startActivity(new Intent(AbstractBaseActivity.this, AboutActivity.class));
      }
    });

  }

  /**
   * @return the application
   */
  public NavigationDrawerApplication getNavigationDrawerApplication() {
    return application;
  }

  /**
   * replaces the content with the fragment which has been selected
   *
   * @param position
   */
  private void selectItem(int position) {
    // load fragment
    Fragment fragment = navigationDrawerPagerAdapter.getItem(position);

    // replace the main content
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.content, fragment).commit();

    // update selected item and title, then close the drawer
    listView.setItemChecked(position, true);
    setTitle(navigationDrawerItems[position]);
    drawerLayout.closeDrawer(drawerContent);
  }

  @Override
  public void setTitle(CharSequence title) {
    getSupportActionBar().setTitle(title);
  }

  /**
   * When using the ActionBarDrawerToggle, you must call it during onPostCreate() and onConfigurationChanged()...
   */
  @Override
  protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    actionBarDrawerToggle.syncState();
  }

  @Override
  public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    // Pass any configuration change to the drawer toggls
    actionBarDrawerToggle.onConfigurationChanged(newConfig);
  }

  /**
   * Click listener for clicks on list items in the navigation drawer. Triggers the selectItem method
   */
  private class DrawerItemClickListener implements ListView.OnItemClickListener {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
      selectItem(position);
    }
  }

}




Java Source Code List

de.dev.eth0.android.sample.navigationDrawer.NavigationDrawerApplication.java
de.dev.eth0.android.sample.navigationDrawer.navigation.NavigationDrawerPagerAdapter.java
de.dev.eth0.android.sample.navigationDrawer.navigation.SlidingTabsPagerAdapter.java
de.dev.eth0.android.sample.navigationDrawer.ui.AboutActivity.java
de.dev.eth0.android.sample.navigationDrawer.ui.AbstractBaseActivity.java
de.dev.eth0.android.sample.navigationDrawer.ui.MainActivity.java
de.dev.eth0.android.sample.navigationDrawer.ui.fragments.AboutFragment.java
de.dev.eth0.android.sample.navigationDrawer.ui.fragments.AbstractSlidingTabsFragment.java
de.dev.eth0.android.sample.navigationDrawer.ui.fragments.Fragment1.java
de.dev.eth0.android.sample.navigationDrawer.ui.fragments.Fragment2.java
de.dev.eth0.android.sample.navigationDrawer.ui.fragments.Fragment3.java
de.dev.eth0.android.sample.navigationDrawer.ui.fragments.Fragment4.java
de.dev.eth0.android.sample.navigationDrawer.ui.fragments.MainFragment.java
de.dev.eth0.android.sample.navigationDrawer.view.slidingtab.SlidingTabLayout.java
de.dev.eth0.android.sample.navigationDrawer.view.slidingtab.SlidingTabStrip.java