Android Open Source - CampusUB1 Sliding Activity Helper






From Project

Back to project page CampusUB1.

License

The source code is released under:

Apache License

If you think the Android project CampusUB1 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

package com.slidingmenu.lib.app;
/*from   w  w w  .  java2 s  . c  om*/
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;

import com.slidingmenu.lib.R;
import com.slidingmenu.lib.SlidingMenu;

public class SlidingActivityHelper {

  private Activity mActivity;

  private SlidingMenu mSlidingMenu;

  private View mViewAbove;

  private View mViewBehind;

  private boolean mBroadcasting = false;

  private boolean mOnPostCreateCalled = false;

  private boolean mEnableSlide = true;

  /**
   * Instantiates a new SlidingActivityHelper.
   *
   * @param activity the associated activity
   */
  public SlidingActivityHelper(Activity activity) {
    mActivity = activity;
  }

  /**
   * Sets mSlidingMenu as a newly inflated SlidingMenu. Should be called within the activitiy's onCreate()
   *
   * @param savedInstanceState the saved instance state (unused)
   */
  public void onCreate(Bundle savedInstanceState) {
    mSlidingMenu = (SlidingMenu) LayoutInflater.from(mActivity).inflate(R.layout.slidingmenumain, null);
  }

  /**
   * Further SlidingMenu initialization. Should be called within the activitiy's onPostCreate()
   *
   * @param savedInstanceState the saved instance state (unused)
   */
  public void onPostCreate(Bundle savedInstanceState) {
    if (mViewBehind == null || mViewAbove == null) {
      throw new IllegalStateException("Both setBehindContentView must be called " +
          "in onCreate in addition to setContentView.");
    }

    mOnPostCreateCalled = true;

    mSlidingMenu.attachToActivity(mActivity, 
        mEnableSlide ? SlidingMenu.SLIDING_WINDOW : SlidingMenu.SLIDING_CONTENT);
    
    final boolean open;
    final boolean secondary;
    if (savedInstanceState != null) {
      open = savedInstanceState.getBoolean("SlidingActivityHelper.open");
      secondary = savedInstanceState.getBoolean("SlidingActivityHelper.secondary");
    } else {
      open = false;
      secondary = false;
    }
    new Handler().post(new Runnable() {
      public void run() {
        if (open) {
          if (secondary) {
            mSlidingMenu.showSecondaryMenu(false);
          } else {
            mSlidingMenu.showMenu(false);
          }
        } else {
          mSlidingMenu.showContent(false);          
        }
      }
    });
  }

  /**
   * Controls whether the ActionBar slides along with the above view when the menu is opened,
   * or if it stays in place.
   *
   * @param slidingActionBarEnabled True if you want the ActionBar to slide along with the SlidingMenu,
   * false if you want the ActionBar to stay in place
   */
  public void setSlidingActionBarEnabled(boolean slidingActionBarEnabled) {
    if (mOnPostCreateCalled)
      throw new IllegalStateException("enableSlidingActionBar must be called in onCreate.");
    mEnableSlide = slidingActionBarEnabled;
  }

  /**
   * Finds a view that was identified by the id attribute from the XML that was processed in onCreate(Bundle).
   * 
   * @param id the resource id of the desired view
   * @return The view if found or null otherwise.
   */
  public View findViewById(int id) {
    View v;
    if (mSlidingMenu != null) {
      v = mSlidingMenu.findViewById(id);
      if (v != null)
        return v;
    }
    return null;
  }

  /**
   * Called to retrieve per-instance state from an activity before being killed so that the state can be
   * restored in onCreate(Bundle) or onRestoreInstanceState(Bundle) (the Bundle populated by this method
   * will be passed to both). 
   *
   * @param outState Bundle in which to place your saved state.
   */
  public void onSaveInstanceState(Bundle outState) {
    outState.putBoolean("SlidingActivityHelper.open", mSlidingMenu.isMenuShowing());
    outState.putBoolean("SlidingActivityHelper.secondary", mSlidingMenu.isSecondaryMenuShowing());
  }

  /**
   * Register the above content view.
   *
   * @param v the above content view to register
   * @param params LayoutParams for that view (unused)
   */
  public void registerAboveContentView(View v, LayoutParams params) {
    if (!mBroadcasting)
      mViewAbove = v;
  }

