From 6ca245c6c4bdb52b55f5bacd4b0deae4765cee8c Mon Sep 17 00:00:00 2001 From: jfeinstein10 Date: Sun, 16 Sep 2012 01:42:03 -0400 Subject: [PATCH 01/17] Right sliding working. No parallax scroll yet. Fading working. Shadow drawn backwards --- library/res/drawable/custom_up.xml | 15 + library/res/values/attrs.xml | 8 +- library/res/values/styles.xml | 8 + .../slidingmenu/lib/CustomUpIndicator.java | 45 +++ .../com/slidingmenu/lib/CustomViewAbove.java | 264 +++++++++++++----- .../com/slidingmenu/lib/CustomViewBehind.java | 50 +++- .../src/com/slidingmenu/lib/SlidingMenu.java | 142 +++++++--- .../lib/app/SlidingActivityHelper.java | 12 +- 8 files changed, 420 insertions(+), 124 deletions(-) create mode 100644 library/res/drawable/custom_up.xml create mode 100644 library/res/values/styles.xml create mode 100644 library/src/com/slidingmenu/lib/CustomUpIndicator.java diff --git a/library/res/drawable/custom_up.xml b/library/res/drawable/custom_up.xml new file mode 100644 index 000000000..189367d0a --- /dev/null +++ b/library/res/drawable/custom_up.xml @@ -0,0 +1,15 @@ + + + + + + + + + + \ No newline at end of file diff --git a/library/res/values/attrs.xml b/library/res/values/attrs.xml index 461440384..ee1fd7222 100644 --- a/library/res/values/attrs.xml +++ b/library/res/values/attrs.xml @@ -18,10 +18,16 @@ - + + + + + + + diff --git a/library/res/values/styles.xml b/library/res/values/styles.xml new file mode 100644 index 000000000..3df4c7504 --- /dev/null +++ b/library/res/values/styles.xml @@ -0,0 +1,8 @@ + + + + + + \ No newline at end of file diff --git a/library/src/com/slidingmenu/lib/CustomUpIndicator.java b/library/src/com/slidingmenu/lib/CustomUpIndicator.java new file mode 100644 index 000000000..4384cadb6 --- /dev/null +++ b/library/src/com/slidingmenu/lib/CustomUpIndicator.java @@ -0,0 +1,45 @@ +package com.slidingmenu.lib; + +import android.graphics.Canvas; +import android.graphics.ColorFilter; +import android.graphics.Paint; +import android.graphics.PixelFormat; +import android.graphics.RectF; +import android.graphics.drawable.Drawable; + +public class CustomUpIndicator extends Drawable { + + public static Paint mPaint; + private RectF mRect; + + @Override + public void draw(Canvas canvas) { + // TODO Auto-generated method stub + mPaint.setARGB(0, 255, 0, 0); + + mRect.left = 0.0f; + mRect.top = 0.0f; + mRect.right = 50.0f; + mRect.bottom = 50.0f; + + canvas.drawRect(mRect, mPaint); + } + + @Override + public int getOpacity() { + return PixelFormat.OPAQUE; + } + + @Override + public void setAlpha(int alpha) { + // TODO Auto-generated method stub + + } + + @Override + public void setColorFilter(ColorFilter cf) { + // TODO Auto-generated method stub + + } + +} diff --git a/library/src/com/slidingmenu/lib/CustomViewAbove.java b/library/src/com/slidingmenu/lib/CustomViewAbove.java index 1628d4717..34a25198c 100644 --- a/library/src/com/slidingmenu/lib/CustomViewAbove.java +++ b/library/src/com/slidingmenu/lib/CustomViewAbove.java @@ -1,7 +1,11 @@ package com.slidingmenu.lib; import android.content.Context; -import android.graphics.*; +import android.graphics.Bitmap; +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.Paint; +import android.graphics.Rect; import android.graphics.drawable.Drawable; import android.os.Build; import android.support.v4.view.KeyEventCompat; @@ -48,8 +52,9 @@ public float getInterpolation(float t) { } }; - private FrameLayout mMenu; - private FrameLayout mContent; + private FrameLayout mMenuLeft; + private FrameLayout mMenuRight; + protected FrameLayout mContent; private int mCurItem; private Scroller mScroller; @@ -91,7 +96,8 @@ public float getInterpolation(float t) { private boolean mLastTouchAllowed = false; private final int mSlidingMenuThreshold = 10; - private CustomViewBehind mCustomViewBehind; + private CustomViewBehind mViewBehindLeft; + private CustomViewBehind mViewBehindRight; private boolean mEnabled = true; private OnPageChangeListener mOnPageChangeListener; @@ -183,13 +189,13 @@ void initCustomViewAbove(boolean isAbove) { setInternalPageChangeListener(new SimpleOnPageChangeListener() { public void onPageSelected(int position) { - if (mCustomViewBehind != null) { + if (mViewBehindLeft != null) { switch (position) { case 0: - mCustomViewBehind.setChildrenEnabled(true); + mViewBehindLeft.setChildrenEnabled(true); break; case 1: - mCustomViewBehind.setChildrenEnabled(false); + mViewBehindLeft.setChildrenEnabled(false); break; } } @@ -200,10 +206,12 @@ public void onPageSelected(int position) { final float density = context.getResources().getDisplayMetrics().density; mFlingDistance = (int) (MIN_DISTANCE_FOR_FLING * density); - mMenu = new FrameLayout(getContext()); - super.addView(mMenu); + mMenuLeft = new FrameLayout(getContext()); + super.addView(mMenuLeft); mContent = new FrameLayout(getContext()); super.addView(mContent); + mMenuRight = new FrameLayout(getContext()); + super.addView(mMenuRight); if (isAbove) { View v = new LinearLayout(getContext()); @@ -243,18 +251,20 @@ void setCurrentItemInternal(int item, boolean smoothScroll, boolean always) { } void setCurrentItemInternal(int item, boolean smoothScroll, boolean always, int velocity) { - if (!always && mCurItem == item && mMenu != null && mContent != null) { + if (!always && mCurItem == item && mMenuLeft != null && mContent != null) { setScrollingCacheEnabled(false); return; } + // TODO count accordingly if (item < 0) { item = 0; - } else if (item >= 2) { - item = 1; + } else if (item >= getViewCount()) { + item = getViewCount() - 1; } + final boolean dispatchSelected = mCurItem != item; mCurItem = item; - final int destX = getChildLeft(mCurItem); + final int destX = this.getDestScrollX(mCurItem); if (dispatchSelected && mOnPageChangeListener != null) { mOnPageChangeListener.onPageSelected(item); } @@ -376,11 +386,31 @@ float distanceInfluenceForSnapDuration(float f) { return (float) FloatMath.sin(f); } - public int getDestScrollX() { - if (isMenuOpen()) { + public int getDestScrollX(int i) { + switch (i) { + case 0: + return 0; + case 1: return getBehindWidth(); + case 2: + return 2*getBehindWidth(); + } + throw new RuntimeException("This should never happen!"); + } + + public int getRightScrollBound() { + if (mViewBehindRight != null) { + return 2*getBehindWidth(); } else { + return getBehindWidth(); + } + } + + public int getLeftScrollBound() { + if (mViewBehindLeft != null) { return 0; + } else { + return getBehindWidth(); } } @@ -393,12 +423,31 @@ public int getChildRight(int i) { return getChildLeft(i) + getChildWidth(i); } - public boolean isMenuOpen() { + public int getContentRight() { + return mContent.getRight(); + } + + public boolean isRightOpen() { + if (mViewBehindRight == null) + return false; + + if (mViewBehindLeft == null) { + return getCurrentItem() == 1; + } else { + return getCurrentItem() == 2; + } + } + + public boolean isLeftOpen() { + if (mViewBehindLeft == null) { + return false; + } + return getCurrentItem() == 0; } public int getCustomWidth() { - int i = isMenuOpen()? 0 : 1; + int i = isLeftOpen()? 0 : 1; return getChildWidth(i); } @@ -411,10 +460,12 @@ public int getChildWidth(int i) { } public int getBehindWidth() { - if (mCustomViewBehind == null) { - return 0; + if (mViewBehindLeft != null) { + return mViewBehindLeft.getWidth(); + } else if (mViewBehindRight != null) { + return mViewBehindRight.getWidth(); } else { - return mCustomViewBehind.getWidth(); + return 0; } } @@ -455,7 +506,7 @@ void smoothScrollTo(int x, int y, int velocity) { int dy = y - sy; if (dx == 0 && dy == 0) { completeScroll(); - if (isMenuOpen()) { + if (isLeftOpen()) { if (mOpenedListener != null) mOpenedListener.onOpened(); } else { @@ -490,10 +541,10 @@ void smoothScrollTo(int x, int y, int velocity) { } protected void setMenu(View v) { - if (mMenu.getChildCount() > 0) { - mMenu.removeAllViews(); + if (mMenuLeft.getChildCount() > 0) { + mMenuLeft.removeAllViews(); } - mMenu.addView(v); + mMenuLeft.addView(v); } public void setContent(View v) { @@ -503,8 +554,19 @@ public void setContent(View v) { mContent.addView(v); } - public void setCustomViewBehind(CustomViewBehind cvb) { - mCustomViewBehind = cvb; + public void setViewBehindLeft(CustomViewBehind cvb) { + mViewBehindLeft = cvb; + } + + public void setViewBehindRight(CustomViewBehind cvb) { + mViewBehindRight = cvb; + } + + private int getViewCount() { + int count = 1; + count += (mViewBehindLeft != null) ? 1 : 0; + count += (mViewBehindRight != null) ? 1 : 0; + return count; } @Override @@ -519,7 +581,8 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { mContent.measure(contentWidth, contentHeight); final int menuWidth = getChildMeasureSpec(widthMeasureSpec, 0, getBehindWidth()); - mMenu.measure(menuWidth, contentHeight); + mMenuLeft.measure(menuWidth, contentHeight); + mMenuRight.measure(menuWidth, contentHeight); } @Override @@ -541,10 +604,9 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) { final int height = b - t; int contentLeft = getChildLeft(1); - float percentOpen = getPercentOpen(); - int offset = (int) (mScrollX * (1 - mScrollScale)); - mMenu.layout(0, 0, width, height); + mMenuLeft.layout(0, 0, width, height); mContent.layout(contentLeft, 0, contentLeft + width, height); + mMenuRight.layout(contentLeft + width, 0, contentLeft + 2*width, height); } @@ -616,7 +678,7 @@ private void completeScroll() { if (oldX != x || oldY != y) { scrollTo(x, y); } - if (isMenuOpen()) { + if (isLeftOpen()) { if (mOpenedListener != null) mOpenedListener.onOpened(); } else { @@ -647,12 +709,22 @@ protected int getTouchModeBehind() { } private boolean thisTouchAllowed(MotionEvent ev) { - if (isMenuOpen()) { + int x = (int) (ev.getX() + mScrollX); + if (isLeftOpen()) { + switch (mTouchModeBehind) { + case SlidingMenu.TOUCHMODE_FULLSCREEN: + return true; + case SlidingMenu.TOUCHMODE_MARGIN: + return x >= getBehindWidth() && x <= getWidth(); + default: + return false; + } + } else if (isRightOpen()) { switch (mTouchModeBehind) { case SlidingMenu.TOUCHMODE_FULLSCREEN: return true; case SlidingMenu.TOUCHMODE_MARGIN: - return ev.getX() >= getBehindWidth() && ev.getX() <= getWidth(); + return x <= getContentRight(); default: return false; } @@ -663,7 +735,8 @@ private boolean thisTouchAllowed(MotionEvent ev) { case SlidingMenu.TOUCHMODE_MARGIN: int pixels = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, mSlidingMenuThreshold, getResources().getDisplayMetrics()); - return ev.getX() >= 0 && ev.getX() <= pixels; + int left = mContent.getLeft(); + return x >= left && x <= pixels + left; default: return false; } @@ -683,7 +756,7 @@ public boolean onInterceptTouchEvent(MotionEvent ev) { if (action == MotionEvent.ACTION_DOWN) Log.v(TAG, "Received ACTION_DOWN"); - + if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) { mIsBeingDragged = false; mIsUnableToDrag = false; @@ -733,8 +806,11 @@ else if (mIsUnableToDrag) if (thisTouchAllowed(ev)) { mIsBeingDragged = false; mIsUnableToDrag = false; - if (isMenuOpen() && mInitialMotionX > getBehindWidth()) + if (isLeftOpen() && mInitialMotionX > getBehindWidth()) return true; + if (isRightOpen() && (mInitialMotionX + mScrollX) < getContentRight()) { + return true; + } } else { mIsUnableToDrag = true; } @@ -816,8 +892,8 @@ public boolean onTouchEvent(MotionEvent ev) { mLastMotionX = x; float oldScrollX = getScrollX(); float scrollX = oldScrollX + deltaX; - final float leftBound = 0; - final float rightBound = getBehindWidth(); + final float leftBound = getLeftScrollBound(); + final float rightBound = getRightScrollBound(); if (scrollX < leftBound) { scrollX = leftBound; } else if (scrollX > rightBound) { @@ -849,9 +925,11 @@ public boolean onTouchEvent(MotionEvent ev) { mActivePointerId = INVALID_POINTER; endDrag(); - } else if (isMenuOpen()) { + } else if (isLeftOpen()) { // close the menu setCurrentItem(1); + } else if (isRightOpen()) { + setCurrentItem(1); } break; case MotionEvent.ACTION_CANCEL: @@ -887,7 +965,7 @@ public float getScrollScale() { public void setScrollScale(float f) { if (f < 0 && f > 1) - throw new IllegalStateException("ScrollScale must be between 0 and 1"); + throw new IllegalArgumentException("ScrollScale must be between 0 and 1"); mScrollScale = f; } @@ -895,9 +973,35 @@ public void setScrollScale(float f) { public void scrollTo(int x, int y) { super.scrollTo(x, y); mScrollX = x; - if (mCustomViewBehind != null && mEnabled) { - mCustomViewBehind.scrollTo((int)(x*mScrollScale), y); + // adjust visibility so we don't have any layout issues + if (mViewBehindLeft != null) { + if (x < getBehindWidth()) { + if (mViewBehindLeft.getVisibility() != View.VISIBLE) { + mViewBehindLeft.setVisibility(View.VISIBLE); + } + } else if (mViewBehindLeft.getVisibility() != View.INVISIBLE){ + mViewBehindLeft.setVisibility(View.INVISIBLE); + } + } + if (mViewBehindRight != null) { + if (x > mContent.getLeft()) { + if (mViewBehindRight.getVisibility() != View.VISIBLE) { + mViewBehindRight.setVisibility(View.VISIBLE); + } + } else if (mViewBehindRight.getVisibility() != View.INVISIBLE){ + mViewBehindRight.setVisibility(View.INVISIBLE); + } + } + // scroll the behind views + if (mViewBehindLeft != null && mEnabled) { + mViewBehindLeft.scrollTo((int)(x*mScrollScale), y); } + if (mViewBehindRight != null && mEnabled) { + int x2 = (int) ((x - 2*getBehindWidth()) * 1); + Log.v(TAG, "scroll x" + x2); + mViewBehindRight.scrollTo(x2, y); + } + if (mShadowDrawable != null || mSelectorDrawable != null) invalidate(); } @@ -909,11 +1013,16 @@ private int determineTargetPage(int currentPage, float pageOffset, int velocity, } else { targetPage = (int) (currentPage + pageOffset + 0.5f); } + Log.v(TAG, "targetPage : " + targetPage); return targetPage; } - protected float getPercentOpen() { - return (getBehindWidth() - mScrollX) / getBehindWidth(); + protected float getPercentOpenLeft() { + return (getBehindWidth() - mScrollX) / getBehindWidth(); + } + + protected float getPercentOpenRight() { + return (getWidth() - (getContentRight() - mScrollX)) / getBehindWidth(); } @Override @@ -925,34 +1034,36 @@ protected void dispatchDraw(Canvas canvas) { final int left = behindWidth - mShadowWidth; mShadowDrawable.setBounds(left, 0, left + mShadowWidth, getHeight()); mShadowDrawable.draw(canvas); + mShadowDrawable.setBounds(getContentRight(), 0, getContentRight() + mShadowWidth, getHeight()); + mShadowDrawable.draw(canvas); } if (mFadeEnabled) - onDrawBehindFade(canvas, getPercentOpen()); + onDrawBehindFade(canvas); if (mSelectorEnabled) - onDrawMenuSelector(canvas, getPercentOpen()); + onDrawMenuSelector(canvas); } - /** - * Pads our content window so that it fits within the system windows. - * @param insets The insets by which we need to offset our view. - * @return True since we handled the padding change. - */ - @Override - protected boolean fitSystemWindows(Rect insets) { - - if (mContent != null) { - int leftPadding = mContent.getPaddingLeft() + insets.left; - int rightPadding = mContent.getPaddingRight() + insets.right; - int topPadding = mContent.getPaddingTop() + insets.top; - int bottomPadding = mContent.getPaddingBottom() + insets.bottom; - mContent.setPadding(leftPadding, topPadding, rightPadding, bottomPadding); - return true; - } + /** + * Pads our content window so that it fits within the system windows. + * @param insets The insets by which we need to offset our view. + * @return True since we handled the padding change. + */ + @Override + protected boolean fitSystemWindows(Rect insets) { + + if (mContent != null) { + int leftPadding = mContent.getPaddingLeft() + insets.left; + int rightPadding = mContent.getPaddingRight() + insets.right; + int topPadding = mContent.getPaddingTop() + insets.top; + int bottomPadding = mContent.getPaddingBottom() + insets.bottom; + mContent.setPadding(leftPadding, topPadding, rightPadding, bottomPadding); + return true; + } - return super.fitSystemWindows(insets); - } + return super.fitSystemWindows(insets); + } @Override protected void onDraw(Canvas canvas) { @@ -971,20 +1082,31 @@ protected void onDraw(Canvas canvas) { private View mSelectedView; - private void onDrawBehindFade(Canvas canvas, float openPercent) { - final int alpha = (int) (mFadeDegree * 255 * Math.abs(1-openPercent)); - if (alpha > 0) { - mBehindFadePaint.setColor(Color.argb(alpha, 0, 0, 0)); - canvas.drawRect(0, 0, getBehindWidth(), getHeight(), mBehindFadePaint); + private void onDrawBehindFade(Canvas canvas) { + int alpha; + if (mViewBehindLeft != null) { + alpha = (int) (mFadeDegree * 255 * Math.abs(1-getPercentOpenLeft())); + if (alpha > 0) { + mBehindFadePaint.setColor(Color.argb(alpha, 0, 0, 0)); + canvas.drawRect(0, 0, getBehindWidth(), getHeight(), mBehindFadePaint); + } + } + if (mViewBehindRight != null) { + alpha = (int) (mFadeDegree * 255 * Math.abs(1-getPercentOpenRight())); + if (alpha > 0) { + mBehindFadePaint.setColor(Color.argb(alpha, 0, 0, 0)); + canvas.drawRect(getContentRight(), 0, getContentRight() + getBehindWidth(), + getHeight(), mBehindFadePaint); + } } } - private void onDrawMenuSelector(Canvas canvas, float openPercent) { + private void onDrawMenuSelector(Canvas canvas) { if (mSelectorDrawable != null && mSelectedView != null) { String tag = (String) mSelectedView.getTag(R.id.selected_view); if (tag.equals(TAG+"SelectedView")) { int right = getChildLeft(1); - int left = (int) (right - mSelectorDrawable.getWidth() * openPercent); + int left = (int) (right - mSelectorDrawable.getWidth() * getPercentOpenLeft()); canvas.save(); canvas.clipRect(left, 0, right, getHeight()); diff --git a/library/src/com/slidingmenu/lib/CustomViewBehind.java b/library/src/com/slidingmenu/lib/CustomViewBehind.java index 7fe8d3829..163024eb1 100644 --- a/library/src/com/slidingmenu/lib/CustomViewBehind.java +++ b/library/src/com/slidingmenu/lib/CustomViewBehind.java @@ -8,23 +8,26 @@ import android.view.View; import android.view.ViewGroup; import android.view.animation.Transformation; +import android.widget.RelativeLayout; import com.slidingmenu.lib.SlidingMenu.CanvasTransformer; public class CustomViewBehind extends CustomViewAbove { private static final String TAG = "CustomViewBehind"; + public static final int LEFT = 0; + public static final int RIGHT = 1; private CustomViewAbove mViewAbove; + private int mMode; private CanvasTransformer mTransformer; private boolean mChildrenEnabled; - public CustomViewBehind(Context context) { - this(context, null); - } - - public CustomViewBehind(Context context, AttributeSet attrs) { - super(context, attrs, false); + public CustomViewBehind(Context context, int mode) { + super(context); + if (mode != LEFT && mode != RIGHT) + throw new IllegalStateException("mode must be LEFT or RIGHT"); + mMode = mode; } public void setCustomViewAbove(CustomViewAbove customViewAbove) { @@ -45,10 +48,27 @@ public void setCanvasTransformer(CanvasTransformer t) { public int getChildLeft(int i) { return 0; } + + @Override + protected void onLayout(boolean changed, int l, int t, int r, int b) { + final int width = r - l; + final int height = b - t; + + int left; + switch (mMode) { + case LEFT: + left = 0; + break; + default: + left = ((RelativeLayout.LayoutParams)getLayoutParams()).leftMargin; + break; + } + mContent.layout(0, 0, 0 + width, height); + } @Override public int getCustomWidth() { - int i = isMenuOpen()? 0 : 1; + int i = isLeftOpen()? 0 : 1; return getChildWidth(i); } @@ -66,11 +86,6 @@ public int getBehindWidth() { return params.width; } - @Override - public void setContent(View v) { - super.setMenu(v); - } - public void setChildrenEnabled(boolean enabled) { mChildrenEnabled = enabled; } @@ -96,7 +111,16 @@ public boolean onTouchEvent(MotionEvent e) { protected void dispatchDraw(Canvas canvas) { if (mTransformer != null) { canvas.save(); - mTransformer.transformCanvas(canvas, mViewAbove.getPercentOpen()); + float percent = 0.0f; + switch (mMode) { + case LEFT: + percent = mViewAbove.getPercentOpenLeft(); + break; + case RIGHT: + percent = mViewAbove.getPercentOpenRight(); + break; + } + mTransformer.transformCanvas(canvas, percent); super.dispatchDraw(canvas); canvas.restore(); } else diff --git a/library/src/com/slidingmenu/lib/SlidingMenu.java b/library/src/com/slidingmenu/lib/SlidingMenu.java index 3564719ec..e18311693 100644 --- a/library/src/com/slidingmenu/lib/SlidingMenu.java +++ b/library/src/com/slidingmenu/lib/SlidingMenu.java @@ -15,6 +15,7 @@ import android.support.v4.os.ParcelableCompat; import android.support.v4.os.ParcelableCompatCreatorCallbacks; import android.util.AttributeSet; +import android.util.Log; import android.view.Display; import android.view.LayoutInflater; import android.view.View; @@ -27,15 +28,22 @@ public class SlidingMenu extends RelativeLayout { + public static final String TAG = "SlidingMenu"; + public static final int TOUCHMODE_MARGIN = 0; public static final int TOUCHMODE_FULLSCREEN = 1; + public static final int SIDES_LEFT = 0; + public static final int SIDES_RIGHT = 1; + public static final int SIDES_LEFT_RIGHT = 2; + private CustomViewAbove mViewAbove; - private CustomViewBehind mViewBehind; + private CustomViewBehind mViewBehindLeft; + private CustomViewBehind mViewBehindRight; private OnOpenListener mOpenListener; private OnCloseListener mCloseListener; - private boolean mSlidingEnabled; + private boolean mSlidingEnabled; public static void attachSlidingMenu(Activity activity, SlidingMenu sm, boolean slidingTitle) { @@ -95,16 +103,10 @@ public SlidingMenu(Context context, AttributeSet attrs) { public SlidingMenu(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); - - LayoutParams behindParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); - mViewBehind = new CustomViewBehind(context); - addView(mViewBehind, behindParams); + // above view LayoutParams aboveParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); mViewAbove = new CustomViewAbove(context); addView(mViewAbove, aboveParams); - // register the CustomViewBehind2 with the CustomViewAbove - mViewAbove.setCustomViewBehind(mViewBehind); - mViewBehind.setCustomViewAbove(mViewAbove); mViewAbove.setOnPageChangeListener(new OnPageChangeListener() { public static final int POSITION_OPEN = 0; public static final int POSITION_CLOSE = 1; @@ -123,13 +125,32 @@ public void onPageSelected(int position) { // now style everything! TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.SlidingMenu); + int slidingSides = ta.getInt(R.styleable.SlidingMenu_slidingSides, SIDES_LEFT); + switch (slidingSides) { + case SIDES_LEFT: + initializeLeft(); + break; + case SIDES_RIGHT: + initializeRight(); + break; + case SIDES_LEFT_RIGHT: + initializeLeft(); + initializeRight(); + break; + } // set the above and behind views if defined in xml int viewAbove = ta.getResourceId(R.styleable.SlidingMenu_viewAbove, -1); - if (viewAbove != -1) + if (viewAbove != -1) { setContent(viewAbove); - int viewBehind = ta.getResourceId(R.styleable.SlidingMenu_viewBehind, -1); - if (viewBehind != -1) - setMenu(viewBehind); + } + int viewBehindLeft = ta.getResourceId(R.styleable.SlidingMenu_viewBehindLeft, -1); + if (viewBehindLeft != -1) { + setViewBehindLeft(viewBehindLeft); + } + int viewBehindRight = ta.getResourceId(R.styleable.SlidingMenu_viewBehindRight, -1); + if (viewBehindRight != -1) { + setViewBehindRight(viewBehindRight); + } int touchModeAbove = ta.getInt(R.styleable.SlidingMenu_aboveTouchMode, TOUCHMODE_MARGIN); setTouchModeAbove(touchModeAbove); int touchModeBehind = ta.getInt(R.styleable.SlidingMenu_behindTouchMode, TOUCHMODE_MARGIN); @@ -164,6 +185,26 @@ else if (widthBehind != -1) setSelectorDrawable(selectorRes); } + private void initializeLeft() { + if (mViewBehindLeft == null) { + LayoutParams behindParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); + mViewBehindLeft = new CustomViewBehind(getContext(), CustomViewBehind.LEFT); + addView(mViewBehindLeft, 0, behindParams); + mViewAbove.setViewBehindLeft(mViewBehindLeft); + mViewBehindLeft.setCustomViewAbove(mViewAbove); + } + } + + private void initializeRight() { + if (mViewBehindRight == null) { + LayoutParams behindParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); + mViewBehindRight = new CustomViewBehind(getContext(), CustomViewBehind.RIGHT); + addView(mViewBehindRight, 0, behindParams); + mViewAbove.setViewBehindRight(mViewBehindRight); + mViewBehindRight.setCustomViewAbove(mViewAbove); + } + } + public void setContent(int res) { setContent(LayoutInflater.from(getContext()).inflate(res, null)); } @@ -174,13 +215,24 @@ public void setContent(View v) { showAbove(); } - public void setMenu(int res) { - setMenu(LayoutInflater.from(getContext()).inflate(res, null)); + public void setViewBehindRight(int res) { + setViewBehindRight(LayoutInflater.from(getContext()).inflate(res, null)); } - public void setMenu(View v) { - mViewBehind.setMenu(v); - mViewBehind.invalidate(); + public void setViewBehindRight(View v) { + initializeRight(); + mViewBehindRight.setContent(v); + mViewBehindRight.invalidate(); + } + + public void setViewBehindLeft(int res) { + setViewBehindLeft(LayoutInflater.from(getContext()).inflate(res, null)); + } + + public void setViewBehindLeft(View v) { + initializeLeft(); + mViewBehindLeft.setContent(v); + mViewBehindLeft.invalidate(); } public void setSlidingEnabled(boolean b) { @@ -199,13 +251,13 @@ public boolean isSlidingEnabled() { public void setStatic(boolean b) { if (b) { setSlidingEnabled(false); - mViewAbove.setCustomViewBehind(null); + mViewAbove.setViewBehindLeft(null); mViewAbove.setCurrentItem(1); - mViewBehind.setCurrentItem(0); + mViewBehindLeft.setCurrentItem(0); } else { mViewAbove.setCurrentItem(1); - mViewBehind.setCurrentItem(1); - mViewAbove.setCustomViewBehind(mViewBehind); + mViewBehindLeft.setCurrentItem(1); + mViewAbove.setViewBehindLeft(mViewBehindLeft); setSlidingEnabled(true); } } @@ -229,7 +281,7 @@ public void showAbove() { * @return Whether or not the behind view is showing */ public boolean isBehindShowing() { - return mViewAbove.getCurrentItem() == 0; + return mViewAbove.getCurrentItem() == 0 || mViewAbove.getCurrentItem() == 2; } /** @@ -237,7 +289,7 @@ public boolean isBehindShowing() { * @return The margin on the right of the screen that the behind view scrolls to */ public int getBehindOffset() { - return ((RelativeLayout.LayoutParams)mViewBehind.getLayoutParams()).rightMargin; + return ((RelativeLayout.LayoutParams)mViewBehindLeft.getLayoutParams()).rightMargin; } /** @@ -245,11 +297,25 @@ public int getBehindOffset() { * @param i The margin on the right of the screen that the behind view scrolls to */ public void setBehindOffset(int i) { - RelativeLayout.LayoutParams params = ((RelativeLayout.LayoutParams)mViewBehind.getLayoutParams()); - int bottom = params.bottomMargin; - int top = params.topMargin; - int left = params.leftMargin; - params.setMargins(left, top, i, bottom); + // behind left + if (mViewBehindLeft != null) { + RelativeLayout.LayoutParams params = ((RelativeLayout.LayoutParams)mViewBehindLeft.getLayoutParams()); + int bottom = params.bottomMargin; + int top = params.topMargin; + int left = params.leftMargin; + params.setMargins(left, top, i, bottom); + } + // behind right + if (mViewBehindRight != null) { + RelativeLayout.LayoutParams params = ((RelativeLayout.LayoutParams)mViewBehindRight.getLayoutParams()); + int bottom = params.bottomMargin; + int top = params.topMargin; + int right = params.rightMargin; + Log.v(TAG, "left margin" + i); + params.setMargins(i, top, right, bottom); + } + requestLayout(); + showAbove(); } @SuppressWarnings("deprecation") @@ -297,7 +363,7 @@ public void setBehindScrollScale(float f) { } public void setBehindCanvasTransformer(CanvasTransformer t) { - mViewBehind.setCanvasTransformer(t); + mViewBehindLeft.setCanvasTransformer(t); } public int getTouchModeAbove() { @@ -313,7 +379,8 @@ public void setTouchModeAbove(int i) { } public int getTouchModeBehind() { - return mViewBehind.getTouchMode(); + return mViewAbove.getTouchModeBehind(); + // return mViewBehindLeft.getTouchMode(); } public void setTouchModeBehind(int i) { @@ -321,7 +388,8 @@ public void setTouchModeBehind(int i) { throw new IllegalStateException("TouchMode must be set to either" + "TOUCHMODE_FULLSCREEN or TOUCHMODE_MARGIN."); } - mViewBehind.setTouchMode(i); + mViewAbove.setTouchModeBehind(i); + // mViewBehindLeft.setTouchMode(i); } public void setShadowDrawable(int resId) { @@ -438,11 +506,11 @@ protected void onRestoreInstanceState(Parcelable state) { @Override protected boolean fitSystemWindows(Rect insets) { - int leftPadding = getPaddingLeft() + insets.left; - int rightPadding = getPaddingRight() + insets.right; - int topPadding = getPaddingTop() + insets.top; - int bottomPadding = getPaddingBottom() + insets.bottom; - this.setPadding(leftPadding, topPadding, rightPadding, bottomPadding); + int leftPadding = getPaddingLeft() + insets.left; + int rightPadding = getPaddingRight() + insets.right; + int topPadding = getPaddingTop() + insets.top; + int bottomPadding = getPaddingBottom() + insets.bottom; + this.setPadding(leftPadding, topPadding, rightPadding, bottomPadding); return super.fitSystemWindows(insets); } diff --git a/library/src/com/slidingmenu/lib/app/SlidingActivityHelper.java b/library/src/com/slidingmenu/lib/app/SlidingActivityHelper.java index 85ba50fd7..28b13ffab 100644 --- a/library/src/com/slidingmenu/lib/app/SlidingActivityHelper.java +++ b/library/src/com/slidingmenu/lib/app/SlidingActivityHelper.java @@ -24,6 +24,7 @@ public class SlidingActivityHelper { private boolean mOnPostCreateCalled = false; private boolean mEnableSlide = true; + private boolean mUseCustomUp = false; public SlidingActivityHelper(Activity activity) { mActivity = activity; @@ -31,6 +32,7 @@ public SlidingActivityHelper(Activity activity) { public void onCreate(Bundle savedInstanceState) { mSlidingMenu = (SlidingMenu) LayoutInflater.from(mActivity).inflate(R.layout.slidingmenumain, null); +// mActivity.getTheme().applyStyle(R.style.CustomUp, true); } public void onPostCreate(Bundle savedInstanceState) { @@ -40,7 +42,7 @@ public void onPostCreate(Bundle savedInstanceState) { } mOnPostCreateCalled = true; - + // get the window background TypedArray a = mActivity.getTheme().obtainStyledAttributes(new int[] {android.R.attr.windowBackground}); int background = a.getResourceId(0, 0); @@ -75,6 +77,12 @@ public void setSlidingActionBarEnabled(boolean b) { mEnableSlide = b; } + public void useCustomUpIndicator() { + if (mOnPostCreateCalled) + throw new IllegalStateException("useCustomUpIndicator must be called in onCreate."); + mUseCustomUp = true; + } + public View findViewById(int id) { View v; if (mSlidingMenu != null) { @@ -97,7 +105,7 @@ public void setContentView(View v) { public void setBehindContentView(View v, LayoutParams params) { mViewBehind = v; - mSlidingMenu.setMenu(mViewBehind); + mSlidingMenu.setViewBehindLeft(mViewBehind); } public SlidingMenu getSlidingMenu() { From ebbce86e66c3f8a856a6a7e7541f0b41047e8cdc Mon Sep 17 00:00:00 2001 From: jfeinstein10 Date: Sun, 16 Sep 2012 01:44:24 -0400 Subject: [PATCH 02/17] Parallax scrolling now working --- library/src/com/slidingmenu/lib/CustomViewAbove.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/src/com/slidingmenu/lib/CustomViewAbove.java b/library/src/com/slidingmenu/lib/CustomViewAbove.java index 34a25198c..38c87c54e 100644 --- a/library/src/com/slidingmenu/lib/CustomViewAbove.java +++ b/library/src/com/slidingmenu/lib/CustomViewAbove.java @@ -997,7 +997,7 @@ public void scrollTo(int x, int y) { mViewBehindLeft.scrollTo((int)(x*mScrollScale), y); } if (mViewBehindRight != null && mEnabled) { - int x2 = (int) ((x - 2*getBehindWidth()) * 1); + int x2 = (int) ((x - 2*getBehindWidth()) * mScrollScale); Log.v(TAG, "scroll x" + x2); mViewBehindRight.scrollTo(x2, y); } From 0d8929dfae94af226b9d2aea5f715508a74f2430 Mon Sep 17 00:00:00 2001 From: jfeinstein10 Date: Sun, 16 Sep 2012 13:15:53 -0400 Subject: [PATCH 03/17] Touch modes working --- .../com/slidingmenu/lib/CustomViewAbove.java | 54 ++++++++++++++----- .../com/slidingmenu/lib/CustomViewBehind.java | 10 ---- 2 files changed, 42 insertions(+), 22 deletions(-) diff --git a/library/src/com/slidingmenu/lib/CustomViewAbove.java b/library/src/com/slidingmenu/lib/CustomViewAbove.java index 38c87c54e..ba70925ad 100644 --- a/library/src/com/slidingmenu/lib/CustomViewAbove.java +++ b/library/src/com/slidingmenu/lib/CustomViewAbove.java @@ -6,6 +6,7 @@ import android.graphics.Color; import android.graphics.Paint; import android.graphics.Rect; +import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.os.Build; import android.support.v4.view.KeyEventCompat; @@ -452,11 +453,36 @@ public int getCustomWidth() { } public int getChildWidth(int i) { - if (i <= 0) { - return getBehindWidth(); - } else { - return getChildAt(i).getWidth(); + if (mViewBehindLeft != null && mViewBehindRight != null) { + switch (i) { + case 0: + return getBehindWidth(); + case 1: + return mContent.getWidth(); + case 2: + return getBehindWidth(); + } + } else if (mViewBehindLeft != null) { + switch (i) { + case 0: + return getBehindWidth(); + case 1: + return mContent.getWidth(); + } + } else if (mViewBehindRight != null) { + switch (i) { + case 0: + return mContent.getWidth(); + case 1: + return getBehindWidth(); + } } + return 0; +// if (i <= 0) { +// return getBehindWidth(); +// } else { +// return getChildAt(i).getWidth(); +// } } public int getBehindWidth() { @@ -736,7 +762,13 @@ private boolean thisTouchAllowed(MotionEvent ev) { int pixels = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, mSlidingMenuThreshold, getResources().getDisplayMetrics()); int left = mContent.getLeft(); - return x >= left && x <= pixels + left; + int right = mContent.getRight(); + boolean bool = false; + if (mViewBehindLeft != null) + bool |= (x >= left && x <= pixels + left); + if (mViewBehindRight != null) + bool |= (x <= right && x >= right - pixels); + return bool; default: return false; } @@ -806,11 +838,8 @@ else if (mIsUnableToDrag) if (thisTouchAllowed(ev)) { mIsBeingDragged = false; mIsUnableToDrag = false; - if (isLeftOpen() && mInitialMotionX > getBehindWidth()) + if ((isLeftOpen() || isRightOpen()) && mTouchModeBehind == SlidingMenu.TOUCHMODE_MARGIN) return true; - if (isRightOpen() && (mInitialMotionX + mScrollX) < getContentRight()) { - return true; - } } else { mIsUnableToDrag = true; } @@ -911,7 +940,7 @@ public boolean onTouchEvent(MotionEvent ev) { velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity); int initialVelocity = (int) VelocityTrackerCompat.getXVelocity( velocityTracker, mActivePointerId); - final int widthWithMargin = getChildWidth(mCurItem) + mShadowWidth; + final int widthWithMargin = getChildWidth(mCurItem); final int scrollX = getScrollX(); final int currentPage = scrollX / widthWithMargin; final float pageOffset = (float) (scrollX % widthWithMargin) / widthWithMargin; @@ -1007,8 +1036,9 @@ public void scrollTo(int x, int y) { } private int determineTargetPage(int currentPage, float pageOffset, int velocity, int deltaX) { + float scale = getBehindWidth()/getWidth(); int targetPage; - if (Math.abs(deltaX) > mFlingDistance && Math.abs(velocity) > mMinimumVelocity) { + if (Math.abs(deltaX) > (float)mFlingDistance*scale && Math.abs(velocity) > mMinimumVelocity) { targetPage = velocity > 0 ? currentPage : currentPage + 1; } else { targetPage = (int) (currentPage + pageOffset + 0.5f); @@ -1029,7 +1059,7 @@ protected float getPercentOpenRight() { protected void dispatchDraw(Canvas canvas) { super.dispatchDraw(canvas); final int behindWidth = getBehindWidth(); - // Draw the margin drawable if needed. + // Draw the margin drawable if needed if (mShadowWidth > 0 && mShadowDrawable != null) { final int left = behindWidth - mShadowWidth; mShadowDrawable.setBounds(left, 0, left + mShadowWidth, getHeight()); diff --git a/library/src/com/slidingmenu/lib/CustomViewBehind.java b/library/src/com/slidingmenu/lib/CustomViewBehind.java index 163024eb1..2377bd520 100644 --- a/library/src/com/slidingmenu/lib/CustomViewBehind.java +++ b/library/src/com/slidingmenu/lib/CustomViewBehind.java @@ -53,16 +53,6 @@ public int getChildLeft(int i) { protected void onLayout(boolean changed, int l, int t, int r, int b) { final int width = r - l; final int height = b - t; - - int left; - switch (mMode) { - case LEFT: - left = 0; - break; - default: - left = ((RelativeLayout.LayoutParams)getLayoutParams()).leftMargin; - break; - } mContent.layout(0, 0, 0 + width, height); } From 8874ab628d0e17fda54aef035e8fd4afb1ad6d0b Mon Sep 17 00:00:00 2001 From: jfeinstein10 Date: Sun, 16 Sep 2012 15:38:46 -0400 Subject: [PATCH 04/17] More position fixes --- library/src/com/slidingmenu/lib/CustomViewAbove.java | 11 ++++++++++- library/src/com/slidingmenu/lib/SlidingMenu.java | 8 +++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/library/src/com/slidingmenu/lib/CustomViewAbove.java b/library/src/com/slidingmenu/lib/CustomViewAbove.java index ba70925ad..2b44f6bec 100644 --- a/library/src/com/slidingmenu/lib/CustomViewAbove.java +++ b/library/src/com/slidingmenu/lib/CustomViewAbove.java @@ -200,6 +200,16 @@ public void onPageSelected(int position) { break; } } + if (mViewBehindRight != null) { + switch (position) { + case 1: + mViewBehindRight.setChildrenEnabled(true); + break; + case 2: + mViewBehindRight.setChildrenEnabled(false); + break; + } + } } }); @@ -1027,7 +1037,6 @@ public void scrollTo(int x, int y) { } if (mViewBehindRight != null && mEnabled) { int x2 = (int) ((x - 2*getBehindWidth()) * mScrollScale); - Log.v(TAG, "scroll x" + x2); mViewBehindRight.scrollTo(x2, y); } diff --git a/library/src/com/slidingmenu/lib/SlidingMenu.java b/library/src/com/slidingmenu/lib/SlidingMenu.java index e18311693..0d77aa224 100644 --- a/library/src/com/slidingmenu/lib/SlidingMenu.java +++ b/library/src/com/slidingmenu/lib/SlidingMenu.java @@ -48,7 +48,7 @@ public class SlidingMenu extends RelativeLayout { public static void attachSlidingMenu(Activity activity, SlidingMenu sm, boolean slidingTitle) { if (sm.getParent() != null) - throw new IllegalStateException("SlidingMenu cannot be attached to another view when" + + throw new IllegalArgumentException("SlidingMenu cannot be attached to another view when" + " calling the static method attachSlidingMenu"); if (slidingTitle) { @@ -372,7 +372,7 @@ public int getTouchModeAbove() { public void setTouchModeAbove(int i) { if (i != TOUCHMODE_FULLSCREEN && i != TOUCHMODE_MARGIN) { - throw new IllegalStateException("TouchMode must be set to either" + + throw new IllegalArgumentException("TouchMode must be set to either" + "TOUCHMODE_FULLSCREEN or TOUCHMODE_MARGIN."); } mViewAbove.setTouchMode(i); @@ -380,16 +380,14 @@ public void setTouchModeAbove(int i) { public int getTouchModeBehind() { return mViewAbove.getTouchModeBehind(); - // return mViewBehindLeft.getTouchMode(); } public void setTouchModeBehind(int i) { if (i != TOUCHMODE_FULLSCREEN && i != TOUCHMODE_MARGIN) { - throw new IllegalStateException("TouchMode must be set to either" + + throw new IllegalArgumentException("TouchMode must be set to either " + "TOUCHMODE_FULLSCREEN or TOUCHMODE_MARGIN."); } mViewAbove.setTouchModeBehind(i); - // mViewBehindLeft.setTouchMode(i); } public void setShadowDrawable(int resId) { From 0220a95b21fb74ad439700179cd1d37630b68b2e Mon Sep 17 00:00:00 2001 From: jfeinstein10 Date: Wed, 19 Sep 2012 11:43:14 -0400 Subject: [PATCH 05/17] More work on left and right sliding. Not quite there yet. Changed most behind methods so that they require LEFT, RIGHT, or BOTH --- library/AndroidManifest.xml | 16 +- library/res/values/attrs.xml | 5 - .../com/slidingmenu/lib/CustomViewAbove.java | 59 +++-- .../src/com/slidingmenu/lib/SlidingMenu.java | 217 ++++++++++-------- .../slidingmenu/lib/app/SlidingActivity.java | 34 ++- .../lib/app/SlidingActivityBase.java | 8 +- .../lib/app/SlidingActivityHelper.java | 30 ++- .../lib/app/SlidingFragmentActivity.java | 34 ++- .../lib/app/SlidingListActivity.java | 34 ++- .../lib/app/SlidingMapActivity.java | 34 ++- 10 files changed, 292 insertions(+), 179 deletions(-) diff --git a/library/AndroidManifest.xml b/library/AndroidManifest.xml index 86b1a3a77..6e6ac9404 100644 --- a/library/AndroidManifest.xml +++ b/library/AndroidManifest.xml @@ -11,10 +11,18 @@ - - - - + + + + \ No newline at end of file diff --git a/library/res/values/attrs.xml b/library/res/values/attrs.xml index ee1fd7222..f349fb02b 100644 --- a/library/res/values/attrs.xml +++ b/library/res/values/attrs.xml @@ -23,11 +23,6 @@ - - - - - diff --git a/library/src/com/slidingmenu/lib/CustomViewAbove.java b/library/src/com/slidingmenu/lib/CustomViewAbove.java index 2b44f6bec..9e1465549 100644 --- a/library/src/com/slidingmenu/lib/CustomViewAbove.java +++ b/library/src/com/slidingmenu/lib/CustomViewAbove.java @@ -442,11 +442,7 @@ public boolean isRightOpen() { if (mViewBehindRight == null) return false; - if (mViewBehindLeft == null) { - return getCurrentItem() == 1; - } else { - return getCurrentItem() == 2; - } + return getCurrentItem() == 2; } public boolean isLeftOpen() { @@ -458,7 +454,9 @@ public boolean isLeftOpen() { } public int getCustomWidth() { - int i = isLeftOpen()? 0 : 1; + int i = 1; + i = isLeftOpen()? 0 : i; + i = isRightOpen()? 2 : i; return getChildWidth(i); } @@ -488,17 +486,18 @@ public int getChildWidth(int i) { } } return 0; -// if (i <= 0) { -// return getBehindWidth(); -// } else { -// return getChildAt(i).getWidth(); -// } + // if (i <= 0) { + // return getBehindWidth(); + // } else { + // return getChildAt(i).getWidth(); + // } } public int getBehindWidth() { - if (mViewBehindLeft != null) { - return mViewBehindLeft.getWidth(); - } else if (mViewBehindRight != null) { + // if (mViewBehindLeft != null) { + // return mViewBehindLeft.getWidth(); + // } else + if (mViewBehindRight != null) { return mViewBehindRight.getWidth(); } else { return 0; @@ -587,7 +586,8 @@ public void setContent(View v) { if (mContent.getChildCount() > 0) { mContent.removeAllViews(); } - mContent.addView(v); + if (v != null) + mContent.addView(v); } public void setViewBehindLeft(CustomViewBehind cvb) { @@ -602,7 +602,7 @@ private int getViewCount() { int count = 1; count += (mViewBehindLeft != null) ? 1 : 0; count += (mViewBehindRight != null) ? 1 : 0; - return count; + return 3; } @Override @@ -996,16 +996,25 @@ public boolean onTouchEvent(MotionEvent ev) { return true; } - private float mScrollScale; + private float mScrollScaleLeft; + private float mScrollScaleRight; - public float getScrollScale() { - return mScrollScale; + public float getScrollScale(int side) { + if (side == SlidingMenu.LEFT) { + return mScrollScaleLeft; + } else { + return mScrollScaleRight; + } } - public void setScrollScale(float f) { + public void setScrollScale(float f, int side) { if (f < 0 && f > 1) throw new IllegalArgumentException("ScrollScale must be between 0 and 1"); - mScrollScale = f; + if (side == SlidingMenu.LEFT) { + mScrollScaleLeft = f; + } else if (side == SlidingMenu.RIGHT) { + mScrollScaleRight = f; + } } @Override @@ -1033,10 +1042,10 @@ public void scrollTo(int x, int y) { } // scroll the behind views if (mViewBehindLeft != null && mEnabled) { - mViewBehindLeft.scrollTo((int)(x*mScrollScale), y); + mViewBehindLeft.scrollTo((int)(x*mScrollScaleLeft), y); } if (mViewBehindRight != null && mEnabled) { - int x2 = (int) ((x - 2*getBehindWidth()) * mScrollScale); + int x2 = (int) ((x - 2*getBehindWidth()) * mScrollScaleRight); mViewBehindRight.scrollTo(x2, y); } @@ -1095,8 +1104,8 @@ protected boolean fitSystemWindows(Rect insets) { if (mContent != null) { int leftPadding = mContent.getPaddingLeft() + insets.left; int rightPadding = mContent.getPaddingRight() + insets.right; - int topPadding = mContent.getPaddingTop() + insets.top; - int bottomPadding = mContent.getPaddingBottom() + insets.bottom; + int topPadding = insets.top; + int bottomPadding = insets.bottom; mContent.setPadding(leftPadding, topPadding, rightPadding, bottomPadding); return true; } diff --git a/library/src/com/slidingmenu/lib/SlidingMenu.java b/library/src/com/slidingmenu/lib/SlidingMenu.java index 0d77aa224..8d6f6e15b 100644 --- a/library/src/com/slidingmenu/lib/SlidingMenu.java +++ b/library/src/com/slidingmenu/lib/SlidingMenu.java @@ -29,13 +29,13 @@ public class SlidingMenu extends RelativeLayout { public static final String TAG = "SlidingMenu"; - - public static final int TOUCHMODE_MARGIN = 0; - public static final int TOUCHMODE_FULLSCREEN = 1; - public static final int SIDES_LEFT = 0; - public static final int SIDES_RIGHT = 1; - public static final int SIDES_LEFT_RIGHT = 2; + public static final int TOUCHMODE_MARGIN = 3; + public static final int TOUCHMODE_FULLSCREEN = 4; + + public static final int LEFT = 0x0100; + public static final int RIGHT = 0x1000; + public static final int BOTH = LEFT | RIGHT; private CustomViewAbove mViewAbove; private CustomViewBehind mViewBehindLeft; @@ -46,7 +46,6 @@ public class SlidingMenu extends RelativeLayout { private boolean mSlidingEnabled; public static void attachSlidingMenu(Activity activity, SlidingMenu sm, boolean slidingTitle) { - if (sm.getParent() != null) throw new IllegalArgumentException("SlidingMenu cannot be attached to another view when" + " calling the static method attachSlidingMenu"); @@ -122,22 +121,11 @@ public void onPageSelected(int position) { } } }); + initializeLeft(); + initializeRight(); // now style everything! TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.SlidingMenu); - int slidingSides = ta.getInt(R.styleable.SlidingMenu_slidingSides, SIDES_LEFT); - switch (slidingSides) { - case SIDES_LEFT: - initializeLeft(); - break; - case SIDES_RIGHT: - initializeRight(); - break; - case SIDES_LEFT_RIGHT: - initializeLeft(); - initializeRight(); - break; - } // set the above and behind views if defined in xml int viewAbove = ta.getResourceId(R.styleable.SlidingMenu_viewAbove, -1); if (viewAbove != -1) { @@ -145,11 +133,11 @@ public void onPageSelected(int position) { } int viewBehindLeft = ta.getResourceId(R.styleable.SlidingMenu_viewBehindLeft, -1); if (viewBehindLeft != -1) { - setViewBehindLeft(viewBehindLeft); + setViewBehind(viewBehindLeft, LEFT); } int viewBehindRight = ta.getResourceId(R.styleable.SlidingMenu_viewBehindRight, -1); if (viewBehindRight != -1) { - setViewBehindRight(viewBehindRight); + setViewBehind(viewBehindRight, RIGHT); } int touchModeAbove = ta.getInt(R.styleable.SlidingMenu_aboveTouchMode, TOUCHMODE_MARGIN); setTouchModeAbove(touchModeAbove); @@ -160,14 +148,17 @@ public void onPageSelected(int position) { int widthBehind = (int) ta.getDimension(R.styleable.SlidingMenu_behindWidth, -1); if (offsetBehind != -1 && widthBehind != -1) throw new IllegalStateException("Cannot set both behindOffset and behindWidth for a SlidingMenu"); - else if (offsetBehind != -1) - setBehindOffset(offsetBehind); - else if (widthBehind != -1) - setBehindWidth(widthBehind); - else - setBehindOffset(0); + else if (offsetBehind != -1) { + setBehindOffset(offsetBehind, BOTH); + } + else if (widthBehind != -1) { + setBehindWidth(widthBehind, BOTH); + } + else { + setBehindOffset(0, BOTH); + } float scrollOffsetBehind = ta.getFloat(R.styleable.SlidingMenu_behindScrollScale, 0.33f); - setBehindScrollScale(scrollOffsetBehind); + setBehindScrollScale(scrollOffsetBehind, BOTH); int shadowRes = ta.getResourceId(R.styleable.SlidingMenu_shadowDrawable, -1); if (shadowRes != -1) { setShadowDrawable(shadowRes); @@ -205,6 +196,16 @@ private void initializeRight() { } } + public void removeViewBehind(int side) { + if (isLeft(side)) { + mViewAbove.setViewBehindLeft(null); + mViewBehindLeft = null; + } else if (isRight(side)) { + mViewAbove.setViewBehindRight(null); + mViewBehindRight = null; + } + } + public void setContent(int res) { setContent(LayoutInflater.from(getContext()).inflate(res, null)); } @@ -215,24 +216,52 @@ public void setContent(View v) { showAbove(); } - public void setViewBehindRight(int res) { - setViewBehindRight(LayoutInflater.from(getContext()).inflate(res, null)); + public void setViewBehind(int res, int side) { + setViewBehind(LayoutInflater.from(getContext()).inflate(res, null), side); } - public void setViewBehindRight(View v) { - initializeRight(); - mViewBehindRight.setContent(v); - mViewBehindRight.invalidate(); + public void setViewBehind(View v, int side) { + checkSide(side); + if (side == LEFT) { + if (v == null) { + removeViewBehind(LEFT); + } else { + initializeLeft(); + mViewBehindLeft.setContent(v); + mViewBehindLeft.invalidate(); + } + } else { + if (v == null) { + removeViewBehind(RIGHT); + } else { + initializeRight(); + mViewBehindRight.setContent(v); + mViewBehindRight.invalidate(); + } + } } - public void setViewBehindLeft(int res) { - setViewBehindLeft(LayoutInflater.from(getContext()).inflate(res, null)); + private void checkSide(int side) { + if (!isLeft(side) && !isRight(side)) + throw new IllegalArgumentException("Side must be SlidingMenu.LEFT or SlidingMenu.RIGHT"); } - public void setViewBehindLeft(View v) { - initializeLeft(); - mViewBehindLeft.setContent(v); - mViewBehindLeft.invalidate(); + private void checkSideStrict(int side) { + checkSide(side); + if (isLeft(side) && mViewBehindLeft == null) + throw new IllegalArgumentException("You have to add a behind left " + + "view before you can reference it"); + if (isRight(side) && mViewBehindRight == null) + throw new IllegalArgumentException("You have to add a behind right " + + "view before you can reference it"); + } + + private boolean isLeft(int side) { + return (side & LEFT) == LEFT; + } + + private boolean isRight(int side) { + return (side & RIGHT) == RIGHT; } public void setSlidingEnabled(boolean b) { @@ -243,30 +272,20 @@ public boolean isSlidingEnabled() { return mViewAbove.isSlidingEnabled(); } - /** - * - * @param b Whether or not the SlidingMenu is in a static mode - * (i.e. nothing is moving and everything is showing) - */ - public void setStatic(boolean b) { - if (b) { - setSlidingEnabled(false); - mViewAbove.setViewBehindLeft(null); - mViewAbove.setCurrentItem(1); - mViewBehindLeft.setCurrentItem(0); - } else { - mViewAbove.setCurrentItem(1); - mViewBehindLeft.setCurrentItem(1); - mViewAbove.setViewBehindLeft(mViewBehindLeft); - setSlidingEnabled(true); - } - } - /** * Shows the behind view */ - public void showBehind() { - mViewAbove.setCurrentItem(0); + public void showBehind(int side) { + checkSideStrict(side); + + if (isLeft(side) && isRight(side)) + throw new IllegalArgumentException("Can't show both left and right"); + + if (isLeft(side)) { + mViewAbove.setCurrentItem(0); + } else if (isRight(side)) { + mViewAbove.setCurrentItem(2); + } } /** @@ -288,25 +307,31 @@ public boolean isBehindShowing() { * * @return The margin on the right of the screen that the behind view scrolls to */ - public int getBehindOffset() { - return ((RelativeLayout.LayoutParams)mViewBehindLeft.getLayoutParams()).rightMargin; + public int getBehindOffset(int side) { + checkSideStrict(side); + if (isLeft(side)) { + return ((RelativeLayout.LayoutParams)mViewBehindLeft.getLayoutParams()).rightMargin; + } else { + return ((RelativeLayout.LayoutParams)mViewBehindRight.getLayoutParams()).leftMargin; + } } /** * * @param i The margin on the right of the screen that the behind view scrolls to */ - public void setBehindOffset(int i) { - // behind left - if (mViewBehindLeft != null) { + public void setBehindOffset(int i, int side) { + checkSideStrict(side); + if (isLeft(side)) { + // behind left RelativeLayout.LayoutParams params = ((RelativeLayout.LayoutParams)mViewBehindLeft.getLayoutParams()); int bottom = params.bottomMargin; int top = params.topMargin; int left = params.leftMargin; params.setMargins(left, top, i, bottom); - } - // behind right - if (mViewBehindRight != null) { + } + if (isRight(side)) { + // behind right RelativeLayout.LayoutParams params = ((RelativeLayout.LayoutParams)mViewBehindRight.getLayoutParams()); int bottom = params.bottomMargin; int top = params.topMargin; @@ -318,8 +343,17 @@ public void setBehindOffset(int i) { showAbove(); } + /** + * + * @param res The dimension resource to be set as the behind offset + */ + public void setBehindOffsetRes(int res, int side) { + int i = (int) getContext().getResources().getDimension(res); + setBehindOffset(i, side); + } + @SuppressWarnings("deprecation") - public void setBehindWidth(int i) { + public void setBehindWidth(int i, int side) { int width; Display display = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)) .getDefaultDisplay(); @@ -333,24 +367,15 @@ public void setBehindWidth(int i) { } catch (Exception e) { width = display.getWidth(); } - setBehindOffset(width-i); - } - - /** - * - * @param res The dimension resource to be set as the behind offset - */ - public void setBehindOffsetRes(int res) { - int i = (int) getContext().getResources().getDimension(res); - setBehindOffset(i); + setBehindOffset(width-i, side); } /** * * @return The scale of the parallax scroll */ - public float getBehindScrollScale() { - return mViewAbove.getScrollScale(); + public float getBehindScrollScale(int side) { + return mViewAbove.getScrollScale(side); } /** @@ -358,12 +383,19 @@ public float getBehindScrollScale() { * @param f The scale of the parallax scroll (i.e. 1.0f scrolls 1 pixel for every * 1 pixel that the above view scrolls and 0.0f scrolls 0 pixels) */ - public void setBehindScrollScale(float f) { - mViewAbove.setScrollScale(f); - } - - public void setBehindCanvasTransformer(CanvasTransformer t) { - mViewBehindLeft.setCanvasTransformer(t); + public void setBehindScrollScale(float f, int side) { + checkSide(side); + mViewAbove.setScrollScale(f, side); + } + + public void setBehindCanvasTransformer(CanvasTransformer t, int side) { + checkSideStrict(side); + if (isLeft(side)) { + mViewBehindLeft.setCanvasTransformer(t); + } + if (isRight(side)) { + mViewBehindRight.setCanvasTransformer(t); + } } public int getTouchModeAbove() { @@ -495,7 +527,8 @@ protected void onRestoreInstanceState(Parcelable state) { super.onRestoreInstanceState(ss.getSuperState()); if (ss.mBehindShowing) { - showBehind(); + // TODO fix this + showBehind(LEFT); } else { showAbove(); } @@ -506,8 +539,8 @@ protected boolean fitSystemWindows(Rect insets) { int leftPadding = getPaddingLeft() + insets.left; int rightPadding = getPaddingRight() + insets.right; - int topPadding = getPaddingTop() + insets.top; - int bottomPadding = getPaddingBottom() + insets.bottom; + int topPadding = insets.top; + int bottomPadding = insets.bottom; this.setPadding(leftPadding, topPadding, rightPadding, bottomPadding); return super.fitSystemWindows(insets); diff --git a/library/src/com/slidingmenu/lib/app/SlidingActivity.java b/library/src/com/slidingmenu/lib/app/SlidingActivity.java index fb74ec180..88341b1c9 100644 --- a/library/src/com/slidingmenu/lib/app/SlidingActivity.java +++ b/library/src/com/slidingmenu/lib/app/SlidingActivity.java @@ -49,32 +49,46 @@ public void setContentView(View v, LayoutParams params) { mHelper.registerAboveContentView(v, params); } - public void setBehindContentView(int id) { - setBehindContentView(getLayoutInflater().inflate(id, null)); + // behind left view + public void setBehindLeftContentView(int id) { + setBehindLeftContentView(getLayoutInflater().inflate(id, null)); } - public void setBehindContentView(View v) { - setBehindContentView(v, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); + public void setBehindLeftContentView(View v) { + setBehindLeftContentView(v, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); } - public void setBehindContentView(View v, LayoutParams params) { - mHelper.setBehindContentView(v, params); + public void setBehindLeftContentView(View v, LayoutParams params) { + mHelper.setBehindLeftContentView(v); + } + + // behind right view + public void setBehindRightContentView(int id) { + setBehindRightContentView(getLayoutInflater().inflate(id, null)); + } + + public void setBehindRightContentView(View v) { + setBehindRightContentView(v, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); + } + + public void setBehindRightContentView(View v, LayoutParams params) { + mHelper.setBehindRightContentView(v); } public SlidingMenu getSlidingMenu() { return mHelper.getSlidingMenu(); } - public void toggle() { - mHelper.toggle(); + public void toggle(int side) { + mHelper.toggle(side); } public void showAbove() { mHelper.showAbove(); } - public void showBehind() { - mHelper.showBehind(); + public void showBehind(int side) { + mHelper.showBehind(side); } public void setSlidingActionBarEnabled(boolean b) { diff --git a/library/src/com/slidingmenu/lib/app/SlidingActivityBase.java b/library/src/com/slidingmenu/lib/app/SlidingActivityBase.java index 319b86e75..14f0b4a0a 100644 --- a/library/src/com/slidingmenu/lib/app/SlidingActivityBase.java +++ b/library/src/com/slidingmenu/lib/app/SlidingActivityBase.java @@ -7,14 +7,16 @@ public interface SlidingActivityBase { - public void setBehindContentView(View v, LayoutParams p); + public void setBehindLeftContentView(View v, LayoutParams p); + + public void setBehindRightContentView(View v, LayoutParams p); public SlidingMenu getSlidingMenu(); - public void toggle(); + public void toggle(int side); public void showAbove(); - public void showBehind(); + public void showBehind(int side); } diff --git a/library/src/com/slidingmenu/lib/app/SlidingActivityHelper.java b/library/src/com/slidingmenu/lib/app/SlidingActivityHelper.java index 28b13ffab..bab2cba8d 100644 --- a/library/src/com/slidingmenu/lib/app/SlidingActivityHelper.java +++ b/library/src/com/slidingmenu/lib/app/SlidingActivityHelper.java @@ -19,7 +19,8 @@ public class SlidingActivityHelper { private SlidingMenu mSlidingMenu; private View mViewAbove; - private View mViewBehind; + private View mViewBehindLeft; + private View mViewBehindRight; private boolean mBroadcasting = false; private boolean mOnPostCreateCalled = false; @@ -36,10 +37,14 @@ public void onCreate(Bundle savedInstanceState) { } public void onPostCreate(Bundle savedInstanceState) { - if (mViewBehind == null || mViewAbove == null) { - throw new IllegalStateException("Both setBehindContentView must be called " + + if ((mViewBehindLeft == null && mViewBehindRight == null) || mViewAbove == null) { + throw new IllegalStateException("Both setBehind[Left|Right]ContentView must be called " + "in onCreate in addition to setContentView."); } + if (mViewBehindLeft == null) + mSlidingMenu.setViewBehind(null, SlidingMenu.LEFT); + if (mViewBehindRight == null) + mSlidingMenu.setViewBehind(null, SlidingMenu.RIGHT); mOnPostCreateCalled = true; @@ -103,20 +108,25 @@ public void setContentView(View v) { mActivity.setContentView(v); } - public void setBehindContentView(View v, LayoutParams params) { - mViewBehind = v; - mSlidingMenu.setViewBehindLeft(mViewBehind); + public void setBehindLeftContentView(View v) { + mViewBehindLeft = v; + mSlidingMenu.setViewBehind(mViewBehindLeft, SlidingMenu.LEFT); + } + + public void setBehindRightContentView(View v) { + mViewBehindRight = v; + mSlidingMenu.setViewBehind(mViewBehindRight, SlidingMenu.RIGHT); } public SlidingMenu getSlidingMenu() { return mSlidingMenu; } - public void toggle() { + public void toggle(int side) { if (mSlidingMenu.isBehindShowing()) { showAbove(); } else { - showBehind(); + showBehind(side); } } @@ -124,8 +134,8 @@ public void showAbove() { mSlidingMenu.showAbove(); } - public void showBehind() { - mSlidingMenu.showBehind(); + public void showBehind(int side) { + mSlidingMenu.showBehind(side); } public boolean onKeyUp(int keyCode, KeyEvent event) { diff --git a/library/src/com/slidingmenu/lib/app/SlidingFragmentActivity.java b/library/src/com/slidingmenu/lib/app/SlidingFragmentActivity.java index 8f4651017..cd294654d 100644 --- a/library/src/com/slidingmenu/lib/app/SlidingFragmentActivity.java +++ b/library/src/com/slidingmenu/lib/app/SlidingFragmentActivity.java @@ -49,32 +49,46 @@ public void setContentView(View v, LayoutParams params) { mHelper.registerAboveContentView(v, params); } - public void setBehindContentView(int id) { - setBehindContentView(getLayoutInflater().inflate(id, null)); + // behind left view + public void setBehindLeftContentView(int id) { + setBehindLeftContentView(getLayoutInflater().inflate(id, null)); } - public void setBehindContentView(View v) { - setBehindContentView(v, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); + public void setBehindLeftContentView(View v) { + setBehindLeftContentView(v, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); } - public void setBehindContentView(View v, LayoutParams params) { - mHelper.setBehindContentView(v, params); + public void setBehindLeftContentView(View v, LayoutParams params) { + mHelper.setBehindLeftContentView(v); } + // behind right view + public void setBehindRightContentView(int id) { + setBehindRightContentView(getLayoutInflater().inflate(id, null)); + } + + public void setBehindRightContentView(View v) { + setBehindRightContentView(v, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); + } + + public void setBehindRightContentView(View v, LayoutParams params) { + mHelper.setBehindRightContentView(v); + } + public SlidingMenu getSlidingMenu() { return mHelper.getSlidingMenu(); } - public void toggle() { - mHelper.toggle(); + public void toggle(int side) { + mHelper.toggle(side); } public void showAbove() { mHelper.showAbove(); } - public void showBehind() { - mHelper.showBehind(); + public void showBehind(int side) { + mHelper.showBehind(side); } public void setSlidingActionBarEnabled(boolean b) { diff --git a/library/src/com/slidingmenu/lib/app/SlidingListActivity.java b/library/src/com/slidingmenu/lib/app/SlidingListActivity.java index 38860653a..3b589d392 100644 --- a/library/src/com/slidingmenu/lib/app/SlidingListActivity.java +++ b/library/src/com/slidingmenu/lib/app/SlidingListActivity.java @@ -53,32 +53,46 @@ public void setContentView(View v, LayoutParams params) { mHelper.registerAboveContentView(v, params); } - public void setBehindContentView(int id) { - setBehindContentView(getLayoutInflater().inflate(id, null)); + // behind left view + public void setBehindLeftContentView(int id) { + setBehindLeftContentView(getLayoutInflater().inflate(id, null)); } - public void setBehindContentView(View v) { - setBehindContentView(v, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); + public void setBehindLeftContentView(View v) { + setBehindLeftContentView(v, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); } - public void setBehindContentView(View v, LayoutParams params) { - mHelper.setBehindContentView(v, params); + public void setBehindLeftContentView(View v, LayoutParams params) { + mHelper.setBehindLeftContentView(v); + } + + // behind right view + public void setBehindRightContentView(int id) { + setBehindRightContentView(getLayoutInflater().inflate(id, null)); + } + + public void setBehindRightContentView(View v) { + setBehindRightContentView(v, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); + } + + public void setBehindRightContentView(View v, LayoutParams params) { + mHelper.setBehindRightContentView(v); } public SlidingMenu getSlidingMenu() { return mHelper.getSlidingMenu(); } - public void toggle() { - mHelper.toggle(); + public void toggle(int side) { + mHelper.toggle(side); } public void showAbove() { mHelper.showAbove(); } - public void showBehind() { - mHelper.showBehind(); + public void showBehind(int side) { + mHelper.showBehind(side); } public void setSlidingActionBarEnabled(boolean b) { diff --git a/library/src/com/slidingmenu/lib/app/SlidingMapActivity.java b/library/src/com/slidingmenu/lib/app/SlidingMapActivity.java index 27fbea3ef..b2af04af8 100644 --- a/library/src/com/slidingmenu/lib/app/SlidingMapActivity.java +++ b/library/src/com/slidingmenu/lib/app/SlidingMapActivity.java @@ -50,32 +50,46 @@ public void setContentView(View v, LayoutParams params) { mHelper.registerAboveContentView(v, params); } - public void setBehindContentView(int id) { - setBehindContentView(getLayoutInflater().inflate(id, null)); + // behind left view + public void setBehindLeftContentView(int id) { + setBehindLeftContentView(getLayoutInflater().inflate(id, null)); } - public void setBehindContentView(View v) { - setBehindContentView(v, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); + public void setBehindLeftContentView(View v) { + setBehindLeftContentView(v, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); } - public void setBehindContentView(View v, LayoutParams params) { - mHelper.setBehindContentView(v, params); + public void setBehindLeftContentView(View v, LayoutParams params) { + mHelper.setBehindLeftContentView(v); + } + + // behind right view + public void setBehindRightContentView(int id) { + setBehindRightContentView(getLayoutInflater().inflate(id, null)); + } + + public void setBehindRightContentView(View v) { + setBehindRightContentView(v, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); + } + + public void setBehindRightContentView(View v, LayoutParams params) { + mHelper.setBehindRightContentView(v); } public SlidingMenu getSlidingMenu() { return mHelper.getSlidingMenu(); } - public void toggle() { - mHelper.toggle(); + public void toggle(int side) { + mHelper.toggle(side); } public void showAbove() { mHelper.showAbove(); } - public void showBehind() { - mHelper.showBehind(); + public void showBehind(int side) { + mHelper.showBehind(side); } public void setSlidingActionBarEnabled(boolean b) { From cc50325265ac7b7e645d37c455fa07fc9c4c75d9 Mon Sep 17 00:00:00 2001 From: jfeinstein10 Date: Wed, 19 Sep 2012 11:45:29 -0400 Subject: [PATCH 06/17] Fixed scroll scale issue --- library/src/com/slidingmenu/lib/CustomViewAbove.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/library/src/com/slidingmenu/lib/CustomViewAbove.java b/library/src/com/slidingmenu/lib/CustomViewAbove.java index 9e1465549..a87b58a82 100644 --- a/library/src/com/slidingmenu/lib/CustomViewAbove.java +++ b/library/src/com/slidingmenu/lib/CustomViewAbove.java @@ -1014,6 +1014,9 @@ public void setScrollScale(float f, int side) { mScrollScaleLeft = f; } else if (side == SlidingMenu.RIGHT) { mScrollScaleRight = f; + } else if (side == SlidingMenu.BOTH) { + mScrollScaleLeft = f; + mScrollScaleRight = f; } } From e0971128f2001bc898885951b25d44b99138731f Mon Sep 17 00:00:00 2001 From: jfeinstein10 Date: Wed, 19 Sep 2012 16:54:36 -0400 Subject: [PATCH 07/17] Fixed layout height issue --- example/res/layout/test.xml | 18 +++++++++++++ .../com/slidingmenu/example/BaseActivity.java | 8 +++--- .../slidingmenu/example/CustomAnimation.java | 4 +-- .../example/CustomRotateAnimation.java | 3 ++- .../example/PropertiesActivity.java | 22 +++++++-------- .../com/slidingmenu/lib/CustomViewAbove.java | 27 +------------------ .../com/slidingmenu/lib/CustomViewBehind.java | 2 +- .../src/com/slidingmenu/lib/SlidingMenu.java | 3 ++- 8 files changed, 42 insertions(+), 45 deletions(-) create mode 100644 example/res/layout/test.xml diff --git a/example/res/layout/test.xml b/example/res/layout/test.xml new file mode 100644 index 000000000..50bf5f76d --- /dev/null +++ b/example/res/layout/test.xml @@ -0,0 +1,18 @@ + + + + + + + + \ No newline at end of file diff --git a/example/src/com/slidingmenu/example/BaseActivity.java b/example/src/com/slidingmenu/example/BaseActivity.java index 7d23bf012..5f0286880 100644 --- a/example/src/com/slidingmenu/example/BaseActivity.java +++ b/example/src/com/slidingmenu/example/BaseActivity.java @@ -12,6 +12,7 @@ import android.support.v4.app.ListFragment; import android.support.v4.view.ViewPager; import android.view.MenuItem; +import android.widget.LinearLayout; import com.slidingmenu.lib.SlidingMenu; import com.slidingmenu.lib.app.SlidingFragmentActivity; @@ -32,17 +33,18 @@ public void onCreate(Bundle savedInstanceState) { setTitle(mTitleRes); // set the Behind View - setBehindContentView(R.layout.menu_frame); + setBehindLeftContentView(R.layout.menu_frame); FragmentTransaction t = this.getSupportFragmentManager().beginTransaction(); mFrag = new SampleListFragment(); t.replace(R.id.menu_frame, mFrag); t.commit(); + setBehindRightContentView(R.layout.test); // customize the SlidingMenu SlidingMenu sm = getSlidingMenu(); sm.setShadowWidthRes(R.dimen.shadow_width); sm.setShadowDrawable(R.drawable.shadow); - sm.setBehindOffsetRes(R.dimen.actionbar_home_width); + sm.setBehindOffsetRes(R.dimen.actionbar_home_width, SlidingMenu.LEFT | SlidingMenu.RIGHT); // customize the ActionBar if (Build.VERSION.SDK_INT >= 11) { @@ -54,7 +56,7 @@ public void onCreate(Bundle savedInstanceState) { public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: - toggle(); + toggle(SlidingMenu.LEFT); return true; } return super.onOptionsItemSelected(item); diff --git a/example/src/com/slidingmenu/example/CustomAnimation.java b/example/src/com/slidingmenu/example/CustomAnimation.java index 63308dbcd..5dd7d547d 100644 --- a/example/src/com/slidingmenu/example/CustomAnimation.java +++ b/example/src/com/slidingmenu/example/CustomAnimation.java @@ -27,8 +27,8 @@ public void onCreate(Bundle savedInstanceState) { SlidingMenu sm = getSlidingMenu(); setSlidingActionBarEnabled(true); - sm.setBehindScrollScale(0.0f); - sm.setBehindCanvasTransformer(mTransformer); + sm.setBehindScrollScale(0.0f, SlidingMenu.LEFT | SlidingMenu.RIGHT); + sm.setBehindCanvasTransformer(mTransformer, SlidingMenu.LEFT | SlidingMenu.RIGHT); } @Override diff --git a/example/src/com/slidingmenu/example/CustomRotateAnimation.java b/example/src/com/slidingmenu/example/CustomRotateAnimation.java index a9f8d65ff..55e734c37 100644 --- a/example/src/com/slidingmenu/example/CustomRotateAnimation.java +++ b/example/src/com/slidingmenu/example/CustomRotateAnimation.java @@ -4,6 +4,7 @@ import android.os.Bundle; import android.view.animation.Interpolator; +import com.slidingmenu.lib.SlidingMenu; import com.slidingmenu.lib.SlidingMenu.CanvasTransformer; public class CustomRotateAnimation extends CustomAnimation { @@ -31,7 +32,7 @@ public void transformCanvas(Canvas canvas, float percentOpen) { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - getSlidingMenu().setBehindScrollScale(1); + getSlidingMenu().setBehindScrollScale(1, SlidingMenu.BOTH); } } diff --git a/example/src/com/slidingmenu/example/PropertiesActivity.java b/example/src/com/slidingmenu/example/PropertiesActivity.java index 7f8bd4a1a..14f0f3015 100644 --- a/example/src/com/slidingmenu/example/PropertiesActivity.java +++ b/example/src/com/slidingmenu/example/PropertiesActivity.java @@ -15,15 +15,15 @@ public class PropertiesActivity extends BaseActivity { public PropertiesActivity() { super(R.string.properties); } - + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setSlidingActionBarEnabled(true); - + setContentView(R.layout.properties); - + // touch mode stuff RadioGroup touchAbove = (RadioGroup) findViewById(R.id.touch_above); touchAbove.check(R.id.touch_above_margin); @@ -40,7 +40,7 @@ public void onCheckedChanged(RadioGroup group, int checkedId) { } } }); - + RadioGroup touchBehind = (RadioGroup) findViewById(R.id.touch_behind); touchBehind.check(R.id.touch_behind_margin); touchBehind.setOnCheckedChangeListener(new OnCheckedChangeListener() { @@ -57,7 +57,7 @@ public void onCheckedChanged(RadioGroup group, int checkedId) { } }); - + // scroll scale stuff SeekBar scrollScale = (SeekBar) findViewById(R.id.scroll_scale); scrollScale.setMax(1000); @@ -70,11 +70,11 @@ public void onProgressChanged(SeekBar seekBar, int progress, public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { - getSlidingMenu().setBehindScrollScale((float) seekBar.getProgress()/seekBar.getMax()); + getSlidingMenu().setBehindScrollScale((float) seekBar.getProgress()/seekBar.getMax(), SlidingMenu.BOTH); } }); - - + + // behind width stuff SeekBar behindWidth = (SeekBar) findViewById(R.id.behind_width); behindWidth.setMax(1000); @@ -88,11 +88,11 @@ public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { float percent = (float) seekBar.getProgress()/seekBar.getMax(); - getSlidingMenu().setBehindWidth((int) (percent * getSlidingMenu().getWidth())); + getSlidingMenu().setBehindWidth((int) (percent * getSlidingMenu().getWidth()), SlidingMenu.BOTH); } }); - - + + // fading stuff CheckBox fadeEnabled = (CheckBox) findViewById(R.id.fade_enabled); fadeEnabled.setChecked(true); diff --git a/library/src/com/slidingmenu/lib/CustomViewAbove.java b/library/src/com/slidingmenu/lib/CustomViewAbove.java index a87b58a82..bba7d942d 100644 --- a/library/src/com/slidingmenu/lib/CustomViewAbove.java +++ b/library/src/com/slidingmenu/lib/CustomViewAbove.java @@ -796,9 +796,6 @@ public boolean onInterceptTouchEvent(MotionEvent ev) { final int action = ev.getAction() & MotionEventCompat.ACTION_MASK; - if (action == MotionEvent.ACTION_DOWN) - Log.v(TAG, "Received ACTION_DOWN"); - if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) { mIsBeingDragged = false; mIsUnableToDrag = false; @@ -842,7 +839,6 @@ else if (mIsUnableToDrag) case MotionEvent.ACTION_DOWN: mActivePointerId = ev.getAction() & ((Build.VERSION.SDK_INT >= 8) ? MotionEvent.ACTION_POINTER_INDEX_MASK : MotionEvent.ACTION_POINTER_ID_MASK); - Log.v(TAG, "active pointer id : " + mActivePointerId); mLastMotionX = mInitialMotionX = MotionEventCompat.getX(ev, mActivePointerId); mLastMotionY = MotionEventCompat.getY(ev, mActivePointerId); if (thisTouchAllowed(ev)) { @@ -1064,7 +1060,7 @@ private int determineTargetPage(int currentPage, float pageOffset, int velocity, } else { targetPage = (int) (currentPage + pageOffset + 0.5f); } - Log.v(TAG, "targetPage : " + targetPage); + if (DEBUG) Log.v(TAG, "targetPage : " + targetPage); return targetPage; } @@ -1096,26 +1092,6 @@ protected void dispatchDraw(Canvas canvas) { onDrawMenuSelector(canvas); } - /** - * Pads our content window so that it fits within the system windows. - * @param insets The insets by which we need to offset our view. - * @return True since we handled the padding change. - */ - @Override - protected boolean fitSystemWindows(Rect insets) { - - if (mContent != null) { - int leftPadding = mContent.getPaddingLeft() + insets.left; - int rightPadding = mContent.getPaddingRight() + insets.right; - int topPadding = insets.top; - int bottomPadding = insets.bottom; - mContent.setPadding(leftPadding, topPadding, rightPadding, bottomPadding); - return true; - } - - return super.fitSystemWindows(insets); - } - @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); @@ -1132,7 +1108,6 @@ protected void onDraw(Canvas canvas) { private Bitmap mSelectorDrawable; private View mSelectedView; - private void onDrawBehindFade(Canvas canvas) { int alpha; if (mViewBehindLeft != null) { diff --git a/library/src/com/slidingmenu/lib/CustomViewBehind.java b/library/src/com/slidingmenu/lib/CustomViewBehind.java index 2377bd520..abd134381 100644 --- a/library/src/com/slidingmenu/lib/CustomViewBehind.java +++ b/library/src/com/slidingmenu/lib/CustomViewBehind.java @@ -53,7 +53,7 @@ public int getChildLeft(int i) { protected void onLayout(boolean changed, int l, int t, int r, int b) { final int width = r - l; final int height = b - t; - mContent.layout(0, 0, 0 + width, height); + mContent.layout(0, 0, width, height); } @Override diff --git a/library/src/com/slidingmenu/lib/SlidingMenu.java b/library/src/com/slidingmenu/lib/SlidingMenu.java index 8d6f6e15b..7f762144b 100644 --- a/library/src/com/slidingmenu/lib/SlidingMenu.java +++ b/library/src/com/slidingmenu/lib/SlidingMenu.java @@ -543,7 +543,8 @@ protected boolean fitSystemWindows(Rect insets) { int bottomPadding = insets.bottom; this.setPadding(leftPadding, topPadding, rightPadding, bottomPadding); - return super.fitSystemWindows(insets); + // return false to propagate the fit to our child views + return false; } } \ No newline at end of file From 431424ced7f4921ac3d2aa68adc420257977457f Mon Sep 17 00:00:00 2001 From: jfeinstein10 Date: Mon, 24 Sep 2012 16:48:34 -0400 Subject: [PATCH 08/17] Fixed different scroll scale scrolling issue --- .../com/slidingmenu/lib/CustomViewAbove.java | 93 ++++++++++--------- .../src/com/slidingmenu/lib/SlidingMenu.java | 8 +- 2 files changed, 56 insertions(+), 45 deletions(-) diff --git a/library/src/com/slidingmenu/lib/CustomViewAbove.java b/library/src/com/slidingmenu/lib/CustomViewAbove.java index bba7d942d..667d0bca5 100644 --- a/library/src/com/slidingmenu/lib/CustomViewAbove.java +++ b/library/src/com/slidingmenu/lib/CustomViewAbove.java @@ -61,7 +61,8 @@ public float getInterpolation(float t) { private Scroller mScroller; private int mShadowWidth; - private Drawable mShadowDrawable; + private Drawable mShadowDrawableLeft; + private Drawable mShadowDrawableRight; private boolean mScrollingCacheEnabled; @@ -266,11 +267,14 @@ void setCurrentItemInternal(int item, boolean smoothScroll, boolean always, int setScrollingCacheEnabled(false); return; } + + int start = mViewBehindLeft == null ? 1 : 0; + int end = mViewBehindRight == null ? 1 : 2; // TODO count accordingly - if (item < 0) { - item = 0; - } else if (item >= getViewCount()) { - item = getViewCount() - 1; + if (item < start) { + item = start; + } else if (item > end) { + item = end; } final boolean dispatchSelected = mCurItem != item; @@ -355,10 +359,19 @@ public int getShadowWidth() { * * @param d Drawable to display between pages */ - public void setShadowDrawable(Drawable d) { - mShadowDrawable = d; + public void setShadowDrawable(Drawable d, int side) { + if (side == SlidingMenu.LEFT) { + Log.v(TAG, "setting left"); + mShadowDrawableLeft = d; + } else if (side == SlidingMenu.RIGHT) { + Log.v(TAG, "setting right"); + mShadowDrawableRight = d; + } else if (side == SlidingMenu.BOTH) { + Log.v(TAG, "setting both"); + mShadowDrawableLeft = d; + mShadowDrawableRight = d; + } if (d != null) refreshDrawableState(); - setWillNotDraw(d == null); invalidate(); } @@ -367,21 +380,21 @@ public void setShadowDrawable(Drawable d) { * * @param resId Resource ID of a drawable to display between pages */ - public void setShadowDrawable(int resId) { - setShadowDrawable(getContext().getResources().getDrawable(resId)); + public void setShadowDrawable(int resId, int side) { + setShadowDrawable(getContext().getResources().getDrawable(resId), side); } @Override protected boolean verifyDrawable(Drawable who) { - return super.verifyDrawable(who) || who == mShadowDrawable; + return super.verifyDrawable(who) || who == mShadowDrawableLeft || who == mShadowDrawableRight; } @Override protected void drawableStateChanged() { super.drawableStateChanged(); - final Drawable d = mShadowDrawable; + final Drawable d = mShadowDrawableLeft; if (d != null && d.isStateful()) { d.setState(getDrawableState()); } @@ -404,16 +417,16 @@ public int getDestScrollX(int i) { case 1: return getBehindWidth(); case 2: - return 2*getBehindWidth(); + return mContent.getLeft() + mViewBehindRight.getWidth(); } throw new RuntimeException("This should never happen!"); } public int getRightScrollBound() { if (mViewBehindRight != null) { - return 2*getBehindWidth(); + return mContent.getLeft() + mViewBehindRight.getWidth(); } else { - return getBehindWidth(); + return mContent.getLeft(); } } @@ -421,7 +434,7 @@ public int getLeftScrollBound() { if (mViewBehindLeft != null) { return 0; } else { - return getBehindWidth(); + return mContent.getLeft(); } } @@ -434,10 +447,6 @@ public int getChildRight(int i) { return getChildLeft(i) + getChildWidth(i); } - public int getContentRight() { - return mContent.getRight(); - } - public boolean isRightOpen() { if (mViewBehindRight == null) return false; @@ -494,10 +503,9 @@ public int getChildWidth(int i) { } public int getBehindWidth() { - // if (mViewBehindLeft != null) { - // return mViewBehindLeft.getWidth(); - // } else - if (mViewBehindRight != null) { + if (mViewBehindLeft != null) { + return mViewBehindLeft.getWidth(); + } else if (mViewBehindRight != null) { return mViewBehindRight.getWidth(); } else { return 0; @@ -602,7 +610,7 @@ private int getViewCount() { int count = 1; count += (mViewBehindLeft != null) ? 1 : 0; count += (mViewBehindRight != null) ? 1 : 0; - return 3; + return count; } @Override @@ -760,7 +768,7 @@ private boolean thisTouchAllowed(MotionEvent ev) { case SlidingMenu.TOUCHMODE_FULLSCREEN: return true; case SlidingMenu.TOUCHMODE_MARGIN: - return x <= getContentRight(); + return x <= mContent.getRight(); default: return false; } @@ -929,6 +937,7 @@ public boolean onTouchEvent(MotionEvent ev) { float scrollX = oldScrollX + deltaX; final float leftBound = getLeftScrollBound(); final float rightBound = getRightScrollBound(); + Log.v(TAG, "leftBound: " + leftBound +", rightBound:" + rightBound); if (scrollX < leftBound) { scrollX = leftBound; } else if (scrollX > rightBound) { @@ -1044,11 +1053,11 @@ public void scrollTo(int x, int y) { mViewBehindLeft.scrollTo((int)(x*mScrollScaleLeft), y); } if (mViewBehindRight != null && mEnabled) { - int x2 = (int) ((x - 2*getBehindWidth()) * mScrollScaleRight); + int x2 = (int) ((x - mContent.getRight()) * mScrollScaleRight); mViewBehindRight.scrollTo(x2, y); } - if (mShadowDrawable != null || mSelectorDrawable != null) + if (mShadowDrawableLeft != null || mSelectorDrawable != null) invalidate(); } @@ -1065,11 +1074,11 @@ private int determineTargetPage(int currentPage, float pageOffset, int velocity, } protected float getPercentOpenLeft() { - return (getBehindWidth() - mScrollX) / getBehindWidth(); + return (getBehindWidth() - mScrollX) / mViewBehindLeft.getWidth(); } protected float getPercentOpenRight() { - return (getWidth() - (getContentRight() - mScrollX)) / getBehindWidth(); + return (getWidth() - (mContent.getRight() - mScrollX)) / mViewBehindRight.getWidth(); } @Override @@ -1077,12 +1086,17 @@ protected void dispatchDraw(Canvas canvas) { super.dispatchDraw(canvas); final int behindWidth = getBehindWidth(); // Draw the margin drawable if needed - if (mShadowWidth > 0 && mShadowDrawable != null) { - final int left = behindWidth - mShadowWidth; - mShadowDrawable.setBounds(left, 0, left + mShadowWidth, getHeight()); - mShadowDrawable.draw(canvas); - mShadowDrawable.setBounds(getContentRight(), 0, getContentRight() + mShadowWidth, getHeight()); - mShadowDrawable.draw(canvas); + if (mShadowWidth > 0 && mShadowDrawableLeft != null) { + if (mShadowDrawableLeft != null) { + final int left = behindWidth - mShadowWidth; + mShadowDrawableLeft.setBounds(left, 0, left + mShadowWidth, getHeight()); + mShadowDrawableLeft.draw(canvas); + } + if (mShadowDrawableRight != null) { + mShadowDrawableRight.setBounds(mContent.getRight(), 0, mContent.getRight() + mShadowWidth, getHeight()); + Log.v(TAG, "content right " + mContent.getRight()); + mShadowDrawableRight.draw(canvas); + } } if (mFadeEnabled) @@ -1092,11 +1106,6 @@ protected void dispatchDraw(Canvas canvas) { onDrawMenuSelector(canvas); } - @Override - protected void onDraw(Canvas canvas) { - super.onDraw(canvas); - } - // variables for drawing private float mScrollX = 0.0f; // for the fade @@ -1121,7 +1130,7 @@ private void onDrawBehindFade(Canvas canvas) { alpha = (int) (mFadeDegree * 255 * Math.abs(1-getPercentOpenRight())); if (alpha > 0) { mBehindFadePaint.setColor(Color.argb(alpha, 0, 0, 0)); - canvas.drawRect(getContentRight(), 0, getContentRight() + getBehindWidth(), + canvas.drawRect(mContent.getRight(), 0, mContent.getRight() + mViewBehindRight.getWidth(), getHeight(), mBehindFadePaint); } } diff --git a/library/src/com/slidingmenu/lib/SlidingMenu.java b/library/src/com/slidingmenu/lib/SlidingMenu.java index 7f762144b..c45e7f4b4 100644 --- a/library/src/com/slidingmenu/lib/SlidingMenu.java +++ b/library/src/com/slidingmenu/lib/SlidingMenu.java @@ -161,7 +161,7 @@ else if (widthBehind != -1) { setBehindScrollScale(scrollOffsetBehind, BOTH); int shadowRes = ta.getResourceId(R.styleable.SlidingMenu_shadowDrawable, -1); if (shadowRes != -1) { - setShadowDrawable(shadowRes); + setShadowDrawable(shadowRes, BOTH); } int shadowWidth = (int) ta.getDimension(R.styleable.SlidingMenu_shadowWidth, 0); setShadowWidth(shadowWidth); @@ -199,9 +199,11 @@ private void initializeRight() { public void removeViewBehind(int side) { if (isLeft(side)) { mViewAbove.setViewBehindLeft(null); + removeView(mViewBehindLeft); mViewBehindLeft = null; } else if (isRight(side)) { mViewAbove.setViewBehindRight(null); + removeView(mViewBehindRight); mViewBehindRight = null; } } @@ -422,8 +424,8 @@ public void setTouchModeBehind(int i) { mViewAbove.setTouchModeBehind(i); } - public void setShadowDrawable(int resId) { - mViewAbove.setShadowDrawable(resId); + public void setShadowDrawable(int resId, int side) { + mViewAbove.setShadowDrawable(resId, side); } public void setShadowWidthRes(int resId) { From 531a4799f8f22097142b0f6f28fdb49e45fca932 Mon Sep 17 00:00:00 2001 From: jfeinstein10 Date: Mon, 24 Sep 2012 16:55:26 -0400 Subject: [PATCH 09/17] Fixed left and right shadow issue --- library/src/com/slidingmenu/lib/CustomViewAbove.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/library/src/com/slidingmenu/lib/CustomViewAbove.java b/library/src/com/slidingmenu/lib/CustomViewAbove.java index 667d0bca5..547e4651d 100644 --- a/library/src/com/slidingmenu/lib/CustomViewAbove.java +++ b/library/src/com/slidingmenu/lib/CustomViewAbove.java @@ -937,7 +937,6 @@ public boolean onTouchEvent(MotionEvent ev) { float scrollX = oldScrollX + deltaX; final float leftBound = getLeftScrollBound(); final float rightBound = getRightScrollBound(); - Log.v(TAG, "leftBound: " + leftBound +", rightBound:" + rightBound); if (scrollX < leftBound) { scrollX = leftBound; } else if (scrollX > rightBound) { @@ -1086,7 +1085,7 @@ protected void dispatchDraw(Canvas canvas) { super.dispatchDraw(canvas); final int behindWidth = getBehindWidth(); // Draw the margin drawable if needed - if (mShadowWidth > 0 && mShadowDrawableLeft != null) { + if (mShadowWidth > 0) { if (mShadowDrawableLeft != null) { final int left = behindWidth - mShadowWidth; mShadowDrawableLeft.setBounds(left, 0, left + mShadowWidth, getHeight()); From 37d873bc431840cbf8d2a2c8cea1769a3dcabc7b Mon Sep 17 00:00:00 2001 From: jfeinstein10 Date: Mon, 24 Sep 2012 17:53:29 -0400 Subject: [PATCH 10/17] Fixed page indexing issue --- .../com/slidingmenu/lib/CustomViewAbove.java | 44 ++++--------------- 1 file changed, 8 insertions(+), 36 deletions(-) diff --git a/library/src/com/slidingmenu/lib/CustomViewAbove.java b/library/src/com/slidingmenu/lib/CustomViewAbove.java index 547e4651d..68293c2d7 100644 --- a/library/src/com/slidingmenu/lib/CustomViewAbove.java +++ b/library/src/com/slidingmenu/lib/CustomViewAbove.java @@ -267,7 +267,7 @@ void setCurrentItemInternal(int item, boolean smoothScroll, boolean always, int setScrollingCacheEnabled(false); return; } - + int start = mViewBehindLeft == null ? 1 : 0; int end = mViewBehindRight == null ? 1 : 2; // TODO count accordingly @@ -280,6 +280,7 @@ void setCurrentItemInternal(int item, boolean smoothScroll, boolean always, int final boolean dispatchSelected = mCurItem != item; mCurItem = item; final int destX = this.getDestScrollX(mCurItem); + if (dispatchSelected && mOnPageChangeListener != null) { mOnPageChangeListener.onPageSelected(item); } @@ -361,13 +362,10 @@ public int getShadowWidth() { */ public void setShadowDrawable(Drawable d, int side) { if (side == SlidingMenu.LEFT) { - Log.v(TAG, "setting left"); mShadowDrawableLeft = d; } else if (side == SlidingMenu.RIGHT) { - Log.v(TAG, "setting right"); mShadowDrawableRight = d; } else if (side == SlidingMenu.BOTH) { - Log.v(TAG, "setting both"); mShadowDrawableLeft = d; mShadowDrawableRight = d; } @@ -415,7 +413,7 @@ public int getDestScrollX(int i) { case 0: return 0; case 1: - return getBehindWidth(); + return mContent.getLeft(); case 2: return mContent.getLeft() + mViewBehindRight.getWidth(); } @@ -470,36 +468,17 @@ public int getCustomWidth() { } public int getChildWidth(int i) { - if (mViewBehindLeft != null && mViewBehindRight != null) { + try { switch (i) { case 0: - return getBehindWidth(); + return mViewBehindLeft.getWidth(); case 1: return mContent.getWidth(); case 2: - return getBehindWidth(); + return mViewBehindRight.getWidth(); } - } else if (mViewBehindLeft != null) { - switch (i) { - case 0: - return getBehindWidth(); - case 1: - return mContent.getWidth(); - } - } else if (mViewBehindRight != null) { - switch (i) { - case 0: - return mContent.getWidth(); - case 1: - return getBehindWidth(); - } - } + } catch (NullPointerException e) { } return 0; - // if (i <= 0) { - // return getBehindWidth(); - // } else { - // return getChildAt(i).getWidth(); - // } } public int getBehindWidth() { @@ -606,13 +585,6 @@ public void setViewBehindRight(CustomViewBehind cvb) { mViewBehindRight = cvb; } - private int getViewCount() { - int count = 1; - count += (mViewBehindLeft != null) ? 1 : 0; - count += (mViewBehindRight != null) ? 1 : 0; - return count; - } - @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { @@ -651,6 +623,7 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) { mMenuLeft.layout(0, 0, width, height); mContent.layout(contentLeft, 0, contentLeft + width, height); mMenuRight.layout(contentLeft + width, 0, contentLeft + 2*width, height); + this.setCurrentItemInternal(mCurItem, false, true); } @@ -1093,7 +1066,6 @@ protected void dispatchDraw(Canvas canvas) { } if (mShadowDrawableRight != null) { mShadowDrawableRight.setBounds(mContent.getRight(), 0, mContent.getRight() + mShadowWidth, getHeight()); - Log.v(TAG, "content right " + mContent.getRight()); mShadowDrawableRight.draw(canvas); } } From 3cb4309df14e83b90f5c03d86eadc053e95f202c Mon Sep 17 00:00:00 2001 From: jfeinstein10 Date: Mon, 24 Sep 2012 18:48:30 -0400 Subject: [PATCH 11/17] Fixing layout and attr stuff --- library/res/values/attrs.xml | 5 ++++ .../com/slidingmenu/lib/CustomViewAbove.java | 13 +++++---- .../src/com/slidingmenu/lib/SlidingMenu.java | 27 +++++++++---------- 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/library/res/values/attrs.xml b/library/res/values/attrs.xml index f349fb02b..6602ca305 100644 --- a/library/res/values/attrs.xml +++ b/library/res/values/attrs.xml @@ -17,6 +17,11 @@ + + + + + diff --git a/library/src/com/slidingmenu/lib/CustomViewAbove.java b/library/src/com/slidingmenu/lib/CustomViewAbove.java index 68293c2d7..cf17c7600 100644 --- a/library/src/com/slidingmenu/lib/CustomViewAbove.java +++ b/library/src/com/slidingmenu/lib/CustomViewAbove.java @@ -935,7 +935,7 @@ public boolean onTouchEvent(MotionEvent ev) { MotionEventCompat.findPointerIndex(ev, mActivePointerId); final float x = MotionEventCompat.getX(ev, activePointerIndex); final int totalDelta = (int) (x - mInitialMotionX); - int nextPage = determineTargetPage(currentPage, pageOffset, initialVelocity, + int nextPage = determineTargetPage(mCurItem, pageOffset, initialVelocity, totalDelta); setCurrentItemInternal(nextPage, true, true, initialVelocity); @@ -1034,12 +1034,15 @@ public void scrollTo(int x, int y) { } private int determineTargetPage(int currentPage, float pageOffset, int velocity, int deltaX) { - float scale = getBehindWidth()/getWidth(); int targetPage; - if (Math.abs(deltaX) > (float)mFlingDistance*scale && Math.abs(velocity) > mMinimumVelocity) { - targetPage = velocity > 0 ? currentPage : currentPage + 1; + if (Math.abs(deltaX) > (float)mFlingDistance && Math.abs(velocity) > mMinimumVelocity) { + Log.v(TAG, "in here!"); + targetPage = velocity > 0 ? currentPage - 1 : currentPage + 1; } else { - targetPage = (int) (currentPage + pageOffset + 0.5f); + targetPage = (int) ((deltaX > 0) ? + (currentPage + pageOffset - 0.5f) : (currentPage - pageOffset + 0.5f)); + if ((deltaX > 0 && targetPage > currentPage) || (deltaX < 0 && targetPage < currentPage)) + targetPage = currentPage; } if (DEBUG) Log.v(TAG, "targetPage : " + targetPage); return targetPage; diff --git a/library/src/com/slidingmenu/lib/SlidingMenu.java b/library/src/com/slidingmenu/lib/SlidingMenu.java index c45e7f4b4..65a565cc4 100644 --- a/library/src/com/slidingmenu/lib/SlidingMenu.java +++ b/library/src/com/slidingmenu/lib/SlidingMenu.java @@ -121,8 +121,7 @@ public void onPageSelected(int position) { } } }); - initializeLeft(); - initializeRight(); + int mode = 0; // now style everything! TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.SlidingMenu); @@ -133,35 +132,32 @@ public void onPageSelected(int position) { } int viewBehindLeft = ta.getResourceId(R.styleable.SlidingMenu_viewBehindLeft, -1); if (viewBehindLeft != -1) { + mode |= LEFT; setViewBehind(viewBehindLeft, LEFT); } int viewBehindRight = ta.getResourceId(R.styleable.SlidingMenu_viewBehindRight, -1); if (viewBehindRight != -1) { + mode |= RIGHT; setViewBehind(viewBehindRight, RIGHT); } int touchModeAbove = ta.getInt(R.styleable.SlidingMenu_aboveTouchMode, TOUCHMODE_MARGIN); setTouchModeAbove(touchModeAbove); int touchModeBehind = ta.getInt(R.styleable.SlidingMenu_behindTouchMode, TOUCHMODE_MARGIN); setTouchModeBehind(touchModeBehind); - int offsetBehind = (int) ta.getDimension(R.styleable.SlidingMenu_behindOffset, -1); int widthBehind = (int) ta.getDimension(R.styleable.SlidingMenu_behindWidth, -1); - if (offsetBehind != -1 && widthBehind != -1) + if (offsetBehind != -1 && widthBehind != -1) { throw new IllegalStateException("Cannot set both behindOffset and behindWidth for a SlidingMenu"); - else if (offsetBehind != -1) { - setBehindOffset(offsetBehind, BOTH); - } - else if (widthBehind != -1) { - setBehindWidth(widthBehind, BOTH); - } - else { - setBehindOffset(0, BOTH); + } else if (offsetBehind != -1) { + setBehindOffset(offsetBehind, mode); + } else if (widthBehind != -1) { + setBehindWidth(widthBehind, mode); } float scrollOffsetBehind = ta.getFloat(R.styleable.SlidingMenu_behindScrollScale, 0.33f); - setBehindScrollScale(scrollOffsetBehind, BOTH); + setBehindScrollScale(scrollOffsetBehind, mode); int shadowRes = ta.getResourceId(R.styleable.SlidingMenu_shadowDrawable, -1); if (shadowRes != -1) { - setShadowDrawable(shadowRes, BOTH); + setShadowDrawable(shadowRes, mode); } int shadowWidth = (int) ta.getDimension(R.styleable.SlidingMenu_shadowWidth, 0); setShadowWidth(shadowWidth); @@ -225,6 +221,7 @@ public void setViewBehind(int res, int side) { public void setViewBehind(View v, int side) { checkSide(side); if (side == LEFT) { + initializeLeft(); if (v == null) { removeViewBehind(LEFT); } else { @@ -233,6 +230,7 @@ public void setViewBehind(View v, int side) { mViewBehindLeft.invalidate(); } } else { + initializeRight(); if (v == null) { removeViewBehind(RIGHT); } else { @@ -386,7 +384,6 @@ public float getBehindScrollScale(int side) { * 1 pixel that the above view scrolls and 0.0f scrolls 0 pixels) */ public void setBehindScrollScale(float f, int side) { - checkSide(side); mViewAbove.setScrollScale(f, side); } From d0d17c5da8d3aa6dbafcf2e632ec28d11effefe8 Mon Sep 17 00:00:00 2001 From: jfeinstein10 Date: Mon, 24 Sep 2012 19:48:15 -0400 Subject: [PATCH 12/17] Fixed scrollTo issues for right view --- library/src/com/slidingmenu/lib/CustomViewAbove.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/library/src/com/slidingmenu/lib/CustomViewAbove.java b/library/src/com/slidingmenu/lib/CustomViewAbove.java index cf17c7600..98c0e2a9e 100644 --- a/library/src/com/slidingmenu/lib/CustomViewAbove.java +++ b/library/src/com/slidingmenu/lib/CustomViewAbove.java @@ -1025,7 +1025,8 @@ public void scrollTo(int x, int y) { mViewBehindLeft.scrollTo((int)(x*mScrollScaleLeft), y); } if (mViewBehindRight != null && mEnabled) { - int x2 = (int) ((x - mContent.getRight()) * mScrollScaleRight); + int x2 = (int) ((x - mContent.getLeft() - mViewBehindRight.getWidth()) * mScrollScaleRight); + Log.v(TAG, "scrolling right to " + x2); mViewBehindRight.scrollTo(x2, y); } From 20ec5841cb7280833c731b889d029647367d2458 Mon Sep 17 00:00:00 2001 From: Kasra Rahjerdi Date: Thu, 27 Sep 2012 15:42:08 -0400 Subject: [PATCH 13/17] Add flag to onOpened for passing which side was just opened. --- library/src/com/slidingmenu/lib/CustomViewAbove.java | 11 +++++++++-- library/src/com/slidingmenu/lib/SlidingMenu.java | 2 +- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/library/src/com/slidingmenu/lib/CustomViewAbove.java b/library/src/com/slidingmenu/lib/CustomViewAbove.java index 98c0e2a9e..75af350e4 100644 --- a/library/src/com/slidingmenu/lib/CustomViewAbove.java +++ b/library/src/com/slidingmenu/lib/CustomViewAbove.java @@ -530,8 +530,12 @@ void smoothScrollTo(int x, int y, int velocity) { completeScroll(); if (isLeftOpen()) { if (mOpenedListener != null) - mOpenedListener.onOpened(); + mOpenedListener.onOpened(SlidingMenu.LEFT); + } else if (isRightOpen()) { + if (mOpenedListener != null) + mOpenedListener.onOpened(SlidingMenu.RIGHT); } else { + if (mClosedListener != null) mClosedListener.onClosed(); } @@ -697,7 +701,10 @@ private void completeScroll() { } if (isLeftOpen()) { if (mOpenedListener != null) - mOpenedListener.onOpened(); + mOpenedListener.onOpened(SlidingMenu.LEFT); + } else if (isRightOpen()) { + if (mOpenedListener != null) + mOpenedListener.onOpened(SlidingMenu.RIGHT); } else { if (mClosedListener != null) mClosedListener.onClosed(); diff --git a/library/src/com/slidingmenu/lib/SlidingMenu.java b/library/src/com/slidingmenu/lib/SlidingMenu.java index 65a565cc4..4ee6cf382 100644 --- a/library/src/com/slidingmenu/lib/SlidingMenu.java +++ b/library/src/com/slidingmenu/lib/SlidingMenu.java @@ -77,7 +77,7 @@ public interface OnOpenListener { } public interface OnOpenedListener { - public void onOpened(); + public void onOpened(int side); } public interface OnCloseListener { From 2d72045bd6f2a5cc8cf13115dbf326b5ac415058 Mon Sep 17 00:00:00 2001 From: mattkranzler5 Date: Thu, 27 Sep 2012 14:44:31 -0700 Subject: [PATCH 14/17] Fixed bug where it wasn't allowing touches on the right behind view. Fixed issue where it was crashing due to the touch mode variables in SlidingMenu not matching the attributes. Fixed the saving of which side is open when rotating/coming back from saved state. Fixed bug where it wasn't calling onOpen() in the OnOpenListener for the right side. --- .../com/slidingmenu/lib/CustomViewAbove.java | 4 +- .../src/com/slidingmenu/lib/SlidingMenu.java | 54 ++++++++++--------- 2 files changed, 30 insertions(+), 28 deletions(-) diff --git a/library/src/com/slidingmenu/lib/CustomViewAbove.java b/library/src/com/slidingmenu/lib/CustomViewAbove.java index 98c0e2a9e..cd3b0e3c4 100644 --- a/library/src/com/slidingmenu/lib/CustomViewAbove.java +++ b/library/src/com/slidingmenu/lib/CustomViewAbove.java @@ -204,10 +204,10 @@ public void onPageSelected(int position) { if (mViewBehindRight != null) { switch (position) { case 1: - mViewBehindRight.setChildrenEnabled(true); + mViewBehindRight.setChildrenEnabled(false); break; case 2: - mViewBehindRight.setChildrenEnabled(false); + mViewBehindRight.setChildrenEnabled(true); break; } } diff --git a/library/src/com/slidingmenu/lib/SlidingMenu.java b/library/src/com/slidingmenu/lib/SlidingMenu.java index 65a565cc4..59c2c9b10 100644 --- a/library/src/com/slidingmenu/lib/SlidingMenu.java +++ b/library/src/com/slidingmenu/lib/SlidingMenu.java @@ -30,8 +30,8 @@ public class SlidingMenu extends RelativeLayout { public static final String TAG = "SlidingMenu"; - public static final int TOUCHMODE_MARGIN = 3; - public static final int TOUCHMODE_FULLSCREEN = 4; + public static final int TOUCHMODE_MARGIN = 0; + public static final int TOUCHMODE_FULLSCREEN = 1; public static final int LEFT = 0x0100; public static final int RIGHT = 0x1000; @@ -44,6 +44,7 @@ public class SlidingMenu extends RelativeLayout { private OnCloseListener mCloseListener; private boolean mSlidingEnabled; + private int mMode; public static void attachSlidingMenu(Activity activity, SlidingMenu sm, boolean slidingTitle) { if (sm.getParent() != null) @@ -107,21 +108,21 @@ public SlidingMenu(Context context, AttributeSet attrs, int defStyle) { mViewAbove = new CustomViewAbove(context); addView(mViewAbove, aboveParams); mViewAbove.setOnPageChangeListener(new OnPageChangeListener() { - public static final int POSITION_OPEN = 0; + public static final int POSITION_LEFT_OPEN = 0; public static final int POSITION_CLOSE = 1; + public static final int POSITION_RIGHT_OPEN = 2; public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } public void onPageSelected(int position) { - if (position == POSITION_OPEN && mOpenListener != null) { + if (position == POSITION_LEFT_OPEN || position == POSITION_RIGHT_OPEN && mOpenListener != null) { mOpenListener.onOpen(); } else if (position == POSITION_CLOSE && mCloseListener != null) { mCloseListener.onClose(); } } }); - int mode = 0; // now style everything! TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.SlidingMenu); @@ -132,12 +133,12 @@ public void onPageSelected(int position) { } int viewBehindLeft = ta.getResourceId(R.styleable.SlidingMenu_viewBehindLeft, -1); if (viewBehindLeft != -1) { - mode |= LEFT; + mMode |= LEFT; setViewBehind(viewBehindLeft, LEFT); } int viewBehindRight = ta.getResourceId(R.styleable.SlidingMenu_viewBehindRight, -1); if (viewBehindRight != -1) { - mode |= RIGHT; + mMode |= RIGHT; setViewBehind(viewBehindRight, RIGHT); } int touchModeAbove = ta.getInt(R.styleable.SlidingMenu_aboveTouchMode, TOUCHMODE_MARGIN); @@ -149,15 +150,15 @@ public void onPageSelected(int position) { if (offsetBehind != -1 && widthBehind != -1) { throw new IllegalStateException("Cannot set both behindOffset and behindWidth for a SlidingMenu"); } else if (offsetBehind != -1) { - setBehindOffset(offsetBehind, mode); + setBehindOffset(offsetBehind, mMode); } else if (widthBehind != -1) { - setBehindWidth(widthBehind, mode); + setBehindWidth(widthBehind, mMode); } float scrollOffsetBehind = ta.getFloat(R.styleable.SlidingMenu_behindScrollScale, 0.33f); - setBehindScrollScale(scrollOffsetBehind, mode); + setBehindScrollScale(scrollOffsetBehind, mMode); int shadowRes = ta.getResourceId(R.styleable.SlidingMenu_shadowDrawable, -1); if (shadowRes != -1) { - setShadowDrawable(shadowRes, mode); + setShadowDrawable(shadowRes, mMode); } int shadowWidth = (int) ta.getDimension(R.styleable.SlidingMenu_shadowWidth, 0); setShadowWidth(shadowWidth); @@ -282,7 +283,7 @@ public void showBehind(int side) { throw new IllegalArgumentException("Can't show both left and right"); if (isLeft(side)) { - mViewAbove.setCurrentItem(0); + mViewAbove.setCurrentItem(0); } else if (isRight(side)) { mViewAbove.setCurrentItem(2); } @@ -296,7 +297,7 @@ public void showAbove() { } /** - * + * * @return Whether or not the behind view is showing */ public boolean isBehindShowing() { @@ -304,7 +305,7 @@ public boolean isBehindShowing() { } /** - * + * * @return The margin on the right of the screen that the behind view scrolls to */ public int getBehindOffset(int side) { @@ -317,7 +318,7 @@ public int getBehindOffset(int side) { } /** - * + * * @param i The margin on the right of the screen that the behind view scrolls to */ public void setBehindOffset(int i, int side) { @@ -329,7 +330,7 @@ public void setBehindOffset(int i, int side) { int top = params.topMargin; int left = params.leftMargin; params.setMargins(left, top, i, bottom); - } + } if (isRight(side)) { // behind right RelativeLayout.LayoutParams params = ((RelativeLayout.LayoutParams)mViewBehindRight.getLayoutParams()); @@ -344,7 +345,7 @@ public void setBehindOffset(int i, int side) { } /** - * + * * @param res The dimension resource to be set as the behind offset */ public void setBehindOffsetRes(int res, int side) { @@ -371,7 +372,7 @@ public void setBehindWidth(int i, int side) { } /** - * + * * @return The scale of the parallax scroll */ public float getBehindScrollScale(int side) { @@ -379,7 +380,7 @@ public float getBehindScrollScale(int side) { } /** - * + * * @param f The scale of the parallax scroll (i.e. 1.0f scrolls 1 pixel for every * 1 pixel that the above view scrolls and 0.0f scrolls 0 pixels) */ @@ -391,7 +392,7 @@ public void setBehindCanvasTransformer(CanvasTransformer t, int side) { checkSideStrict(side); if (isLeft(side)) { mViewBehindLeft.setCanvasTransformer(t); - } + } if (isRight(side)) { mViewBehindRight.setCanvasTransformer(t); } @@ -477,6 +478,7 @@ public void setOnClosedListener(OnClosedListener listener) { private static class SavedState extends BaseSavedState { boolean mBehindShowing; + int mMode; public SavedState(Parcelable superState) { super(superState); @@ -484,7 +486,8 @@ public SavedState(Parcelable superState) { public void writeToParcel(Parcel out, int flags) { super.writeToParcel(out, flags); - out.writeBooleanArray(new boolean[]{mBehindShowing}); + out.writeValue(mBehindShowing); + out.writeInt(mMode); } public static final Parcelable.Creator CREATOR @@ -501,9 +504,8 @@ public SavedState[] newArray(int size) { SavedState(Parcel in) { super(in); - boolean[] showing = new boolean[1]; - in.readBooleanArray(showing); - mBehindShowing = showing[0]; + mBehindShowing = (Boolean) in.readValue(null); + mMode = in.readInt(); } } @@ -512,6 +514,7 @@ protected Parcelable onSaveInstanceState() { Parcelable superState = super.onSaveInstanceState(); SavedState ss = new SavedState(superState); ss.mBehindShowing = isBehindShowing(); + ss.mMode = mMode; return ss; } @@ -526,8 +529,7 @@ protected void onRestoreInstanceState(Parcelable state) { super.onRestoreInstanceState(ss.getSuperState()); if (ss.mBehindShowing) { - // TODO fix this - showBehind(LEFT); + showBehind(ss.mMode); } else { showAbove(); } From bcbd1f10057a6a5c80035d191b849e2321f0fd2d Mon Sep 17 00:00:00 2001 From: jfeinstein10 Date: Fri, 28 Sep 2012 18:00:32 -0400 Subject: [PATCH 15/17] Parentheses wrong in one of the pull requests --- library/src/com/slidingmenu/lib/SlidingMenu.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/src/com/slidingmenu/lib/SlidingMenu.java b/library/src/com/slidingmenu/lib/SlidingMenu.java index 9e69df92b..d4f91cb6c 100644 --- a/library/src/com/slidingmenu/lib/SlidingMenu.java +++ b/library/src/com/slidingmenu/lib/SlidingMenu.java @@ -116,7 +116,7 @@ public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } public void onPageSelected(int position) { - if (position == POSITION_LEFT_OPEN || position == POSITION_RIGHT_OPEN && mOpenListener != null) { + if ((position == POSITION_LEFT_OPEN || position == POSITION_RIGHT_OPEN) && mOpenListener != null) { mOpenListener.onOpen(); } else if (position == POSITION_CLOSE && mCloseListener != null) { mCloseListener.onClose(); From a4a57746f97b06eb91d4a8ebcba517a2a8527ee5 Mon Sep 17 00:00:00 2001 From: jfeinstein10 Date: Wed, 17 Oct 2012 11:53:16 -0400 Subject: [PATCH 16/17] Fixed directional sliding. --- example/res/drawable/shadow_right.xml | 9 +++ example/res/layout/test.xml | 6 +- .../com/slidingmenu/example/BaseActivity.java | 59 ++++++++++++++----- .../slidingmenu/example/CustomAnimation.java | 4 +- .../slidingmenu/example/SlidingTitleBar.java | 19 ++++-- .../com/slidingmenu/lib/CustomViewAbove.java | 28 ++++++--- 6 files changed, 89 insertions(+), 36 deletions(-) create mode 100644 example/res/drawable/shadow_right.xml diff --git a/example/res/drawable/shadow_right.xml b/example/res/drawable/shadow_right.xml new file mode 100644 index 000000000..df27938c1 --- /dev/null +++ b/example/res/drawable/shadow_right.xml @@ -0,0 +1,9 @@ + + + + + + \ No newline at end of file diff --git a/example/res/layout/test.xml b/example/res/layout/test.xml index 50bf5f76d..33281374a 100644 --- a/example/res/layout/test.xml +++ b/example/res/layout/test.xml @@ -3,13 +3,9 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > - - = 11) { @@ -52,6 +45,40 @@ public void onCreate(Bundle savedInstanceState) { } } + private void addLeft() { + FrameLayout left = new FrameLayout(this); + left.setId("LEFT".hashCode()); + setBehindLeftContentView(left); + getSupportFragmentManager() + .beginTransaction() + .replace("LEFT".hashCode(), new SampleListFragment()) + .commit(); + + SlidingMenu sm = getSlidingMenu(); + sm.setShadowDrawable(R.drawable.shadow, SlidingMenu.LEFT); + sm.setBehindOffsetRes(R.dimen.actionbar_home_width, SlidingMenu.LEFT); + } + + private void addRight() { + FrameLayout right = new FrameLayout(this); + right.setId("RIGHT".hashCode()); + this.setBehindRightContentView(right); + getSupportFragmentManager() + .beginTransaction() + .replace("RIGHT".hashCode(), new SampleListFragment()) + .commit(); + + SlidingMenu sm = getSlidingMenu(); + sm.setShadowDrawable(R.drawable.shadow_right, SlidingMenu.RIGHT); + sm.setBehindOffsetRes(R.dimen.actionbar_home_width, SlidingMenu.RIGHT); + } + +// @Override +// public void onResume() { +// super.onResume(); +// getSlidingMenu().showAbove(); +// } + @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { @@ -61,7 +88,7 @@ public boolean onOptionsItemSelected(MenuItem item) { } return super.onOptionsItemSelected(item); } - + public class PagerAdapter extends FragmentPagerAdapter { private List mFragments = new ArrayList(); private ViewPager mPager; @@ -70,6 +97,8 @@ public PagerAdapter(FragmentManager fm, ViewPager vp) { super(fm); mPager = vp; mPager.setAdapter(this); + for (int i = 0; i < 3; i++) + addTab(new SampleListFragment()); } public void addTab(Fragment frag) { @@ -86,5 +115,5 @@ public int getCount() { return mFragments.size(); } } - + } diff --git a/example/src/com/slidingmenu/example/CustomAnimation.java b/example/src/com/slidingmenu/example/CustomAnimation.java index 5dd7d547d..31eac6dba 100644 --- a/example/src/com/slidingmenu/example/CustomAnimation.java +++ b/example/src/com/slidingmenu/example/CustomAnimation.java @@ -27,8 +27,8 @@ public void onCreate(Bundle savedInstanceState) { SlidingMenu sm = getSlidingMenu(); setSlidingActionBarEnabled(true); - sm.setBehindScrollScale(0.0f, SlidingMenu.LEFT | SlidingMenu.RIGHT); - sm.setBehindCanvasTransformer(mTransformer, SlidingMenu.LEFT | SlidingMenu.RIGHT); + sm.setBehindScrollScale(0.0f, SlidingMenu.BOTH); + sm.setBehindCanvasTransformer(mTransformer, SlidingMenu.BOTH); } @Override diff --git a/example/src/com/slidingmenu/example/SlidingTitleBar.java b/example/src/com/slidingmenu/example/SlidingTitleBar.java index b781a1ba5..f66600120 100644 --- a/example/src/com/slidingmenu/example/SlidingTitleBar.java +++ b/example/src/com/slidingmenu/example/SlidingTitleBar.java @@ -1,6 +1,9 @@ package com.slidingmenu.example; +import com.slidingmenu.lib.SlidingMenu; + import android.os.Bundle; +import android.support.v4.view.ViewPager; public class SlidingTitleBar extends BaseActivity { @@ -14,11 +17,17 @@ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // set the Above View - setContentView(R.layout.content_frame); - getSupportFragmentManager() - .beginTransaction() - .replace(R.id.content_frame, new SampleListFragment()) - .commit(); +// setContentView(R.layout.content_frame); +// getSupportFragmentManager() +// .beginTransaction() +// .replace(R.id.content_frame, new SampleListFragment()) +// .commit(); + + ViewPager vp = new ViewPager(this); + vp.setId("VP".hashCode()); + vp.setAdapter(new PagerAdapter(getSupportFragmentManager(), vp)); + setContentView(vp); + getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN); setSlidingActionBarEnabled(true); } diff --git a/library/src/com/slidingmenu/lib/CustomViewAbove.java b/library/src/com/slidingmenu/lib/CustomViewAbove.java index f33de3a2f..aaa293f9b 100644 --- a/library/src/com/slidingmenu/lib/CustomViewAbove.java +++ b/library/src/com/slidingmenu/lib/CustomViewAbove.java @@ -5,8 +5,6 @@ import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; -import android.graphics.Rect; -import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.os.Build; import android.support.v4.view.KeyEventCompat; @@ -97,7 +95,7 @@ public float getInterpolation(float t) { private int mFlingDistance; private boolean mLastTouchAllowed = false; - private final int mSlidingMenuThreshold = 10; + private final int mSlidingMenuThreshold = 16; private CustomViewBehind mViewBehindLeft; private CustomViewBehind mViewBehindRight; private boolean mEnabled = true; @@ -772,6 +770,22 @@ private boolean thisTouchAllowed(MotionEvent ev) { } } } + + private boolean thisSlideAllowed(float dx) { + boolean allowed = false; + if (isLeftOpen()) { + allowed = dx < 0; + } else if (isRightOpen()) { + allowed = dx > 0; + } else { + if (mViewBehindLeft != null) + allowed |= dx > 0; + if (mViewBehindRight != null) + allowed |= dx < 0; + } + Log.v(TAG, "this slide allowed " + allowed); + return allowed; + } private boolean mIsUnableToDrag; @@ -814,7 +828,7 @@ else if (mIsUnableToDrag) final float xDiff = Math.abs(dx); final float y = MotionEventCompat.getY(ev, pointerIndex); final float yDiff = Math.abs(y - mLastMotionY); - if (xDiff > mTouchSlop && xDiff > yDiff) { + if (xDiff > mTouchSlop && xDiff > yDiff && thisSlideAllowed(dx)) { if (DEBUG) Log.v(TAG, "Starting drag! from onInterceptTouch"); mIsBeingDragged = true; mLastMotionX = x; @@ -825,8 +839,7 @@ else if (mIsUnableToDrag) break; case MotionEvent.ACTION_DOWN: - mActivePointerId = ev.getAction() & ((Build.VERSION.SDK_INT >= 8) ? MotionEvent.ACTION_POINTER_INDEX_MASK : - MotionEvent.ACTION_POINTER_ID_MASK); + mActivePointerId = ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK; mLastMotionX = mInitialMotionX = MotionEventCompat.getX(ev, mActivePointerId); mLastMotionY = MotionEventCompat.getY(ev, mActivePointerId); if (thisTouchAllowed(ev)) { @@ -936,7 +949,6 @@ public boolean onTouchEvent(MotionEvent ev) { velocityTracker, mActivePointerId); final int widthWithMargin = getChildWidth(mCurItem); final int scrollX = getScrollX(); - final int currentPage = scrollX / widthWithMargin; final float pageOffset = (float) (scrollX % widthWithMargin) / widthWithMargin; final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId); @@ -1044,7 +1056,6 @@ public void scrollTo(int x, int y) { private int determineTargetPage(int currentPage, float pageOffset, int velocity, int deltaX) { int targetPage; if (Math.abs(deltaX) > (float)mFlingDistance && Math.abs(velocity) > mMinimumVelocity) { - Log.v(TAG, "in here!"); targetPage = velocity > 0 ? currentPage - 1 : currentPage + 1; } else { targetPage = (int) ((deltaX > 0) ? @@ -1052,7 +1063,6 @@ private int determineTargetPage(int currentPage, float pageOffset, int velocity, if ((deltaX > 0 && targetPage > currentPage) || (deltaX < 0 && targetPage < currentPage)) targetPage = currentPage; } - if (DEBUG) Log.v(TAG, "targetPage : " + targetPage); return targetPage; } From e08636b53eb43daa504250cf1af07338a0174472 Mon Sep 17 00:00:00 2001 From: jfeinstein10 Date: Wed, 24 Oct 2012 16:13:47 -0400 Subject: [PATCH 17/17] Fixing naming conventions --- library/res/values/attrs.xml | 4 ++-- library/src/com/slidingmenu/lib/SlidingMenu.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/library/res/values/attrs.xml b/library/res/values/attrs.xml index 6602ca305..d65a6c56f 100644 --- a/library/res/values/attrs.xml +++ b/library/res/values/attrs.xml @@ -28,11 +28,11 @@ - + - + diff --git a/library/src/com/slidingmenu/lib/SlidingMenu.java b/library/src/com/slidingmenu/lib/SlidingMenu.java index d4f91cb6c..e7cd4fc93 100644 --- a/library/src/com/slidingmenu/lib/SlidingMenu.java +++ b/library/src/com/slidingmenu/lib/SlidingMenu.java @@ -141,9 +141,9 @@ public void onPageSelected(int position) { mMode |= RIGHT; setViewBehind(viewBehindRight, RIGHT); } - int touchModeAbove = ta.getInt(R.styleable.SlidingMenu_aboveTouchMode, TOUCHMODE_MARGIN); + int touchModeAbove = ta.getInt(R.styleable.SlidingMenu_touchModeAbove, TOUCHMODE_MARGIN); setTouchModeAbove(touchModeAbove); - int touchModeBehind = ta.getInt(R.styleable.SlidingMenu_behindTouchMode, TOUCHMODE_MARGIN); + int touchModeBehind = ta.getInt(R.styleable.SlidingMenu_touchModeBehind, TOUCHMODE_MARGIN); setTouchModeBehind(touchModeBehind); int offsetBehind = (int) ta.getDimension(R.styleable.SlidingMenu_behindOffset, -1); int widthBehind = (int) ta.getDimension(R.styleable.SlidingMenu_behindWidth, -1);