Code:
</pre><span style="font-size:18px"></span></p><p><pre name="code" class="java">import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.Display;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.Scroller;
/**
* modify SlidingMenu , reference:
* note:this sliding menu which we can touch left side only
*
* @author xuchun
*
*/
@SuppressLint("Recycle")
public class SlidingMenu extends RelativeLayout {
private View mSlidingView;
private View mMenuView;
private View mDetailView;
private View bgShadeContent;
private RelativeLayout bgShade;
private int screenWidth;
private int screenHeight;
private Context mContext;
private Scroller mScroller;
private VelocityTracker mVelocityTracker;
private int mTouchSlop;
private float mLastMotionX;
private float mLastMotionY;
private float mFirstMotionX;
private static final int VELOCITY = 50;
private boolean mIsBeingDragged = true;
private boolean tCanSlideLeft = true;
private boolean tCanSlideRight = false;
private boolean hasClickLeft = false;
private boolean hasClickRight = false;
public SlidingMenu(Context context) {
super(context);
mContext = context;
init(context);
}
@SuppressWarnings("deprecation")
private void init(Context context) {
mContext = context;
bgShade = new RelativeLayout(context);
mScroller = new Scroller(getContext());
mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
if (!isInEditMode()) {
WindowManager windowManager = ((Activity) context).getWindow()
.getWindowManager();
Display display = windowManager.getDefaultDisplay();
screenWidth = display.getWidth();
screenHeight = display.getHeight();
}
LayoutParams bgParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
bgParams.addRule(RelativeLayout.CENTER_IN_PARENT);
bgShade.setLayoutParams(bgParams);
}
public SlidingMenu(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
init(context);
}
public SlidingMenu(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mContext = context;
init(context);
}
/**
* add three sides views
*
* @param left
* @param center
* @param right
*/
public void addViews(View left, View center, View right) {
setLeftView(left);
setRightView(right);
setCenterView(center);
}
/**
* set left side view
*
* @param view
*/
@SuppressWarnings("deprecation")
public void setLeftView(View view) {
LayoutParams behindParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.FILL_PARENT);
if (null != mMenuView) {
removeView(mMenuView);
}
addView(view, behindParams);
mMenuView = view;
}
/**
* set right side view
*
* @param view
*/
@SuppressWarnings("deprecation")
public void setRightView(View view) {
LayoutParams behindParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.FILL_PARENT);
behindParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
addView(view, behindParams);
mDetailView = view;
}
/**
* this method can replace this center show view
*
* @param view
*/
@SuppressWarnings("deprecation")
public void replaceCenterView(View view) {
LayoutParams aboveParams = new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT);
LayoutParams bgParams = new LayoutParams(screenWidth, screenHeight);
bgParams.addRule(RelativeLayout.CENTER_IN_PARENT);
bgShadeContent = new View(mContext);
bgShadeContent.setBackgroundDrawable(getResources().getDrawable(
R.drawable.shade_bg));
// remove old
if (null != bgShade) {
removeView(bgShade);
bgShade = new RelativeLayout(mContext);
}
bgShade.addView(bgShadeContent, bgParams);
addView(bgShade, bgParams);
// remove old
if (null != mSlidingView) {
removeView(mSlidingView);
}
addView(view, aboveParams);
mSlidingView = view;
}
/**
* set center view
*
* @param view
*/
@SuppressWarnings("deprecation")
public void setCenterView(View view) {
LayoutParams aboveParams = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
LayoutParams bgParams = new LayoutParams(screenWidth, screenHeight);
bgParams.addRule(RelativeLayout.CENTER_IN_PARENT);
bgShadeContent = new View(mContext);
bgShadeContent.setBackgroundDrawable(getResources().getDrawable(
R.drawable.shade_bg));
bgShade.addView(bgShadeContent, bgParams);
// remove old
if (null != bgShade) {
removeView(bgShade);
}
addView(bgShade, bgParams);
view.setLayoutParams(aboveParams);
mSlidingView = view;
mSlidingView.bringToFront();
}
@Override
public void scrollTo(int x, int y) {
super.scrollTo(x, y);
postInvalidate();
}
@Override
public void computeScroll() {
if (!mScroller.isFinished()) {
if (mScroller.computeScrollOffset()) {
int oldX = mSlidingView.getScrollX();
int oldY = mSlidingView.getScrollY();
int x = mScroller.getCurrX();
int y = mScroller.getCurrY();
if (oldX != x || oldY != y) {
if (mSlidingView != null) {
mSlidingView.scrollTo(x, y);
if (x < 0)
// Backgroud shade turn to right
bgShade.scrollTo(x + 20, y);
else
// Backgroud shade turn to right
bgShade.scrollTo(x - 20, y);
}
}
invalidate();
}
}
}
private boolean canSlideLeft = true;
private boolean canSlideRight = false;
public void setCanSliding(boolean left, boolean right) {
canSlideLeft = left;
canSlideRight = right;
}
/**
* intercept touch event
*/
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
final int action = ev.getAction();
final float x = ev.getX();
final float y = ev.getY();
switch (action) {
case MotionEvent.ACTION_DOWN:
mFirstMotionX = x;
mLastMotionX = x;
mLastMotionY = y;
mIsBeingDragged = false;
if (canSlideLeft) {
mMenuView.setVisibility(View.VISIBLE);
mDetailView.setVisibility(View.INVISIBLE);
}
if (canSlideRight) {
mMenuView.setVisibility(View.INVISIBLE);
mDetailView.setVisibility(View.VISIBLE);
}
break;
case MotionEvent.ACTION_MOVE:
final float dx = x - mLastMotionX;
final float xDiff = Math.abs(dx);
if (dx > 0 && mFirstMotionX > screenWidth / 2) {
return false;
}
final float yDiff = Math.abs(y - mLastMotionY);
if (xDiff > mTouchSlop && xDiff > yDiff) {
if (canSlideLeft) {
float oldScrollX = mSlidingView.getScrollX();
if (oldScrollX < 0) {
mIsBeingDragged = true;
mLastMotionX = x;
} else {
if (dx > 0) {
mIsBeingDragged = true;
mLastMotionX = x;
}
}
} else if (canSlideRight) {
float oldScrollX = mSlidingView.getScrollX();
if (oldScrollX > 0) {
mIsBeingDragged = true;
mLastMotionX = x;
} else {
if (dx < 0) {
mIsBeingDragged = true;
mLastMotionX = x;
}
}
}
}
break;
}
return mIsBeingDragged;
}
/**
* intercept touch event
*/
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (mVelocityTracker == null) {
mVelocityTracker = VelocityTracker.obtain();
}
mVelocityTracker.addMovement(ev);
final int action = ev.getAction();
final float x = ev.getX();
final float y = ev.getY();
switch (action) {
case MotionEvent.ACTION_DOWN:
mFirstMotionX = x;
if (!mScroller.isFinished()) {
mScroller.abortAnimation();
}
mLastMotionX = x;
mLastMotionY = y;
if (mSlidingView.getScrollX() == -getMenuViewWidth()
&& mLastMotionX < getMenuViewWidth()) {
return false;
}
if (mSlidingView.getScrollX() == getDetailViewWidth()
&& mLastMotionX > getMenuViewWidth()) {
return false;
}
break;
case MotionEvent.ACTION_MOVE:
if ((x - mFirstMotionX) > 0 && mFirstMotionX > screenWidth / 2) {
return false;
}
if (mIsBeingDragged) {
final float deltaX = mLastMotionX - x;
mLastMotionX = x;
float oldScrollX = mSlidingView.getScrollX();
float scrollX = oldScrollX + deltaX;
if (canSlideLeft) {
if (scrollX > 0)
scrollX = 0;
}
if (canSlideRight) {
if (scrollX < 0)
scrollX = 0;
}
if (deltaX < 0 && oldScrollX < 0) { // left view
final float leftBound = 0;
final float rightBound = -getMenuViewWidth();
if (scrollX > leftBound) {
scrollX = leftBound;
} else if (scrollX < rightBound) {
scrollX = rightBound;
}
} else if (deltaX > 0 && oldScrollX > 0) { // right view
final float rightBound = getDetailViewWidth();
final float leftBound = 0;
if (scrollX < leftBound) {
scrollX = leftBound;
} else if (scrollX > rightBound) {
scrollX = rightBound;
}
}
if (mSlidingView != null) {
mSlidingView.scrollTo((int) scrollX,
mSlidingView.getScrollY());
if (scrollX < 0)
bgShade.scrollTo((int) scrollX + 20,
mSlidingView.getScrollY());
else
bgShade.scrollTo((int) scrollX - 20,
mSlidingView.getScrollY());
}
}
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
if ((x - mFirstMotionX) > 0 && mFirstMotionX > screenWidth / 2) {
mFirstMotionX = 0;
return true;
}
mFirstMotionX = 0;
if (mIsBeingDragged) {
final VelocityTracker velocityTracker = mVelocityTracker;
velocityTracker.computeCurrentVelocity(100);
float xVelocity = velocityTracker.getXVelocity();// move speed
int oldScrollX = mSlidingView.getScrollX();
int dx = 0;
if (oldScrollX <= 0 && canSlideLeft) {// left view
if (xVelocity > VELOCITY) {
dx = -getMenuViewWidth() - oldScrollX;
} else if (xVelocity < -VELOCITY) {
dx = -oldScrollX;
if (hasClickLeft) {
hasClickLeft = false;
setCanSliding(tCanSlideLeft, tCanSlideRight);
}
} else if (oldScrollX < -getMenuViewWidth() / 2) {
dx = -getMenuViewWidth() - oldScrollX;
} else if (oldScrollX >= -getMenuViewWidth() / 2) {
dx = -oldScrollX;
if (hasClickLeft) {
hasClickLeft = false;
setCanSliding(tCanSlideLeft, tCanSlideRight);
}
}
}
if (oldScrollX >= 0 && canSlideRight) {
if (xVelocity < -VELOCITY) {
dx = getDetailViewWidth() - oldScrollX;
} else if (xVelocity > VELOCITY) {
dx = -oldScrollX;
if (hasClickRight) {
hasClickRight = false;
setCanSliding(tCanSlideLeft, tCanSlideRight);
}
} else if (oldScrollX > getDetailViewWidth() / 2) {
dx = getDetailViewWidth() - oldScrollX;
} else if (oldScrollX <= getDetailViewWidth() / 2) {
dx = -oldScrollX;
if (hasClickRight) {
hasClickRight = false;
setCanSliding(tCanSlideLeft, tCanSlideRight);
}
}
}
smoothScrollTo(dx);
}
break;
}
return true;
}
private int getMenuViewWidth() {
if (mMenuView == null) {
return 0;
}
return mMenuView.getWidth();
}
private int getDetailViewWidth() {
if (mDetailView == null) {
return 0;
}
return mDetailView.getWidth();
}
void smoothScrollTo(int dx) {
int duration = 500;
int oldScrollX = mSlidingView.getScrollX();
mScroller.startScroll(oldScrollX, mSlidingView.getScrollY(), dx,
mSlidingView.getScrollY(), duration);
invalidate();
}
/*
* show left side view
*/
public void showLeftView() {
int menuWidth = mMenuView.getWidth();
int oldScrollX = mSlidingView.getScrollX();
if (oldScrollX == 0) {
mMenuView.setVisibility(View.VISIBLE);
mDetailView.setVisibility(View.INVISIBLE);
smoothScrollTo(-menuWidth);
tCanSlideLeft = canSlideLeft;
tCanSlideRight = canSlideRight;
hasClickLeft = true;
setCanSliding(true, false);
} else if (oldScrollX == -menuWidth) {
smoothScrollTo(menuWidth);
if (hasClickLeft) {
hasClickLeft = false;
setCanSliding(tCanSlideLeft, tCanSlideRight);
}
}
}
/**
* if left menu is show, return true , else return false
*
* @return
*/
public boolean isLeftShowed() {
int oldScrollX = mSlidingView.getScrollX();
if (oldScrollX == 0) {
return false;
}
if (oldScrollX < 0) {
return true;
}
return false;
}
/*
* right side view
*/
public void showRightView() {
int menuWidth = mDetailView.getWidth();
int oldScrollX = mSlidingView.getScrollX();
if (oldScrollX == 0) {
mMenuView.setVisibility(View.INVISIBLE);
mDetailView.setVisibility(View.VISIBLE);
smoothScrollTo(menuWidth);
tCanSlideLeft = canSlideLeft;
tCanSlideRight = canSlideRight;
hasClickRight = true;
setCanSliding(false, true);
} else if (oldScrollX == menuWidth) {
smoothScrollTo(-menuWidth);
if (hasClickRight) {
hasClickRight = false;
setCanSliding(tCanSlideLeft, tCanSlideRight);
}
}
}
}