  /**
   * Set the activity content to an explicit view. This view is placed directly into the activity's view
   * hierarchy. It can itself be a complex view hierarchy. When calling this method, the layout parameters
   * of the specified view are ignored. Both the width and the height of the view are set by default to
   * MATCH_PARENT. To use your own layout parameters, invoke setContentView(android.view.View,
   * android.view.ViewGroup.LayoutParams) instead.
   *
   * @param v The desired content to display.
   */
  public void setContentView(View v) {
    mBroadcasting = true;
    mActivity.setContentView(v);
  }

  /**
   * Set the behind view content to an explicit view. This view is placed directly into the behind view 's view hierarchy.
   * It can itself be a complex view hierarchy.
   *
   * @param view The desired content to display.
   * @param layoutParams Layout parameters for the view. (unused)
   */
  public void setBehindContentView(View view, LayoutParams layoutParams) {
    mViewBehind = view;
    mSlidingMenu.setMenu(mViewBehind);
  }

  /**
   * Gets the SlidingMenu associated with this activity.
   *
   * @return the SlidingMenu associated with this activity.
   */
  public SlidingMenu getSlidingMenu() {
    return mSlidingMenu;
  }

  /**
   * Toggle the SlidingMenu. If it is open, it will be closed, and vice versa.
   */
  public void toggle() {
    mSlidingMenu.toggle();
  }

  /**
   * Close the SlidingMenu and show the content view.
   */
  public void showContent() {
    mSlidingMenu.showContent();
  }

  /**
   * Open the SlidingMenu and show the menu view.
   */
  public void showMenu() {
    mSlidingMenu.showMenu();
  }

  /**
   * Open the SlidingMenu and show the secondary menu view. Will default to the regular menu
   * if there is only one.
   */
  public void showSecondaryMenu() {
    mSlidingMenu.showSecondaryMenu();
  }

  /**
   * On key up.
   *
   * @param keyCode the key code
   * @param event the event
   * @return true, if successful
   */
  public boolean onKeyUp(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK && mSlidingMenu.isMenuShowing()) {
      showContent();
      return true;
    }
    return false;
  }

}




Java Source Code List

android.UnusedStub.java
com.dev.campus.CampusUB1App.java
com.dev.campus.LauncherActivity.java
com.dev.campus.SettingsActivity.java
com.dev.campus.directory.Contact.java
com.dev.campus.directory.DirectoryActivity.java
com.dev.campus.directory.DirectoryAdapter.java
com.dev.campus.directory.DirectoryManager.java
com.dev.campus.event.Category.java
com.dev.campus.event.EventAdapter.java
com.dev.campus.event.EventFragment.java
com.dev.campus.event.EventMultiChoiceModeListener.java
com.dev.campus.event.EventPagerAdapter.java
com.dev.campus.event.EventParser.java
com.dev.campus.event.EventViewActivity.java
com.dev.campus.event.Event.java
com.dev.campus.event.EventsActivity.java
com.dev.campus.event.Feed.java
com.dev.campus.home.HomeActivity.java
com.dev.campus.home.HomeAdapter.java
com.dev.campus.home.HomeEntryItem.java
com.dev.campus.home.HomeItem.java
com.dev.campus.home.HomeSeparatorItem.java
com.dev.campus.map.MapActivity.java
com.dev.campus.map.Position.java
com.dev.campus.schedule.Group.java
com.dev.campus.schedule.ScheduleActivity.java
com.dev.campus.schedule.ScheduleAdapter.java
com.dev.campus.schedule.ScheduleImportService.java
com.dev.campus.schedule.ScheduleParser.java
com.dev.campus.util.AboutDialog.java
com.dev.campus.util.FilterDialog.java
com.dev.campus.util.Persistence.java
com.dev.campus.util.PrefFragment.java
com.dev.campus.util.SubscribeDialog.java
com.dev.campus.util.TimeExtractor.java
com.dev.campus.util.UpcomingEventsDialog.java
com.slidingmenu.lib.CanvasTransformerBuilder.java
com.slidingmenu.lib.CustomViewAbove.java
com.slidingmenu.lib.CustomViewBehind.java
com.slidingmenu.lib.MenuInterface.java
com.slidingmenu.lib.SlidingMenu.java
com.slidingmenu.lib.app.SlidingActivityBase.java
com.slidingmenu.lib.app.SlidingActivityHelper.java
com.slidingmenu.lib.app.SlidingActivity.java
com.slidingmenu.lib.app.SlidingFragmentActivity.java
com.slidingmenu.lib.app.SlidingListActivity.java
com.slidingmenu.lib.app.SlidingMapActivity.java
com.slidingmenu.lib.app.SlidingPreferenceActivity.java