Android Open Source - BtnSlideMenu Main Activity






From Project

Back to project page BtnSlideMenu.

License

The source code is released under:

Apache License

If you think the Android project BtnSlideMenu 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.hao.btnslidemenu;
//from w w w .  jav  a 2 s .  c o  m
import com.hao.btnslidemenu.R;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends FragmentActivity implements OnClickListener {

  TextView mAppTitle;
  ImageButton mBtn;
  RadioButton mBtn_first, mBtn_second, mBtn_third;

  Fragment mFirst;
  Fragment mSecond;
  Fragment mThird;

  LinearLayout mMenuLayout;
  RelativeLayout mContentLayout;
  FrameLayout mContentView;
  LinearLayout.LayoutParams mContentParams;
  TranslateAnimation mSlide;
  int mMarginX, mAnimateFromX, mAnimateToX = 0;
  int mShadowWidth = 0;
  boolean mMenuOpen = false;

  Fragment mContent;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initView();
    initLsnr();

    mMenuLayout = (LinearLayout) findViewById(R.id.menu);
    mContentLayout = (RelativeLayout) findViewById(R.id.content);
    mContentParams = (LinearLayout.LayoutParams) mContentLayout
        .getLayoutParams();
    mContentParams.width = getWindowManager().getDefaultDisplay()
        .getWidth() + mShadowWidth;
    mContentParams.leftMargin = -(mMenuLayout.getLayoutParams().width)
        - mShadowWidth;
    mContentLayout.setLayoutParams(mContentParams);

    mFirst = new FirshFragment();
    mSecond = new SecondFragment();
    mThird = new ThirdFragment();

    getSupportFragmentManager().beginTransaction()
        .replace(R.id.contentView, mFirst).commit();
    mContent = mFirst;

  }

  @Override
  public void onClick(View v) {
    switch (v.getId()) {
    case R.id.menu_first:
      mAppTitle.setText(((TextView) v).getText().toString());
      switchContent(mContent, mFirst);
      break;
    case R.id.menu_second:
      mAppTitle.setText(((TextView) v).getText().toString());
      switchContent(mContent, mSecond);
      break;
    case R.id.menu_third:
      mAppTitle.setText(((TextView) v).getText().toString());
      switchContent(mContent, mThird);
      break;
    }
    moveIn();
  }

  /**
   * ??
   * 
   * @param from
   * @param to
   */
  public void switchContent(Fragment from, Fragment to) {
    if (from != to) {
      mContent = to;
      FragmentTransaction transaction = getSupportFragmentManager()
          .beginTransaction();
      // ????????add?
      if (!to.isAdded()) {
        // ????????fragment?add????Activity?
        transaction.hide(from).add(R.id.contentView, to).commit();
      } else {
        // ????????fragment??????
        transaction.hide(from).show(to).commit();
      }
    }
  }

  /**
   * ??????????????
   */
  public void moveIn() {
    if (mContentParams.leftMargin == -(mMenuLayout.getLayoutParams().width)
        - mShadowWidth) {
      mAnimateFromX = 0;
      mAnimateToX = (mMenuLayout.getLayoutParams().width);
      mMarginX = 0;
      mMenuOpen = true;
      setMenuLsnr();
    } else {
      mAnimateFromX = 0;
      mAnimateToX = -(mMenuLayout.getLayoutParams().width);
      mMarginX = -(mMenuLayout.getLayoutParams().width) - mShadowWidth;
      mMenuOpen = false;
      cancelMenuLsnr();
    }
    slideMenuIn(mAnimateFromX, mAnimateToX, mMarginX);
  }

  /**
   * ??
   * 
   * @param mAnimateFromX
   * @param mAnimateToX
   * @param mMarginX
   */
  public void slideMenuIn(int mAnimateFromX, int mAnimateToX,
      final int mMarginX) {
    mSlide = new TranslateAnimation(mAnimateFromX, mAnimateToX, 0, 0);
    mSlide.setDuration(300);
    mSlide.setFillEnabled(true);
    mSlide.setAnimationListener(new AnimationListener() {
      public void onAnimationEnd(Animation animation) {
        mContentParams.setMargins(mMarginX, 0, 0, 0);
        mContentLayout.setLayoutParams(mContentParams);
      }

      public void onAnimationRepeat(Animation animation) {
      }

      public void onAnimationStart(Animation animation) {
      }
    });
    mContentLayout.startAnimation(mSlide);
  }

  /**
   * onkeydown
   */
  public boolean onKeyDown(int keyCode, KeyEvent keyEvent) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
      if (mMenuOpen) {
        slideMenuIn(0, -(mMenuLayout.getLayoutParams().width),
            -(mMenuLayout.getLayoutParams().width) - mShadowWidth);
        mMenuOpen = false;
        return true;
      }
    }
    return super.onKeyDown(keyCode, keyEvent);
  }

  /**
   * ??????
   */
  private void initView() {
    mAppTitle = (TextView) findViewById(R.id.app_title);
    mBtn = (ImageButton) findViewById(R.id.menu_button);

    mBtn_first = (RadioButton) findViewById(R.id.menu_first);
    mBtn_second = (RadioButton) findViewById(R.id.menu_second);
    mBtn_third = (RadioButton) findViewById(R.id.menu_third);
  }

  /**
   * ????????
   */
  private void initLsnr() {
    setMenuLsnr();
    mBtn.setOnClickListener(this);
  }

  /**
   * ????????????
   */
  private void setMenuLsnr() {
    mBtn_first.setOnClickListener(this);
    mBtn_second.setOnClickListener(this);
    mBtn_third.setOnClickListener(this);
  }

  /**
   * ??????????????
   */
  private void cancelMenuLsnr() {
    mBtn_first.setOnClickListener(null);
    mBtn_second.setOnClickListener(null);
    mBtn_third.setOnClickListener(null);
  }

}




Java Source Code List

com.hao.btnslidemenu.FirshFragment.java
com.hao.btnslidemenu.MainActivity.java
com.hao.btnslidemenu.SecondFragment.java
com.hao.btnslidemenu.ThirdFragment.java