Android Open Source - android-slide-menu Slide Menu






From Project

Back to project page android-slide-menu.

License

The source code is released under:

Apache License

If you think the Android project android-slide-menu 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.github.takuji31.slidemenu;
//from   ww w .j a  v  a 2  s  .  co m

import com.github.takuji31.appbase.util.PixelUtil;
import com.github.takuji31.appbase.util.ViewGroupUtil;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;

public class SlideMenu {
  private static int sAnimationDuration = 500;
  
  public static void setAnimationDuration(int duration) {
    sAnimationDuration = duration;
  }
  
  private static boolean sMenuShown = false;
  private static View sMenu;
  private static LinearLayout sContent;
  private static FrameLayout sParent;
  private static int sMenuSize;
  private static int sStatusHeight = 0;
  private Activity mActivity;
  private SlideMenuAdapter mAdapter;
  private OnItemClickListener mItemClickListener;

  public SlideMenu(Activity activity) {
    mActivity = activity;
  }

  // call this in your onCreate() for screen rotation
  public void checkEnabled() {
    if (sMenuShown) {
      show(false);
    }
  }

  public void show() {
    // get the height of the status bar
    if (sStatusHeight == 0) {
      Rect rectgle = new Rect();
      Window window = mActivity.getWindow();
      window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
      sStatusHeight = rectgle.top;
    }
    show(true);
  }

  public void show(boolean animate) {
    sMenuSize = PixelUtil.dpToPixel(250, mActivity);
    sContent = ((LinearLayout) mActivity.findViewById(android.R.id.content)
        .getParent());
    FrameLayout.LayoutParams parm = (FrameLayout.LayoutParams) sContent
        .getLayoutParams();
    parm.setMargins(sMenuSize, 0, -sMenuSize, 0);
    sContent.setLayoutParams(parm);
    // animation for smooth slide-out
    TranslateAnimation ta = new TranslateAnimation(-sMenuSize, 0, 0, 0);
    ta.setDuration(sAnimationDuration);
    if (animate) {
      sContent.startAnimation(ta);
    }
    sParent = (FrameLayout) sContent.getParent();
    LayoutInflater inflater = (LayoutInflater) mActivity
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    sMenu = inflater.inflate(R.layout.menu, null);
    FrameLayout.LayoutParams lays = new FrameLayout.LayoutParams(-1, -1, 3);
    lays.setMargins(0, sStatusHeight, 0, 0);
    sMenu.setLayoutParams(lays);
    sParent.addView(sMenu);
    ListView list = (ListView) mActivity.findViewById(R.id.menu_listview);
    list.setOnItemClickListener(mItemClickListener);
    if (animate) {
      sMenu.startAnimation(ta);
    }
    sMenu.findViewById(R.id.overlay).setOnClickListener(
        new OnClickListener() {
          @Override
          public void onClick(View v) {
            hide();
          }
        });
    ViewGroupUtil.enableDisableViewGroup(
        (LinearLayout) sParent.findViewById(android.R.id.content)
            .getParent(), false);
    sMenuShown = true;
    list.setAdapter(mAdapter);
  }

  public void setAdapter(SlideMenuAdapter adapter) {
    mAdapter = adapter;
  }
  
  public void setOnItemClickListener(OnItemClickListener listener) {
    mItemClickListener = listener;
  }

  public void hide() {
    TranslateAnimation ta = new TranslateAnimation(0, -sMenuSize, 0, 0);
    ta.setDuration(sAnimationDuration);
    sMenu.startAnimation(ta);
    sParent.removeView(sMenu);

    TranslateAnimation tra = new TranslateAnimation(sMenuSize, 0, 0, 0);
    tra.setDuration(sAnimationDuration);
    sContent.startAnimation(tra);
    FrameLayout.LayoutParams parm = (FrameLayout.LayoutParams) sContent
        .getLayoutParams();
    parm.setMargins(0, 0, 0, 0);
    sContent.setLayoutParams(parm);
    ViewGroupUtil.enableDisableViewGroup(
        (LinearLayout) sParent.findViewById(android.R.id.content)
            .getParent(), true);
    sMenuShown = false;
  }

  public boolean isMenuShown() {
    return sMenuShown;
  }
  
  public void toggle() {
    if (sMenuShown) {
      hide();
    } else {
      show();
    }
  }
}




Java Source Code List

com.github.takuji31.slidemenu.SlideMenuAdapter.java
com.github.takuji31.slidemenu.SlideMenuItem.java
com.github.takuji31.slidemenu.SlideMenu.java