From 71ef2ad7e854d3f7a88c0d84b9cf7b36ac8728f9 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 22 Dec 2014 17:48:51 +0530 Subject: [PATCH 01/16] Enabling both side swipe --- .../com/daimajia/swipedemo/MyActivity.java | 25 +- demo/src/main/res/layout/listview_item.xml | 3 +- demo/src/main/res/layout/sample1.xml | 31 + .../java/com/daimajia/swipe/SwipeLayout.java | 1476 ++++++++++------- library/src/main/res/values/attrs.xml | 16 +- 5 files changed, 936 insertions(+), 615 deletions(-) diff --git a/demo/src/main/java/com/daimajia/swipedemo/MyActivity.java b/demo/src/main/java/com/daimajia/swipedemo/MyActivity.java index 18fb36f7..75483437 100644 --- a/demo/src/main/java/com/daimajia/swipedemo/MyActivity.java +++ b/demo/src/main/java/com/daimajia/swipedemo/MyActivity.java @@ -28,7 +28,8 @@ protected void onCreate(Bundle savedInstanceState) { sample1 = (SwipeLayout) findViewById(R.id.sample1); sample1.setShowMode(SwipeLayout.ShowMode.LayDown); - sample1.setDragEdge(SwipeLayout.DragEdge.Left); + sample1.setDragEdges(SwipeLayout.DragEdge.Left, SwipeLayout.DragEdge.Right); + Toast.makeText(this, sample1.getDragEdge() + " is the drag edge", Toast.LENGTH_LONG).show(); sample1.addRevealListener(R.id.delete, new SwipeLayout.OnRevealListener() { @Override public void onReveal(View child, SwipeLayout.DragEdge edge, float fraction, int distance) { @@ -36,10 +37,32 @@ public void onReveal(View child, SwipeLayout.DragEdge edge, float fraction, int } }); + sample1.findViewById(R.id.star2).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Toast.makeText(MyActivity.this, "Star", Toast.LENGTH_SHORT).show(); + } + }); + + sample1.findViewById(R.id.trash2).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Toast.makeText(MyActivity.this, "Trash Bin", Toast.LENGTH_SHORT).show(); + } + }); + + sample1.findViewById(R.id.magnifier2).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Toast.makeText(MyActivity.this, "Magnifier", Toast.LENGTH_SHORT).show(); + } + }); + //sample2 sample2 = (SwipeLayout) findViewById(R.id.sample2); sample2.setShowMode(SwipeLayout.ShowMode.LayDown); + sample2.setDragEdge(SwipeLayout.DragEdge.Right); // sample2.setShowMode(SwipeLayout.ShowMode.PullOut); sample2.findViewById(R.id.star).setOnClickListener(new View.OnClickListener() { @Override diff --git a/demo/src/main/res/layout/listview_item.xml b/demo/src/main/res/layout/listview_item.xml index db12e971..e88954ff 100644 --- a/demo/src/main/res/layout/listview_item.xml +++ b/demo/src/main/res/layout/listview_item.xml @@ -7,7 +7,8 @@ android:id="@+id/swipe" android:layout_width="match_parent" android:layout_height="wrap_content" - swipe:horizontalSwipeOffset="25dp" + swipe:leftEdgeSwipeOffset="25dp" + swipe:rightEdgeSwipeOffset="25dp" > + + + + + + + diff --git a/library/src/main/java/com/daimajia/swipe/SwipeLayout.java b/library/src/main/java/com/daimajia/swipe/SwipeLayout.java index 5d6cdc3d..014d8fa4 100644 --- a/library/src/main/java/com/daimajia/swipe/SwipeLayout.java +++ b/library/src/main/java/com/daimajia/swipe/SwipeLayout.java @@ -6,6 +6,7 @@ import android.support.v4.view.ViewCompat; import android.support.v4.widget.ViewDragHelper; import android.util.AttributeSet; +import android.util.Log; import android.view.GestureDetector; import android.view.MotionEvent; import android.view.View; @@ -22,16 +23,30 @@ import java.util.List; import java.util.Map; -public class SwipeLayout extends FrameLayout { +public class SwipeLayout extends FrameLayout +{ + private static final int DRAG_LEFT = 1; + private static final int DRAG_RIGHT = 2; + private static final int DRAG_TOP = 4; + private static final int DRAG_BOTTOM = 8; + + private int mLeftIndex; + private int mRightIndex; + private int mTopIndex; + private int mBottomIndex; + + private int currentDirectionIndex = 0; private ViewDragHelper mDragHelper; private int mDragDistance = 0; - private DragEdge mDragEdge; + private List mDragEdges; private ShowMode mShowMode; - private float mHorizontalSwipeOffset; - private float mVerticalSwipeOffset; + private float mLeftEdgeSwipeOffset; + private float mRightEdgeSwipeOffset; + private float mTopEdgeSwipeOffset; + private float mBottomEdgeSwipeOffset; private List mSwipeListeners = new ArrayList(); private List mSwipeDeniers = new ArrayList(); @@ -42,130 +57,171 @@ public class SwipeLayout extends FrameLayout { private boolean mSwipeEnabled = true; - public static enum DragEdge { + public static enum DragEdge + { Left, Right, Top, Bottom; }; - public static enum ShowMode { - LayDown, PullOut + public static enum ShowMode + { + LayDown, + PullOut } - - public SwipeLayout(Context context) { + public SwipeLayout(Context context) + { this(context, null); } - public SwipeLayout(Context context, AttributeSet attrs) { + public SwipeLayout(Context context, AttributeSet attrs) + { this(context, attrs, 0); } - public SwipeLayout(Context context, AttributeSet attrs, int defStyle) { + public SwipeLayout(Context context, AttributeSet attrs, int defStyle) + { super(context, attrs, defStyle); mDragHelper = ViewDragHelper.create(this, mDragHelperCallback); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SwipeLayout); - int ordinal = a.getInt(R.styleable.SwipeLayout_drag_edge, DragEdge.Right.ordinal()); - mHorizontalSwipeOffset =a.getDimension(R.styleable.SwipeLayout_horizontalSwipeOffset,0); - mVerticalSwipeOffset =a.getDimension(R.styleable.SwipeLayout_verticalSwipeOffset,0); - mDragEdge = DragEdge.values()[ordinal]; - ordinal = a.getInt(R.styleable.SwipeLayout_show_mode, ShowMode.PullOut.ordinal()); + int dragEdgeChoices = a.getInt(R.styleable.SwipeLayout_drag_edge, DragEdge.Right.ordinal()); + mLeftEdgeSwipeOffset = a.getDimension(R.styleable.SwipeLayout_leftEdgeSwipeOffset, 0); + mRightEdgeSwipeOffset = a.getDimension(R.styleable.SwipeLayout_rightEdgeSwipeOffset, 0); + mTopEdgeSwipeOffset = a.getDimension(R.styleable.SwipeLayout_topEdgeSwipeOffset, 0); + mBottomEdgeSwipeOffset = a.getDimension(R.styleable.SwipeLayout_bottomEdgeSwipeOffset, 0); + + mDragEdges = new ArrayList(); + if ((dragEdgeChoices & DRAG_LEFT) == DRAG_LEFT) { + mDragEdges.add(DragEdge.Left); + } + if ((dragEdgeChoices & DRAG_RIGHT) == DRAG_RIGHT) { + mDragEdges.add(DragEdge.Right); + } + if ((dragEdgeChoices & DRAG_TOP) == DRAG_TOP) { + mDragEdges.add(DragEdge.Top); + } + if ((dragEdgeChoices & DRAG_BOTTOM) == DRAG_BOTTOM) { + mDragEdges.add(DragEdge.Bottom); + } + populateIndexes(); + int ordinal = a.getInt(R.styleable.SwipeLayout_show_mode, ShowMode.PullOut.ordinal()); mShowMode = ShowMode.values()[ordinal]; } - - public interface SwipeListener{ + public interface SwipeListener + { public void onStartOpen(SwipeLayout layout); + public void onOpen(SwipeLayout layout); + public void onStartClose(SwipeLayout layout); + public void onClose(SwipeLayout layout); + public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset); + public void onHandRelease(SwipeLayout layout, float xvel, float yvel); } - public void addSwipeListener(SwipeListener l){ + public void addSwipeListener(SwipeListener l) + { mSwipeListeners.add(l); } - public void removeSwipeListener(SwipeListener l){ + public void removeSwipeListener(SwipeListener l) + { mSwipeListeners.remove(l); } - public static interface SwipeDenier { - /* - * Called in onInterceptTouchEvent - * Determines if this swipe event should be denied - * Implement this interface if you are using views with swipe gestures - * As a child of SwipeLayout - * - * @return true deny - * false allow - */ + public static interface SwipeDenier + { + /* + * Called in onInterceptTouchEvent Determines if this swipe event should + * be denied Implement this interface if you are using views with swipe + * gestures As a child of SwipeLayout + * + * @return true deny false allow + */ public boolean shouldDenySwipe(MotionEvent ev); } - public void addSwipeDenier(SwipeDenier denier) { + public void addSwipeDenier(SwipeDenier denier) + { mSwipeDeniers.add(denier); } - public void removeSwipeDenier(SwipeDenier denier) { + public void removeSwipeDenier(SwipeDenier denier) + { mSwipeDeniers.remove(denier); } - public void removeAllSwipeDeniers() { + public void removeAllSwipeDeniers() + { mSwipeDeniers.clear(); } - public interface OnRevealListener { + public interface OnRevealListener + { public void onReveal(View child, DragEdge edge, float fraction, int distance); } /** - * bind a view with a specific {@link com.daimajia.swipe.SwipeLayout.OnRevealListener} - * @param childId the view id. - * @param l the target {@link com.daimajia.swipe.SwipeLayout.OnRevealListener} + * bind a view with a specific + * {@link com.daimajia.swipe.SwipeLayout.OnRevealListener} + * + * @param childId + * the view id. + * @param l + * the target + * {@link com.daimajia.swipe.SwipeLayout.OnRevealListener} */ - public void addRevealListener(int childId, OnRevealListener l){ - View child = findViewById(childId); - if(child == null){ + public void addRevealListener(int childId, OnRevealListener l) + { + View child = findViewById(childId); + if (child == null) { throw new IllegalArgumentException("Child does not belong to SwipeListener."); } - if(!mShowEntirely.containsKey(child)){ + if (!mShowEntirely.containsKey(child)) { mShowEntirely.put(child, false); } - if(mRevealListeners.get(child) == null) - mRevealListeners.put(child, new ArrayList()); + if (mRevealListeners.get(child) == null) mRevealListeners.put(child, new ArrayList()); mRevealListeners.get(child).add(l); } /** - * bind multiple views with an {@link com.daimajia.swipe.SwipeLayout.OnRevealListener}. - * @param childIds the view id. - * @param l the {@link com.daimajia.swipe.SwipeLayout.OnRevealListener} + * bind multiple views with an + * {@link com.daimajia.swipe.SwipeLayout.OnRevealListener}. + * + * @param childIds + * the view id. + * @param l + * the {@link com.daimajia.swipe.SwipeLayout.OnRevealListener} */ - public void addRevealListener(int[] childIds, OnRevealListener l){ - for(int i : childIds) + public void addRevealListener(int[] childIds, OnRevealListener l) + { + for (int i : childIds) addRevealListener(i, l); } - public void removeRevealListener(int childId, OnRevealListener l){ + public void removeRevealListener(int childId, OnRevealListener l) + { View child = findViewById(childId); - if(child == null) - return; + if (child == null) return; mShowEntirely.remove(child); - if(mRevealListeners.containsKey(child)) - mRevealListeners.get(child).remove(l); + if (mRevealListeners.containsKey(child)) mRevealListeners.get(child).remove(l); } - public void removeAllRevealListeners(int childId){ + public void removeAllRevealListeners(int childId) + { View child = findViewById(childId); - if(child != null){ + if (child != null) { mRevealListeners.remove(child); mShowEntirely.remove(child); } @@ -174,127 +230,132 @@ public void removeAllRevealListeners(int childId){ private ViewDragHelper.Callback mDragHelperCallback = new ViewDragHelper.Callback() { @Override - public int clampViewPositionHorizontal(View child, int left, int dx) { - if(child == getSurfaceView()){ - switch (mDragEdge){ - case Top: - case Bottom: - return getPaddingLeft(); - case Left: - if(left < getPaddingLeft()) - return getPaddingLeft(); - if(left > getPaddingLeft() + mDragDistance) - return getPaddingLeft() + mDragDistance; - break; - case Right: - if(left > getPaddingLeft()) - return getPaddingLeft(); - if(left < getPaddingLeft() - mDragDistance) - return getPaddingLeft() - mDragDistance; - break; + public int clampViewPositionHorizontal(View child, int left, int dx) + { + if (child == getSurfaceView()) { + switch (mDragEdges.get(currentDirectionIndex)) + { + case Top: + case Bottom: + return getPaddingLeft(); + case Left: + if (left < getPaddingLeft()) return getPaddingLeft(); + if (left > getPaddingLeft() + mDragDistance) return getPaddingLeft() + mDragDistance; + break; + case Right: + if (left > getPaddingLeft()) return getPaddingLeft(); + if (left < getPaddingLeft() - mDragDistance) return getPaddingLeft() - mDragDistance; + break; } - }else if(child == getBottomView()){ + } + else if (getBottomViews().get(currentDirectionIndex) == child) { - switch (mDragEdge){ - case Top: - case Bottom: - return getPaddingLeft(); - case Left: - if(mShowMode == ShowMode.PullOut){ - if(left > getPaddingLeft()) - return getPaddingLeft(); - } - break; - case Right: - if(mShowMode == ShowMode.PullOut){ - if(left < getMeasuredWidth() - mDragDistance){ - return getMeasuredWidth() - mDragDistance; - } + switch (mDragEdges.get(currentDirectionIndex)) + { + case Top: + case Bottom: + return getPaddingLeft(); + case Left: + if (mShowMode == ShowMode.PullOut) { + if (left > getPaddingLeft()) return getPaddingLeft(); + } + break; + case Right: + if (mShowMode == ShowMode.PullOut) { + if (left < getMeasuredWidth() - mDragDistance) { + return getMeasuredWidth() - mDragDistance; } - break; + } + break; } } return left; } @Override - public int clampViewPositionVertical(View child, int top, int dy) { - if(child == getSurfaceView()){ - switch (mDragEdge){ - case Left: - case Right: + public int clampViewPositionVertical(View child, int top, int dy) + { + if (child == getSurfaceView()) { + switch (mDragEdges.get(currentDirectionIndex)) + { + case Left: + case Right: + return getPaddingTop(); + case Top: + if (top < getPaddingTop()) return getPaddingTop(); + if (top > getPaddingTop() + mDragDistance) return getPaddingTop() + mDragDistance; + break; + case Bottom: + if (top < getPaddingTop() - mDragDistance) { + return getPaddingTop() - mDragDistance; + } + if (top > getPaddingTop()) { return getPaddingTop(); - case Top: - if(top < getPaddingTop()) - return getPaddingTop(); - if(top > getPaddingTop() + mDragDistance) + } + } + } + else { + switch (mDragEdges.get(currentDirectionIndex)) + { + case Left: + case Right: + return getPaddingTop(); + case Top: + if (mShowMode == ShowMode.PullOut) { + if (top > getPaddingTop()) return getPaddingTop(); + } + else { + if (getSurfaceView().getTop() + dy < getPaddingTop()) return getPaddingTop(); + if (getSurfaceView().getTop() + dy > getPaddingTop() + mDragDistance) return getPaddingTop() + mDragDistance; - break; - case Bottom: - if(top < getPaddingTop() - mDragDistance){ + } + break; + case Bottom: + if (mShowMode == ShowMode.PullOut) { + if (top < getMeasuredHeight() - mDragDistance) return getMeasuredHeight() - mDragDistance; + } + else { + if (getSurfaceView().getTop() + dy >= getPaddingTop()) return getPaddingTop(); + if (getSurfaceView().getTop() + dy <= getPaddingTop() - mDragDistance) return getPaddingTop() - mDragDistance; - } - if(top > getPaddingTop()){ - return getPaddingTop(); - } - } - }else{ - switch (mDragEdge){ - case Left: - case Right: - return getPaddingTop(); - case Top: - if(mShowMode == ShowMode.PullOut){ - if(top > getPaddingTop()) - return getPaddingTop(); - }else{ - if(getSurfaceView().getTop() + dy < getPaddingTop()) - return getPaddingTop(); - if(getSurfaceView().getTop() + dy > getPaddingTop() + mDragDistance) - return getPaddingTop() + mDragDistance; - } - break; - case Bottom: - if(mShowMode == ShowMode.PullOut){ - if(top < getMeasuredHeight() - mDragDistance) - return getMeasuredHeight() - mDragDistance; - }else{ - if(getSurfaceView().getTop() + dy >= getPaddingTop()) - return getPaddingTop(); - if(getSurfaceView().getTop() + dy <= getPaddingTop() - mDragDistance) - return getPaddingTop() - mDragDistance; - } + } } } return top; } @Override - public boolean tryCaptureView(View child, int pointerId) { - return child == getSurfaceView() || child == getBottomView(); + public boolean tryCaptureView(View child, int pointerId) + { + return child == getSurfaceView() || getBottomViews().contains(child); } @Override - public int getViewHorizontalDragRange(View child) { + public int getViewHorizontalDragRange(View child) + { return mDragDistance; } @Override - public int getViewVerticalDragRange(View child) { + public int getViewVerticalDragRange(View child) + { return mDragDistance; } @Override - public void onViewReleased(View releasedChild, float xvel, float yvel) { + public void onViewReleased(View releasedChild, float xvel, float yvel) + { super.onViewReleased(releasedChild, xvel, yvel); - for(SwipeListener l : mSwipeListeners) + for (SwipeListener l : mSwipeListeners) l.onHandRelease(SwipeLayout.this, xvel, yvel); - if(releasedChild == getSurfaceView()){ + if (releasedChild == getSurfaceView()) { processSurfaceRelease(xvel, yvel); - }else if(releasedChild == getBottomView()){ - if(getShowMode() == ShowMode.PullOut){ + } + else if (getBottomViews().contains(releasedChild)) { + if (getShowMode() == ShowMode.PullOut) { processBottomPullOutRelease(xvel, yvel); - }else if(getShowMode() == ShowMode.LayDown){ + } + else if (getShowMode() == ShowMode.LayDown) { processBottomLayDownMode(xvel, yvel); } } @@ -303,35 +364,43 @@ public void onViewReleased(View releasedChild, float xvel, float yvel) { } @Override - public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) { - int evLeft = getSurfaceView().getLeft(), evRight = getSurfaceView().getRight(), - evTop = getSurfaceView().getTop(),evBottom = getSurfaceView().getBottom(); - if(changedView == getSurfaceView()){ - - if(mShowMode == ShowMode.PullOut){ - if(mDragEdge == DragEdge.Left || mDragEdge == DragEdge.Right) - getBottomView().offsetLeftAndRight(dx); - else - getBottomView().offsetTopAndBottom(dy); + public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) + { + int evLeft = getSurfaceView().getLeft(), evRight = getSurfaceView().getRight(), evTop = getSurfaceView() + .getTop(), evBottom = getSurfaceView().getBottom(); + if (changedView == getSurfaceView()) { + + if (mShowMode == ShowMode.PullOut) { + if (mDragEdges.get(currentDirectionIndex) == DragEdge.Left + || mDragEdges.get(currentDirectionIndex) == DragEdge.Right) + getBottomViews().get(currentDirectionIndex).offsetLeftAndRight(dx); + else getBottomViews().get(currentDirectionIndex).offsetTopAndBottom(dy); } - }else if(changedView == getBottomView()){ + } + else if (getBottomViews().contains(changedView)) { - if(mShowMode == ShowMode.PullOut){ + if (mShowMode == ShowMode.PullOut) { getSurfaceView().offsetLeftAndRight(dx); getSurfaceView().offsetTopAndBottom(dy); - }else{ - Rect rect = computeBottomLayDown(mDragEdge); - getBottomView().layout(rect.left, rect.top, rect.right, rect.bottom); + } + else { + Rect rect = computeBottomLayDown(mDragEdges.get(currentDirectionIndex)); + getBottomViews().get(currentDirectionIndex).layout(rect.left, rect.top, rect.right, rect.bottom); int newLeft = getSurfaceView().getLeft() + dx, newTop = getSurfaceView().getTop() + dy; - if(mDragEdge == DragEdge.Left && newLeft < getPaddingLeft()) newLeft = getPaddingLeft(); - else if(mDragEdge == DragEdge.Right && newLeft > getPaddingLeft()) newLeft = getPaddingLeft(); - else if(mDragEdge == DragEdge.Top && newTop < getPaddingTop()) newTop = getPaddingTop(); - else if(mDragEdge == DragEdge.Bottom && newTop > getPaddingTop()) newTop = getPaddingTop(); - - getSurfaceView().layout(newLeft, newTop, newLeft + getMeasuredWidth(), newTop + getMeasuredHeight()); + if (mDragEdges.get(currentDirectionIndex) == DragEdge.Left && newLeft < getPaddingLeft()) + newLeft = getPaddingLeft(); + else if (mDragEdges.get(currentDirectionIndex) == DragEdge.Right && newLeft > getPaddingLeft()) + newLeft = getPaddingLeft(); + else if (mDragEdges.get(currentDirectionIndex) == DragEdge.Top && newTop < getPaddingTop()) + newTop = getPaddingTop(); + else if (mDragEdges.get(currentDirectionIndex) == DragEdge.Bottom && newTop > getPaddingTop()) + newTop = getPaddingTop(); + + getSurfaceView() + .layout(newLeft, newTop, newLeft + getMeasuredWidth(), newTop + getMeasuredHeight()); } } @@ -344,8 +413,10 @@ public void onViewPositionChanged(View changedView, int left, int top, int dx, i }; /** - * the dispatchRevealEvent method may not always get accurate position, it makes the view may not always get the event when the view is - * totally show( fraction = 1), so , we need to calculate every time. + * the dispatchRevealEvent method may not always get accurate position, it + * makes the view may not always get the event when the view is totally + * show( fraction = 1), so , we need to calculate every time. + * * @param child * @param relativePosition * @param edge @@ -355,91 +426,89 @@ public void onViewPositionChanged(View changedView, int left, int top, int dx, i * @param surfaceBottom * @return */ - protected boolean isViewTotallyFirstShowed(View child, Rect relativePosition, DragEdge edge, int surfaceLeft, int surfaceTop, int surfaceRight, int surfaceBottom){ - if(mShowEntirely.get(child)) - return false; + protected boolean isViewTotallyFirstShowed(View child, Rect relativePosition, DragEdge edge, int surfaceLeft, + int surfaceTop, int surfaceRight, int surfaceBottom) + { + if (mShowEntirely.get(child)) return false; int childLeft = relativePosition.left; int childRight = relativePosition.right; int childTop = relativePosition.top; int childBottom = relativePosition.bottom; boolean r = false; - if(getShowMode() == ShowMode.LayDown) { - if((edge == DragEdge.Right && surfaceRight <= childLeft) - || (edge == DragEdge.Left && surfaceLeft >= childRight) - || (edge == DragEdge.Top && surfaceTop >= childBottom) - || (edge == DragEdge.Bottom && surfaceBottom <= childTop)) - r = true; - } - else if(getShowMode() == ShowMode.PullOut){ - if((edge == DragEdge.Right && childRight <= getWidth()) - || (edge == DragEdge.Left && childLeft >= getPaddingLeft()) - || (edge == DragEdge.Top && childTop >= getPaddingTop()) - || (edge == DragEdge.Bottom && childBottom <= getHeight())) - r = true; + if (getShowMode() == ShowMode.LayDown) { + if ((edge == DragEdge.Right && surfaceRight <= childLeft) + || (edge == DragEdge.Left && surfaceLeft >= childRight) + || (edge == DragEdge.Top && surfaceTop >= childBottom) + || (edge == DragEdge.Bottom && surfaceBottom <= childTop)) r = true; + } + else if (getShowMode() == ShowMode.PullOut) { + if ((edge == DragEdge.Right && childRight <= getWidth()) + || (edge == DragEdge.Left && childLeft >= getPaddingLeft()) + || (edge == DragEdge.Top && childTop >= getPaddingTop()) + || (edge == DragEdge.Bottom && childBottom <= getHeight())) r = true; } return r; } - protected boolean isViewShowing(View child, Rect relativePosition, DragEdge availableEdge, int surfaceLeft, int surfaceTop, int surfaceRight, int surfaceBottom){ + protected boolean isViewShowing(View child, Rect relativePosition, DragEdge availableEdge, int surfaceLeft, + int surfaceTop, int surfaceRight, int surfaceBottom) + { int childLeft = relativePosition.left; int childRight = relativePosition.right; int childTop = relativePosition.top; int childBottom = relativePosition.bottom; - if(getShowMode() == ShowMode.LayDown){ - switch (availableEdge){ - case Right: - if(surfaceRight > childLeft && surfaceRight <= childRight){ - return true; - } - break; - case Left: - if(surfaceLeft < childRight && surfaceLeft >= childLeft){ - return true; - } - break; - case Top: - if(surfaceTop >= childTop && surfaceTop < childBottom){ - return true; - } - break; - case Bottom: - if(surfaceBottom > childTop && surfaceBottom <= childBottom){ - return true; - } - break; + if (getShowMode() == ShowMode.LayDown) { + switch (availableEdge) + { + case Right: + if (surfaceRight > childLeft && surfaceRight <= childRight) { + return true; + } + break; + case Left: + if (surfaceLeft < childRight && surfaceLeft >= childLeft) { + return true; + } + break; + case Top: + if (surfaceTop >= childTop && surfaceTop < childBottom) { + return true; + } + break; + case Bottom: + if (surfaceBottom > childTop && surfaceBottom <= childBottom) { + return true; + } + break; } - }else if(getShowMode() == ShowMode.PullOut){ - switch (availableEdge){ - case Right: - if(childLeft <= getWidth() && childRight > getWidth()) - return true; - break; - case Left: - if(childRight >= getPaddingLeft() && childLeft < getPaddingLeft()) - return true; - break; - case Top: - if(childTop < getPaddingTop() && childBottom >= getPaddingTop()) - return true; - break; - case Bottom: - if(childTop < getHeight() && childTop >= getPaddingTop()) - return true; - break; + } + else if (getShowMode() == ShowMode.PullOut) { + switch (availableEdge) + { + case Right: + if (childLeft <= getWidth() && childRight > getWidth()) return true; + break; + case Left: + if (childRight >= getPaddingLeft() && childLeft < getPaddingLeft()) return true; + break; + case Top: + if (childTop < getPaddingTop() && childBottom >= getPaddingTop()) return true; + break; + case Bottom: + if (childTop < getHeight() && childTop >= getPaddingTop()) return true; + break; } } return false; } - - - protected Rect getRelativePosition(View child){ + protected Rect getRelativePosition(View child) + { View t = child; - Rect r = new Rect(t.getLeft(), t.getTop(), 0 , 0); - while(t.getParent() != null && t != getRootView()){ - t = (View)t.getParent(); - if(t == this) - break; + Rect r = new Rect(t.getLeft(), t.getTop(), 0, 0); + while (t.getParent() != null && t != getRootView()) { + t = (View) t.getParent(); + if (t == this) break; r.left += t.getLeft(); r.top += t.getTop(); } @@ -450,49 +519,55 @@ protected Rect getRelativePosition(View child){ private int mEventCounter = 0; - protected void dispatchSwipeEvent(int surfaceLeft, int surfaceTop, int dx, int dy){ + protected void dispatchSwipeEvent(int surfaceLeft, int surfaceTop, int dx, int dy) + { DragEdge edge = getDragEdge(); boolean open = true; - if(edge == DragEdge.Left){ - if(dx < 0) open = false; - }else if(edge == DragEdge.Right){ - if(dx > 0) open = false; - }else if(edge == DragEdge.Top){ - if(dy < 0) open = false; - }else if(edge == DragEdge.Bottom){ - if(dy > 0) open = false; + if (edge == DragEdge.Left) { + if (dx < 0) open = false; + } + else if (edge == DragEdge.Right) { + if (dx > 0) open = false; + } + else if (edge == DragEdge.Top) { + if (dy < 0) open = false; + } + else if (edge == DragEdge.Bottom) { + if (dy > 0) open = false; } dispatchSwipeEvent(surfaceLeft, surfaceTop, open); } - protected void dispatchSwipeEvent(int surfaceLeft, int surfaceTop, boolean open){ + protected void dispatchSwipeEvent(int surfaceLeft, int surfaceTop, boolean open) + { safeBottomView(); Status status = getOpenStatus(); - if(!mSwipeListeners.isEmpty()){ + if (!mSwipeListeners.isEmpty()) { mEventCounter++; - for(SwipeListener l : mSwipeListeners){ - if(mEventCounter == 1){ - if(open){ + for (SwipeListener l : mSwipeListeners) { + if (mEventCounter == 1) { + if (open) { l.onStartOpen(this); - }else{ + } + else { l.onStartClose(this); } } l.onUpdate(SwipeLayout.this, surfaceLeft - getPaddingLeft(), surfaceTop - getPaddingTop()); } - if(status == Status.Close){ - for(SwipeListener l : mSwipeListeners){ + if (status == Status.Close) { + for (SwipeListener l : mSwipeListeners) { l.onClose(SwipeLayout.this); } mEventCounter = 0; } - if(status == Status.Open){ - getBottomView().setEnabled(true); - for(SwipeListener l : mSwipeListeners){ + if (status == Status.Open) { + getBottomViews().get(currentDirectionIndex).setEnabled(true); + for (SwipeListener l : mSwipeListeners) { l.onOpen(SwipeLayout.this); } mEventCounter = 0; @@ -503,84 +578,93 @@ protected void dispatchSwipeEvent(int surfaceLeft, int surfaceTop, boolean open) /** * prevent bottom view get any touch event. Especially in LayDown mode. */ - private void safeBottomView(){ + private void safeBottomView() + { Status status = getOpenStatus(); - ViewGroup bottom = getBottomView(); + List bottoms = getBottomViews(); - if(status == Status.Close){ - if(bottom.getVisibility() != INVISIBLE) - bottom.setVisibility(INVISIBLE); - }else { - if(bottom.getVisibility() != VISIBLE) - bottom.setVisibility(VISIBLE); + if (status == Status.Close) { + for (ViewGroup bottom : bottoms) { + if (bottom.getVisibility() != INVISIBLE) bottom.setVisibility(INVISIBLE); + } + } + else { + if (bottoms.get(currentDirectionIndex).getVisibility() != VISIBLE) + bottoms.get(currentDirectionIndex).setVisibility(VISIBLE); } } - - protected void dispatchRevealEvent(final int surfaceLeft, final int surfaceTop, final int surfaceRight, final int surfaceBottom){ - if(mRevealListeners.isEmpty()) return; - for(Map.Entry> entry : mRevealListeners.entrySet()){ + protected void dispatchRevealEvent(final int surfaceLeft, final int surfaceTop, final int surfaceRight, + final int surfaceBottom) + { + if (mRevealListeners.isEmpty()) return; + for (Map.Entry> entry : mRevealListeners.entrySet()) { View child = entry.getKey(); Rect rect = getRelativePosition(child); - if(isViewShowing(child,rect, mDragEdge, surfaceLeft, surfaceTop, surfaceRight, surfaceBottom)){ + if (isViewShowing(child, rect, mDragEdges.get(currentDirectionIndex), surfaceLeft, surfaceTop, + surfaceRight, surfaceBottom)) { mShowEntirely.put(child, false); int distance = 0; float fraction = 0f; - if(getShowMode() == ShowMode.LayDown){ - switch (mDragEdge){ - case Left: - distance = rect.left - surfaceLeft; - fraction = distance/(float)child.getWidth(); - break; - case Right: - distance = rect.right - surfaceRight; - fraction = distance/(float)child.getWidth(); - break; - case Top: - distance = rect.top - surfaceTop; - fraction = distance/(float)child.getHeight(); - break; - case Bottom: - distance = rect.bottom - surfaceBottom; - fraction = distance/(float)child.getHeight(); - break; + if (getShowMode() == ShowMode.LayDown) { + switch (mDragEdges.get(currentDirectionIndex)) + { + case Left: + distance = rect.left - surfaceLeft; + fraction = distance / (float) child.getWidth(); + break; + case Right: + distance = rect.right - surfaceRight; + fraction = distance / (float) child.getWidth(); + break; + case Top: + distance = rect.top - surfaceTop; + fraction = distance / (float) child.getHeight(); + break; + case Bottom: + distance = rect.bottom - surfaceBottom; + fraction = distance / (float) child.getHeight(); + break; } - }else if(getShowMode() == ShowMode.PullOut){ - switch (mDragEdge){ - case Left: - distance = rect.right - getPaddingLeft(); - fraction = distance / (float)child.getWidth(); - break; - case Right: - distance = rect.left - getWidth(); - fraction = distance / (float)child.getWidth(); - break; - case Top: - distance = rect.bottom - getPaddingTop(); - fraction = distance/(float)child.getHeight(); - break; - case Bottom: - distance = rect.top - getHeight(); - fraction = distance/(float)child.getHeight(); - break; + } + else if (getShowMode() == ShowMode.PullOut) { + switch (mDragEdges.get(currentDirectionIndex)) + { + case Left: + distance = rect.right - getPaddingLeft(); + fraction = distance / (float) child.getWidth(); + break; + case Right: + distance = rect.left - getWidth(); + fraction = distance / (float) child.getWidth(); + break; + case Top: + distance = rect.bottom - getPaddingTop(); + fraction = distance / (float) child.getHeight(); + break; + case Bottom: + distance = rect.top - getHeight(); + fraction = distance / (float) child.getHeight(); + break; } } - for(OnRevealListener l : entry.getValue()){ - l.onReveal(child, mDragEdge, Math.abs(fraction), distance); - if(Math.abs(fraction) == 1){ + for (OnRevealListener l : entry.getValue()) { + l.onReveal(child, mDragEdges.get(currentDirectionIndex), Math.abs(fraction), distance); + if (Math.abs(fraction) == 1) { mShowEntirely.put(child, true); } } } - if(isViewTotallyFirstShowed(child, rect, mDragEdge, surfaceLeft, surfaceTop, surfaceRight, surfaceBottom)){ + if (isViewTotallyFirstShowed(child, rect, mDragEdges.get(currentDirectionIndex), surfaceLeft, surfaceTop, + surfaceRight, surfaceBottom)) { mShowEntirely.put(child, true); - for(OnRevealListener l : entry.getValue()){ - if(mDragEdge == DragEdge.Left || mDragEdge == DragEdge.Right) - l.onReveal(child, mDragEdge, 1, child.getWidth()); - else - l.onReveal(child, mDragEdge, 1, child.getHeight()); + for (OnRevealListener l : entry.getValue()) { + if (mDragEdges.get(currentDirectionIndex) == DragEdge.Left + || mDragEdges.get(currentDirectionIndex) == DragEdge.Right) + l.onReveal(child, mDragEdges.get(currentDirectionIndex), 1, child.getWidth()); + else l.onReveal(child, mDragEdges.get(currentDirectionIndex), 1, child.getHeight()); } } @@ -588,95 +672,103 @@ protected void dispatchRevealEvent(final int surfaceLeft, final int surfaceTop, } @Override - public void computeScroll() { + public void computeScroll() + { super.computeScroll(); - if(mDragHelper.continueSettling(true)) { + if (mDragHelper.continueSettling(true)) { ViewCompat.postInvalidateOnAnimation(this); } } /** - * {@link android.view.View.OnLayoutChangeListener} added in API 11. - * I need to support it from API 8. + * {@link android.view.View.OnLayoutChangeListener} added in API 11. I need + * to support it from API 8. */ - public interface OnLayout{ + public interface OnLayout + { public void onLayout(SwipeLayout v); } private List mOnLayoutListeners; - public void addOnLayoutListener(OnLayout l){ - if(mOnLayoutListeners == null) - mOnLayoutListeners = new ArrayList(); + public void addOnLayoutListener(OnLayout l) + { + if (mOnLayoutListeners == null) mOnLayoutListeners = new ArrayList(); mOnLayoutListeners.add(l); } - public void removeOnLayoutListener(OnLayout l){ - if(mOnLayoutListeners != null) - mOnLayoutListeners.remove(l); + public void removeOnLayoutListener(OnLayout l) + { + if (mOnLayoutListeners != null) mOnLayoutListeners.remove(l); } @Override - protected void onLayout(boolean changed, int l, int t, int r, int b) { + protected void onLayout(boolean changed, int l, int t, int r, int b) + { int childCount = getChildCount(); - if(childCount != 2){ - throw new IllegalStateException("You need 2 views in SwipeLayout"); + if (childCount != 1 + mDragEdges.size()) { + throw new IllegalStateException("You need to have one surface view plus one view for each of your drag edges"); } - if(!(getChildAt(0) instanceof ViewGroup) || !(getChildAt(1) instanceof ViewGroup)){ - throw new IllegalArgumentException("The 2 children in SwipeLayout must be an instance of ViewGroup"); + for (int i = 0; i < childCount; i++) { + if (!(getChildAt(i) instanceof ViewGroup)) { + throw new IllegalArgumentException("All the children in SwipeLayout must be an instance of ViewGroup"); + } } - if(mShowMode == ShowMode.PullOut) + if (mShowMode == ShowMode.PullOut) layoutPullOut(); - else if(mShowMode == ShowMode.LayDown) - layoutLayDown(); + else if (mShowMode == ShowMode.LayDown) layoutLayDown(); safeBottomView(); - if(mOnLayoutListeners != null) - for(int i = 0; i < mOnLayoutListeners.size(); i++){ - mOnLayoutListeners.get(i).onLayout(this); - } + if (mOnLayoutListeners != null) for (int i = 0; i < mOnLayoutListeners.size(); i++) { + mOnLayoutListeners.get(i).onLayout(this); + } } - void layoutPullOut(){ + void layoutPullOut() + { Rect rect = computeSurfaceLayoutArea(false); getSurfaceView().layout(rect.left, rect.top, rect.right, rect.bottom); rect = computeBottomLayoutAreaViaSurface(ShowMode.PullOut, rect); - getBottomView().layout(rect.left, rect.top, rect.right, rect.bottom); + getBottomViews().get(currentDirectionIndex).layout(rect.left, rect.top, rect.right, rect.bottom); bringChildToFront(getSurfaceView()); } - void layoutLayDown(){ + void layoutLayDown() + { Rect rect = computeSurfaceLayoutArea(false); getSurfaceView().layout(rect.left, rect.top, rect.right, rect.bottom); rect = computeBottomLayoutAreaViaSurface(ShowMode.LayDown, rect); - getBottomView().layout(rect.left, rect.top, rect.right, rect.bottom); + getBottomViews().get(currentDirectionIndex).layout(rect.left, rect.top, rect.right, rect.bottom); bringChildToFront(getSurfaceView()); } - @Override - protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) + { super.onMeasure(widthMeasureSpec, heightMeasureSpec); - if(mDragEdge == DragEdge.Left || mDragEdge == DragEdge.Right) - mDragDistance = getBottomView().getMeasuredWidth()-dp2px(mHorizontalSwipeOffset); - else - mDragDistance = getBottomView().getMeasuredHeight()-dp2px(mVerticalSwipeOffset); + if (mDragEdges.get(currentDirectionIndex) == DragEdge.Left + || mDragEdges.get(currentDirectionIndex) == DragEdge.Right) + mDragDistance = getBottomViews().get(currentDirectionIndex).getMeasuredWidth() + - dp2px(getCurrentOffset()); + else mDragDistance = getBottomViews().get(currentDirectionIndex).getMeasuredHeight() + - dp2px(getCurrentOffset()); } private boolean mTouchConsumedByChild = false; @Override - public boolean onInterceptTouchEvent(MotionEvent ev) { + public boolean onInterceptTouchEvent(MotionEvent ev) + { - if(!isEnabled() || !isEnabledInAdapterView()){ + if (!isEnabled() || !isEnabledInAdapterView()) { return true; } - if(!isSwipeEnabled()){ + if (!isSwipeEnabled()) { return false; } @@ -686,49 +778,51 @@ public boolean onInterceptTouchEvent(MotionEvent ev) { } } // - //if a child wants to handle the touch event, - //then let it do it. + // if a child wants to handle the touch event, + // then let it do it. // int action = ev.getActionMasked(); - switch (action){ - case MotionEvent.ACTION_DOWN: - Status status = getOpenStatus(); - if(status == Status.Close){ - mTouchConsumedByChild = childNeedHandleTouchEvent(getSurfaceView(), ev) != null; - }else if(status == Status.Open){ - mTouchConsumedByChild = childNeedHandleTouchEvent(getBottomView(), ev) != null; - } - break; - case MotionEvent.ACTION_UP: - case MotionEvent.ACTION_CANCEL: - mTouchConsumedByChild = false; + switch (action) + { + case MotionEvent.ACTION_DOWN: + Status status = getOpenStatus(); + if (status == Status.Close) { + mTouchConsumedByChild = childNeedHandleTouchEvent(getSurfaceView(), ev) != null; + } + else if (status == Status.Open) { + mTouchConsumedByChild = childNeedHandleTouchEvent(getBottomViews().get(currentDirectionIndex), ev) != null; + } + break; + case MotionEvent.ACTION_UP: + case MotionEvent.ACTION_CANCEL: + mTouchConsumedByChild = false; } - if(mTouchConsumedByChild) return false; + if (mTouchConsumedByChild) return false; return mDragHelper.shouldInterceptTouchEvent(ev); } /** * if the ViewGroup children want to handle this event. + * * @param v * @param event * @return */ - private View childNeedHandleTouchEvent(ViewGroup v, MotionEvent event){ - if(v == null) return null; - if(v.onTouchEvent(event)) - return v; + private View childNeedHandleTouchEvent(ViewGroup v, MotionEvent event) + { + if (v == null) return null; + if (v.onTouchEvent(event)) return v; int childCount = v.getChildCount(); - for(int i = childCount - 1; i >= 0; i--){ + for (int i = childCount - 1; i >= 0; i--) { View child = v.getChildAt(i); - if(child instanceof ViewGroup){ + if (child instanceof ViewGroup) { View grandChild = childNeedHandleTouchEvent((ViewGroup) child, event); - if(grandChild != null) - return grandChild; - }else{ - if(childNeedHandleTouchEvent(v.getChildAt(i), event)) - return v.getChildAt(i); + if (grandChild != null) return grandChild; + } + else { + if (childNeedHandleTouchEvent(v.getChildAt(i), event)) return v.getChildAt(i); } } return null; @@ -736,33 +830,35 @@ private View childNeedHandleTouchEvent(ViewGroup v, MotionEvent event){ /** * if the view (v) wants to handle this event. + * * @param v * @param event * @return */ - private boolean childNeedHandleTouchEvent(View v, MotionEvent event){ - if(v == null) return false; + private boolean childNeedHandleTouchEvent(View v, MotionEvent event) + { + if (v == null) return false; int[] loc = new int[2]; v.getLocationOnScreen(loc); int left = loc[0], top = loc[1]; - if(event.getRawX() > left && event.getRawX() < left + v.getWidth() - && event.getRawY() > top && event.getRawY() < top + v.getHeight()){ + if (event.getRawX() > left && event.getRawX() < left + v.getWidth() && event.getRawY() > top + && event.getRawY() < top + v.getHeight()) { return v.onTouchEvent(event); } return false; } - private float sX = -1 , sY = -1; + private float sX = -1, sY = -1; + @Override - public boolean onTouchEvent(MotionEvent event) { - if(!isEnabledInAdapterView() || !isEnabled()) - return true; + public boolean onTouchEvent(MotionEvent event) + { + if (!isEnabledInAdapterView() || !isEnabled()) return true; - if(!isSwipeEnabled()) - return super.onTouchEvent(event); + if (!isSwipeEnabled()) return super.onTouchEvent(event); int action = event.getActionMasked(); ViewParent parent = getParent(); @@ -770,123 +866,148 @@ public boolean onTouchEvent(MotionEvent event) { gestureDetector.onTouchEvent(event); Status status = getOpenStatus(); ViewGroup touching = null; - if(status == Status.Close){ + if (status == Status.Close) { touching = getSurfaceView(); - }else if(status == Status.Open){ - touching = getBottomView(); } + else if (status == Status.Open) { + touching = getBottomViews().get(currentDirectionIndex); + } + + switch (action) + { + case MotionEvent.ACTION_DOWN: + mDragHelper.processTouchEvent(event); + parent.requestDisallowInterceptTouchEvent(true); + + sX = event.getRawX(); + sY = event.getRawY(); + + if (touching != null) touching.setPressed(true); - switch (action){ - case MotionEvent.ACTION_DOWN: + return true; + case MotionEvent.ACTION_MOVE: { + if (sX == -1 || sY == -1) { + // Trick: + // When in nested mode, we need to send a constructed + // ACTION_DOWN MotionEvent to mDragHelper, to help + // it initialize itself. + event.setAction(MotionEvent.ACTION_DOWN); mDragHelper.processTouchEvent(event); parent.requestDisallowInterceptTouchEvent(true); - sX = event.getRawX(); sY = event.getRawY(); - - if(touching != null) - touching.setPressed(true); - return true; - case MotionEvent.ACTION_MOVE:{ - if(sX == -1 || sY == -1){ - // Trick: - // When in nested mode, we need to send a constructed ACTION_DOWN MotionEvent to mDragHelper, to help - // it initialize itself. - event.setAction(MotionEvent.ACTION_DOWN); - mDragHelper.processTouchEvent(event); - parent.requestDisallowInterceptTouchEvent(true); - sX = event.getRawX(); - sY = event.getRawY(); - return true; - } - - float distanceX = event.getRawX() - sX; - float distanceY = event.getRawY() - sY; - float angle = Math.abs(distanceY / distanceX); - angle = (float)Math.toDegrees(Math.atan(angle)); - - boolean doNothing = false; - if(mDragEdge == DragEdge.Right){ - boolean suitable = (status == Status.Open && distanceX > 0) || (status == Status.Close && distanceX < 0); - suitable = suitable || (status == Status.Middle); + } - if(angle > 30 || !suitable){ - doNothing = true; + float distanceX = event.getRawX() - sX; + float distanceY = event.getRawY() - sY; + float angle = Math.abs(distanceY / distanceX); + angle = (float) Math.toDegrees(Math.atan(angle)); + if (getOpenStatus() == Status.Close) { + if (angle < 45) { + if (mLeftIndex != -1 && distanceX > 0) { + currentDirectionIndex = mLeftIndex; + } else if (mRightIndex != -1) { + currentDirectionIndex = mRightIndex; + } + } else { + if (mTopIndex != -1 && distanceY < 0) { + currentDirectionIndex = mTopIndex; + } else if (mBottomIndex != -1) { + currentDirectionIndex = mBottomIndex; } } + updateBottomViews(); + } - if(mDragEdge == DragEdge.Left){ - boolean suitable = (status == Status.Open && distanceX < 0 ) || (status == Status.Close && distanceX > 0); - suitable = suitable || status == Status.Middle; + boolean doNothing = false; + if (mDragEdges.get(currentDirectionIndex) == DragEdge.Right) { + boolean suitable = (status == Status.Open && distanceX > 0) + || (status == Status.Close && distanceX < 0); + suitable = suitable || (status == Status.Middle); - if(angle > 30 || ! suitable){ - doNothing = true; - } + if (angle > 30 || !suitable) { + doNothing = true; } + } - if(mDragEdge == DragEdge.Top){ - boolean suitable = (status == Status.Open && distanceY < 0 ) || (status == Status.Close && distanceY > 0); - suitable = suitable || status == Status.Middle; + if (mDragEdges.get(currentDirectionIndex) == DragEdge.Left) { + boolean suitable = (status == Status.Open && distanceX < 0) + || (status == Status.Close && distanceX > 0); + suitable = suitable || status == Status.Middle; - if(angle < 60 || ! suitable){ - doNothing = true; - } + if (angle > 30 || !suitable) { + doNothing = true; } + } - if(mDragEdge == DragEdge.Bottom){ - boolean suitable = (status == Status.Open && distanceY > 0 ) || (status == Status.Close && distanceY < 0); - suitable = suitable || status == Status.Middle; + if (mDragEdges.get(currentDirectionIndex) == DragEdge.Top) { + boolean suitable = (status == Status.Open && distanceY < 0) + || (status == Status.Close && distanceY > 0); + suitable = suitable || status == Status.Middle; - if(angle < 60 || ! suitable){ - doNothing = true; - } + if (angle < 60 || !suitable) { + doNothing = true; } + } - if(doNothing){ - parent.requestDisallowInterceptTouchEvent(false); - return false; - }else{ - if(touching != null){ - touching.setPressed(false); - } - parent.requestDisallowInterceptTouchEvent(true); - mDragHelper.processTouchEvent(event); + if (mDragEdges.get(currentDirectionIndex) == DragEdge.Bottom) { + boolean suitable = (status == Status.Open && distanceY > 0) + || (status == Status.Close && distanceY < 0); + suitable = suitable || status == Status.Middle; + + if (angle < 60 || !suitable) { + doNothing = true; } - break; } - case MotionEvent.ACTION_UP: - case MotionEvent.ACTION_CANCEL: - { - sX = -1; - sY = -1; - if(touching != null){ + + if (doNothing) { + parent.requestDisallowInterceptTouchEvent(false); + return false; + } + else { + if (touching != null) { touching.setPressed(false); } - } - default: parent.requestDisallowInterceptTouchEvent(true); mDragHelper.processTouchEvent(event); + } + break; + } + case MotionEvent.ACTION_UP: + case MotionEvent.ACTION_CANCEL: { + sX = -1; + sY = -1; + if (touching != null) { + touching.setPressed(false); + } + } + default: + parent.requestDisallowInterceptTouchEvent(true); + mDragHelper.processTouchEvent(event); } return true; } /** - * if working in {@link android.widget.AdapterView}, we should response {@link android.widget.Adapter} - * isEnable(int position). + * if working in {@link android.widget.AdapterView}, we should response + * {@link android.widget.Adapter} isEnable(int position). + * * @return true when item is enabled, else disabled. */ - private boolean isEnabledInAdapterView(){ + private boolean isEnabledInAdapterView() + { AdapterView adapterView = getAdapterView(); boolean enable = true; - if(adapterView != null){ + if (adapterView != null) { Adapter adapter = adapterView.getAdapter(); - if(adapter != null){ + if (adapter != null) { int p = adapterView.getPositionForView(SwipeLayout.this); - if(adapter instanceof BaseAdapter){ + if (adapter instanceof BaseAdapter) { enable = ((BaseAdapter) adapter).isEnabled(p); - }else if(adapter instanceof ListAdapter){ + } + else if (adapter instanceof ListAdapter) { enable = ((ListAdapter) adapter).isEnabled(p); } } @@ -894,91 +1015,107 @@ private boolean isEnabledInAdapterView(){ return enable; } - public void setSwipeEnabled(boolean enabled){ + public void setSwipeEnabled(boolean enabled) + { mSwipeEnabled = enabled; } - public boolean isSwipeEnabled(){ + public boolean isSwipeEnabled() + { return mSwipeEnabled; } - private boolean insideAdapterView(){ + private boolean insideAdapterView() + { return getAdapterView() != null; } - private AdapterView getAdapterView(){ + private AdapterView getAdapterView() + { ViewParent t = getParent(); - while(t != null){ - if(t instanceof AdapterView){ - return (AdapterView)t; + while (t != null) { + if (t instanceof AdapterView) { + return (AdapterView) t; } t = t.getParent(); } return null; } - private void performAdapterViewItemClick(MotionEvent e){ + private void performAdapterViewItemClick(MotionEvent e) + { ViewParent t = getParent(); - while(t != null) { - if(t instanceof AdapterView){ - AdapterView view = (AdapterView)t; + while (t != null) { + if (t instanceof AdapterView) { + AdapterView view = (AdapterView) t; int p = view.getPositionForView(SwipeLayout.this); - if( p != AdapterView.INVALID_POSITION && - view.performItemClick(view.getChildAt(p-view.getFirstVisiblePosition()), p, view.getAdapter().getItemId(p))) - return; - }else{ - if(t instanceof View && ((View) t).performClick()) - return; + if (p != AdapterView.INVALID_POSITION + && view.performItemClick(view.getChildAt(p - view.getFirstVisiblePosition()), p, view + .getAdapter().getItemId(p))) return; + } + else { + if (t instanceof View && ((View) t).performClick()) return; } t = t.getParent(); } } private GestureDetector gestureDetector = new GestureDetector(getContext(), new SwipeDetector()); - class SwipeDetector extends GestureDetector.SimpleOnGestureListener{ + + class SwipeDetector extends GestureDetector.SimpleOnGestureListener + { @Override - public boolean onDown(MotionEvent e) { + public boolean onDown(MotionEvent e) + { return true; } /** - * Simulate the touch event lifecycle. If you use SwipeLayout in {@link android.widget.AdapterView} - * ({@link android.widget.ListView}, {@link android.widget.GridView} etc.) It will manually call - * {@link android.widget.AdapterView}.performItemClick, performItemLongClick. + * Simulate the touch event lifecycle. If you use SwipeLayout in + * {@link android.widget.AdapterView} ({@link android.widget.ListView}, + * {@link android.widget.GridView} etc.) It will manually call + * {@link android.widget.AdapterView}.performItemClick, + * performItemLongClick. + * * @param e * @return */ @Override - public boolean onSingleTapUp(MotionEvent e) { - if(mDoubleClickListener == null){ + public boolean onSingleTapUp(MotionEvent e) + { + if (mDoubleClickListener == null) { performAdapterViewItemClick(e); } return true; } @Override - public boolean onSingleTapConfirmed(MotionEvent e) { - if(mDoubleClickListener != null){ + public boolean onSingleTapConfirmed(MotionEvent e) + { + if (mDoubleClickListener != null) { performAdapterViewItemClick(e); } return true; } @Override - public void onLongPress(MotionEvent e) { + public void onLongPress(MotionEvent e) + { performLongClick(); } @Override - public boolean onDoubleTap(MotionEvent e) { - if(mDoubleClickListener != null){ + public boolean onDoubleTap(MotionEvent e) + { + if (mDoubleClickListener != null) { View target; - ViewGroup bottom = getBottomView(); + ViewGroup bottom = getBottomViews().get(currentDirectionIndex); ViewGroup surface = getSurfaceView(); - if(e.getX() > bottom.getLeft() && e.getX() < bottom.getRight() - && e.getY() > bottom.getTop() && e.getY() < bottom.getBottom()){ + if (e.getX() > bottom.getLeft() && e.getX() < bottom.getRight() && e.getY() > bottom.getTop() + && e.getY() < bottom.getBottom()) { target = bottom; - }else{ + } + else { target = surface; } mDoubleClickListener.onDoubleClick(SwipeLayout.this, target == surface); @@ -987,68 +1124,91 @@ public boolean onDoubleTap(MotionEvent e) { } } - public void setDragEdge(DragEdge dragEdge){ - mDragEdge = dragEdge; + public void setDragEdge(DragEdge dragEdge) + { + mDragEdges = new ArrayList(); + mDragEdges.add(dragEdge); + currentDirectionIndex = 0; + populateIndexes(); requestLayout(); + updateBottomViews(); } /** - * set the drag distance, it will force set the bottom view's width or height via this value. + * set the drag distance, it will force set the bottom view's width or + * height via this value. + * * @param max */ - public void setDragDistance(int max){ - if(max < 0) - throw new IllegalArgumentException("Drag distance can not be < 0"); + public void setDragDistance(int max) + { + if (max < 0) throw new IllegalArgumentException("Drag distance can not be < 0"); mDragDistance = dp2px(max); requestLayout(); } /** * There are 2 diffirent show mode. - * {@link com.daimajia.swipe.SwipeLayout.ShowMode}.PullOut and {@link com.daimajia.swipe.SwipeLayout.ShowMode}.LayDown. + * {@link com.daimajia.swipe.SwipeLayout.ShowMode}.PullOut and + * {@link com.daimajia.swipe.SwipeLayout.ShowMode}.LayDown. + * * @param mode */ - public void setShowMode(ShowMode mode){ + public void setShowMode(ShowMode mode) + { mShowMode = mode; requestLayout(); } - public DragEdge getDragEdge(){ - return mDragEdge; + public DragEdge getDragEdge() + { + return mDragEdges.get(currentDirectionIndex); } - public int getDragDistance(){ + public int getDragDistance() + { return mDragDistance; } - public ShowMode getShowMode(){ + public ShowMode getShowMode() + { return mShowMode; } - public ViewGroup getSurfaceView(){ - return (ViewGroup)getChildAt(1); + public ViewGroup getSurfaceView() + { + return (ViewGroup) getChildAt(getChildCount() - 1); } - public ViewGroup getBottomView(){ - return (ViewGroup)getChildAt(0); + public List getBottomViews() + { + List lvg = new ArrayList(); + for (int i = 0; i < (getChildCount() - 1); i++) { + lvg.add((ViewGroup) getChildAt(i)); + } + return lvg; } - public enum Status { - Middle , Open, Close + public enum Status + { + Middle, + Open, + Close } /** * get the open status. - * @return {@link com.daimajia.swipe.SwipeLayout.Status} - * Open , Close or Middle. + * + * @return {@link com.daimajia.swipe.SwipeLayout.Status} Open , Close or + * Middle. */ - public Status getOpenStatus(){ + public Status getOpenStatus() + { int surfaceLeft = getSurfaceView().getLeft(); int surfaceTop = getSurfaceView().getTop(); - if(surfaceLeft == getPaddingLeft() && surfaceTop == getPaddingTop()) - return Status.Close; + if (surfaceLeft == getPaddingLeft() && surfaceTop == getPaddingTop()) return Status.Close; - if(surfaceLeft == (getPaddingLeft() - mDragDistance) || surfaceLeft == (getPaddingLeft() + mDragDistance) + if (surfaceLeft == (getPaddingLeft() - mDragDistance) || surfaceLeft == (getPaddingLeft() + mDragDistance) || surfaceTop == (getPaddingTop() - mDragDistance) || surfaceTop == (getPaddingTop() + mDragDistance)) return Status.Open; @@ -1057,29 +1217,36 @@ public Status getOpenStatus(){ /** * Process the surface release event. + * * @param xvel * @param yvel */ - private void processSurfaceRelease(float xvel, float yvel){ - if(xvel == 0 && getOpenStatus() == Status.Middle) - close(); - - if(mDragEdge == DragEdge.Left || mDragEdge == DragEdge.Right){ - if(xvel > 0){ - if(mDragEdge == DragEdge.Left) open(); + private void processSurfaceRelease(float xvel, float yvel) + { + if (xvel == 0 && getOpenStatus() == Status.Middle) close(); + + if (mDragEdges.get(currentDirectionIndex) == DragEdge.Left + || mDragEdges.get(currentDirectionIndex) == DragEdge.Right) { + if (xvel > 0) { + if (mDragEdges.get(currentDirectionIndex) == DragEdge.Left) + open(); else close(); } - if(xvel < 0){ - if(mDragEdge == DragEdge.Left) close(); + if (xvel < 0) { + if (mDragEdges.get(currentDirectionIndex) == DragEdge.Left) + close(); else open(); } - }else{ - if(yvel > 0){ - if(mDragEdge == DragEdge.Top) open(); + } + else { + if (yvel > 0) { + if (mDragEdges.get(currentDirectionIndex) == DragEdge.Top) + open(); else close(); } - if(yvel < 0){ - if(mDragEdge == DragEdge.Top) close(); + if (yvel < 0) { + if (mDragEdges.get(currentDirectionIndex) == DragEdge.Top) + close(); else open(); } } @@ -1087,53 +1254,61 @@ private void processSurfaceRelease(float xvel, float yvel){ /** * process bottom (PullOut mode) hand release event. + * * @param xvel * @param yvel */ - private void processBottomPullOutRelease(float xvel, float yvel){ + private void processBottomPullOutRelease(float xvel, float yvel) + { - if(xvel == 0 && getOpenStatus() == Status.Middle) - close(); + if (xvel == 0 && getOpenStatus() == Status.Middle) close(); - if(mDragEdge == DragEdge.Left || mDragEdge == DragEdge.Right){ - if(xvel > 0){ - if(mDragEdge == DragEdge.Left) open(); + if (mDragEdges.get(currentDirectionIndex) == DragEdge.Left + || mDragEdges.get(currentDirectionIndex) == DragEdge.Right) { + if (xvel > 0) { + if (mDragEdges.get(currentDirectionIndex) == DragEdge.Left) + open(); else close(); } - if(xvel < 0){ - if(mDragEdge == DragEdge.Left) close(); + if (xvel < 0) { + if (mDragEdges.get(currentDirectionIndex) == DragEdge.Left) + close(); else open(); } - }else{ - if(yvel > 0) { - if (mDragEdge == DragEdge.Top) open(); + } + else { + if (yvel > 0) { + if (mDragEdges.get(currentDirectionIndex) == DragEdge.Top) + open(); else close(); } - if(yvel < 0){ - if(mDragEdge == DragEdge.Top) close(); - else open(); + if (yvel < 0) { + if (mDragEdges.get(currentDirectionIndex) == DragEdge.Top) + close(); + else open(); } } } /** * process bottom (LayDown mode) hand release event. + * * @param xvel * @param yvel */ - private void processBottomLayDownMode(float xvel, float yvel){ + private void processBottomLayDownMode(float xvel, float yvel) + { - if(xvel == 0 && getOpenStatus() == Status.Middle) - close(); + if (xvel == 0 && getOpenStatus() == Status.Middle) close(); int l = getPaddingLeft(), t = getPaddingTop(); - if(xvel < 0 && mDragEdge == DragEdge.Right) l -= mDragDistance; - if(xvel > 0 && mDragEdge == DragEdge.Left) l += mDragDistance; + if (xvel < 0 && mDragEdges.get(currentDirectionIndex) == DragEdge.Right) l -= mDragDistance; + if (xvel > 0 && mDragEdges.get(currentDirectionIndex) == DragEdge.Left) l += mDragDistance; - if(yvel > 0 && mDragEdge == DragEdge.Top) t += mDragDistance; - if(yvel < 0 && mDragEdge == DragEdge.Bottom) t -= mDragDistance; + if (yvel > 0 && mDragEdges.get(currentDirectionIndex) == DragEdge.Top) t += mDragDistance; + if (yvel < 0 && mDragEdges.get(currentDirectionIndex) == DragEdge.Bottom) t -= mDragDistance; mDragHelper.smoothSlideViewTo(getSurfaceView(), l, t); invalidate(); @@ -1142,33 +1317,37 @@ private void processBottomLayDownMode(float xvel, float yvel){ /** * smoothly open surface. */ - public void open(){ + public void open() + { open(true, true); } - public void open(boolean smooth){ + public void open(boolean smooth) + { open(smooth, true); } - public void open(boolean smooth, boolean notify){ - ViewGroup surface = getSurfaceView(), bottom = getBottomView(); - int dx,dy; + public void open(boolean smooth, boolean notify) + { + ViewGroup surface = getSurfaceView(), bottom = getBottomViews().get(currentDirectionIndex); + int dx, dy; Rect rect = computeSurfaceLayoutArea(true); - if(smooth) { + if (smooth) { mDragHelper.smoothSlideViewTo(getSurfaceView(), rect.left, rect.top); } - else{ + else { dx = rect.left - surface.getLeft(); dy = rect.top - surface.getTop(); surface.layout(rect.left, rect.top, rect.right, rect.bottom); - if(getShowMode() == ShowMode.PullOut){ + if (getShowMode() == ShowMode.PullOut) { Rect bRect = computeBottomLayoutAreaViaSurface(ShowMode.PullOut, rect); bottom.layout(bRect.left, bRect.top, bRect.right, bRect.bottom); } - if(notify) { + if (notify) { dispatchRevealEvent(rect.left, rect.top, rect.right, rect.bottom); dispatchSwipeEvent(rect.left, rect.top, dx, dy); - }else{ + } + else { safeBottomView(); } } @@ -1178,123 +1357,208 @@ public void open(boolean smooth, boolean notify){ /** * smoothly close surface. */ - public void close(){ + public void close() + { close(true, true); } - public void close(boolean smooth){ + public void close(boolean smooth) + { close(smooth, true); } /** * close surface - * @param smooth smoothly or not. - * @param notify if notify all the listeners. + * + * @param smooth + * smoothly or not. + * @param notify + * if notify all the listeners. */ - public void close(boolean smooth, boolean notify){ + public void close(boolean smooth, boolean notify) + { ViewGroup surface = getSurfaceView(); int dx, dy; - if(smooth) + if (smooth) mDragHelper.smoothSlideViewTo(getSurfaceView(), getPaddingLeft(), getPaddingTop()); else { Rect rect = computeSurfaceLayoutArea(false); dx = rect.left - surface.getLeft(); dy = rect.top - surface.getTop(); surface.layout(rect.left, rect.top, rect.right, rect.bottom); - if(notify) { + if (notify) { dispatchRevealEvent(rect.left, rect.top, rect.right, rect.bottom); dispatchSwipeEvent(rect.left, rect.top, dx, dy); - }else{ + } + else { safeBottomView(); } } invalidate(); } - public void toggle(){ + public void toggle() + { toggle(true); } - public void toggle(boolean smooth){ - if(getOpenStatus() == Status.Open) + public void toggle(boolean smooth) + { + if (getOpenStatus() == Status.Open) close(smooth); - else if(getOpenStatus() == Status.Close) - open(smooth); + else if (getOpenStatus() == Status.Close) open(smooth); } /** * a helper function to compute the Rect area that surface will hold in. - * @param open open status or close status. + * + * @param open + * open status or close status. * @return */ - private Rect computeSurfaceLayoutArea(boolean open){ + private Rect computeSurfaceLayoutArea(boolean open) + { int l = getPaddingLeft(), t = getPaddingTop(); - if(open){ - if(mDragEdge == DragEdge.Left) l = getPaddingLeft() + mDragDistance; - else if(mDragEdge == DragEdge.Right) l = getPaddingLeft() - mDragDistance; - else if(mDragEdge == DragEdge.Top) t = getPaddingTop() + mDragDistance; - else t = getPaddingTop() - mDragDistance; + if (open) { + if (mDragEdges.get(currentDirectionIndex) == DragEdge.Left) + l = getPaddingLeft() + mDragDistance; + else if (mDragEdges.get(currentDirectionIndex) == DragEdge.Right) + l = getPaddingLeft() - mDragDistance; + else if (mDragEdges.get(currentDirectionIndex) == DragEdge.Top) + t = getPaddingTop() + mDragDistance; + else t = getPaddingTop() - mDragDistance; } return new Rect(l, t, l + getMeasuredWidth(), t + getMeasuredHeight()); } - - private Rect computeBottomLayoutAreaViaSurface(ShowMode mode, Rect surfaceArea){ + private Rect computeBottomLayoutAreaViaSurface(ShowMode mode, Rect surfaceArea) + { Rect rect = surfaceArea; int bl = rect.left, bt = rect.top, br = rect.right, bb = rect.bottom; - if(mode == ShowMode.PullOut){ - if (mDragEdge == DragEdge.Left) bl = rect.left - mDragDistance; - else if (mDragEdge == DragEdge.Right) bl = rect.right; - else if (mDragEdge == DragEdge.Top) bt = rect.top - mDragDistance; - else bt = rect.bottom; - - if (mDragEdge == DragEdge.Left || mDragEdge == DragEdge.Right) { + if (mode == ShowMode.PullOut) { + if (mDragEdges.get(currentDirectionIndex) == DragEdge.Left) + bl = rect.left - mDragDistance; + else if (mDragEdges.get(currentDirectionIndex) == DragEdge.Right) + bl = rect.right; + else if (mDragEdges.get(currentDirectionIndex) == DragEdge.Top) + bt = rect.top - mDragDistance; + else bt = rect.bottom; + + if (mDragEdges.get(currentDirectionIndex) == DragEdge.Left || mDragEdges.get(currentDirectionIndex) == DragEdge.Right) { bb = rect.bottom; - br = bl + getBottomView().getMeasuredWidth(); - } else { - bb = bt + getBottomView().getMeasuredHeight(); + br = bl + getBottomViews().get(currentDirectionIndex).getMeasuredWidth(); + } + else { + bb = bt + getBottomViews().get(currentDirectionIndex).getMeasuredHeight(); br = rect.right; } - }else if(mode == ShowMode.LayDown){ - if(mDragEdge == DragEdge.Left) br = bl + mDragDistance; - else if(mDragEdge == DragEdge.Right) bl = br - mDragDistance; - else if(mDragEdge == DragEdge.Top) bb = bt + mDragDistance; - else bt = bb - mDragDistance; + } + else if (mode == ShowMode.LayDown) { + if (mDragEdges.get(currentDirectionIndex) == DragEdge.Left) + br = bl + mDragDistance; + else if (mDragEdges.get(currentDirectionIndex) == DragEdge.Right) + bl = br - mDragDistance; + else if (mDragEdges.get(currentDirectionIndex) == DragEdge.Top) + bb = bt + mDragDistance; + else bt = bb - mDragDistance; } return new Rect(bl, bt, br, bb); - } - private Rect computeBottomLayDown(DragEdge dragEdge){ + private Rect computeBottomLayDown(DragEdge dragEdge) + { int bl = getPaddingLeft(), bt = getPaddingTop(); int br, bb; - if(dragEdge == DragEdge.Right){ + if (dragEdge == DragEdge.Right) { bl = getMeasuredWidth() - mDragDistance; - }else if(dragEdge == DragEdge.Bottom){ + } + else if (dragEdge == DragEdge.Bottom) { bt = getMeasuredHeight() - mDragDistance; } - if(dragEdge == DragEdge.Left || dragEdge == DragEdge.Right){ + if (dragEdge == DragEdge.Left || dragEdge == DragEdge.Right) { br = bl + mDragDistance; bb = bt + getMeasuredHeight(); - }else{ + } + else { br = bl + getMeasuredWidth(); bb = bt + mDragDistance; } return new Rect(bl, bt, br, bb); } - public void setOnDoubleClickListener(DoubleClickListener doubleClickListener){ + public void setOnDoubleClickListener(DoubleClickListener doubleClickListener) + { mDoubleClickListener = doubleClickListener; } - public interface DoubleClickListener { + public interface DoubleClickListener + { public void onDoubleClick(SwipeLayout layout, boolean surface); } - private int dp2px(float dp){ + private int dp2px(float dp) + { return (int) (dp * getContext().getResources().getDisplayMetrics().density + 0.5f); } + + public List getDragEdges() + { + return mDragEdges; + } + + public void setDragEdges(List mDragEdges) + { + this.mDragEdges = mDragEdges; + currentDirectionIndex = 0; + populateIndexes(); + updateBottomViews(); + } + + public void setDragEdges(DragEdge... mDragEdges) + { + this.mDragEdges = new ArrayList(); + for (DragEdge e : mDragEdges) { + this.mDragEdges.add(e); + } + currentDirectionIndex = 0; + populateIndexes(); + updateBottomViews(); + } + + private void populateIndexes() + { + mLeftIndex = this.mDragEdges.indexOf(DragEdge.Left); + mRightIndex = this.mDragEdges.indexOf(DragEdge.Right); + mTopIndex = this.mDragEdges.indexOf(DragEdge.Top); + mBottomIndex = this.mDragEdges.indexOf(DragEdge.Bottom); + } + + private float getCurrentOffset() + { + if (mDragEdges.get(currentDirectionIndex) == DragEdge.Left) return mLeftEdgeSwipeOffset; + else if (mDragEdges.get(currentDirectionIndex) == DragEdge.Right) return mRightEdgeSwipeOffset; + else if (mDragEdges.get(currentDirectionIndex) == DragEdge.Top) return mTopEdgeSwipeOffset; + else return mLeftEdgeSwipeOffset; + } + + private void updateBottomViews() + { +// removeAllViews(); +// addView(getBottomViews().get(currentDirectionIndex)); +// addView(getSurfaceView()); +// getBottomViews().get(currentDirectionIndex).bringToFront(); +// getSurfaceView().bringToFront(); + if (mShowMode == ShowMode.PullOut) + layoutPullOut(); + else if (mShowMode == ShowMode.LayDown) layoutLayDown(); + + safeBottomView(); + + if (mOnLayoutListeners != null) for (int i = 0; i < mOnLayoutListeners.size(); i++) { + mOnLayoutListeners.get(i).onLayout(this); + } + } } diff --git a/library/src/main/res/values/attrs.xml b/library/src/main/res/values/attrs.xml index 9f69031c..c23e5a69 100644 --- a/library/src/main/res/values/attrs.xml +++ b/library/src/main/res/values/attrs.xml @@ -1,14 +1,16 @@ - - - - - + + + + + - - + + + + From c5d3e6743e068a129ac06ff6b57493c4afa1a04c Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 23 Dec 2014 10:19:01 +0530 Subject: [PATCH 02/16] fixed bug with layout not moving on swipe --- demo/src/main/java/com/daimajia/swipedemo/MyActivity.java | 2 +- library/src/main/java/com/daimajia/swipe/SwipeLayout.java | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/demo/src/main/java/com/daimajia/swipedemo/MyActivity.java b/demo/src/main/java/com/daimajia/swipedemo/MyActivity.java index 75483437..449a8f4d 100644 --- a/demo/src/main/java/com/daimajia/swipedemo/MyActivity.java +++ b/demo/src/main/java/com/daimajia/swipedemo/MyActivity.java @@ -27,7 +27,7 @@ protected void onCreate(Bundle savedInstanceState) { //sample1 sample1 = (SwipeLayout) findViewById(R.id.sample1); - sample1.setShowMode(SwipeLayout.ShowMode.LayDown); + sample1.setShowMode(SwipeLayout.ShowMode.PullOut); sample1.setDragEdges(SwipeLayout.DragEdge.Left, SwipeLayout.DragEdge.Right); Toast.makeText(this, sample1.getDragEdge() + " is the drag edge", Toast.LENGTH_LONG).show(); sample1.addRevealListener(R.id.delete, new SwipeLayout.OnRevealListener() { diff --git a/library/src/main/java/com/daimajia/swipe/SwipeLayout.java b/library/src/main/java/com/daimajia/swipe/SwipeLayout.java index 014d8fa4..2c6bc03f 100644 --- a/library/src/main/java/com/daimajia/swipe/SwipeLayout.java +++ b/library/src/main/java/com/daimajia/swipe/SwipeLayout.java @@ -904,6 +904,7 @@ else if (status == Status.Open) { float angle = Math.abs(distanceY / distanceX); angle = (float) Math.toDegrees(Math.atan(angle)); if (getOpenStatus() == Status.Close) { + int lastCurrentDirectionIndex = currentDirectionIndex; if (angle < 45) { if (mLeftIndex != -1 && distanceX > 0) { currentDirectionIndex = mLeftIndex; @@ -917,7 +918,9 @@ else if (status == Status.Open) { currentDirectionIndex = mBottomIndex; } } - updateBottomViews(); + if (lastCurrentDirectionIndex != currentDirectionIndex) { + updateBottomViews(); + } } boolean doNothing = false; From 14c3135029552f1547c0bf4f2cb0486a308bc681 Mon Sep 17 00:00:00 2001 From: HarshEvilGeek Date: Tue, 23 Dec 2014 10:23:28 +0530 Subject: [PATCH 03/16] setting sample1 to laydown mode --- demo/src/main/java/com/daimajia/swipedemo/MyActivity.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/demo/src/main/java/com/daimajia/swipedemo/MyActivity.java b/demo/src/main/java/com/daimajia/swipedemo/MyActivity.java index 449a8f4d..75483437 100644 --- a/demo/src/main/java/com/daimajia/swipedemo/MyActivity.java +++ b/demo/src/main/java/com/daimajia/swipedemo/MyActivity.java @@ -27,7 +27,7 @@ protected void onCreate(Bundle savedInstanceState) { //sample1 sample1 = (SwipeLayout) findViewById(R.id.sample1); - sample1.setShowMode(SwipeLayout.ShowMode.PullOut); + sample1.setShowMode(SwipeLayout.ShowMode.LayDown); sample1.setDragEdges(SwipeLayout.DragEdge.Left, SwipeLayout.DragEdge.Right); Toast.makeText(this, sample1.getDragEdge() + " is the drag edge", Toast.LENGTH_LONG).show(); sample1.addRevealListener(R.id.delete, new SwipeLayout.OnRevealListener() { From 4ab9c29b8c2fc6820ad8d57ffda8f8a8d9ca1164 Mon Sep 17 00:00:00 2001 From: HarshEvilGeek Date: Tue, 23 Dec 2014 10:44:15 +0530 Subject: [PATCH 04/16] Removing unnecessary toast --- demo/src/main/java/com/daimajia/swipedemo/MyActivity.java | 1 - 1 file changed, 1 deletion(-) diff --git a/demo/src/main/java/com/daimajia/swipedemo/MyActivity.java b/demo/src/main/java/com/daimajia/swipedemo/MyActivity.java index 75483437..3203a6b5 100644 --- a/demo/src/main/java/com/daimajia/swipedemo/MyActivity.java +++ b/demo/src/main/java/com/daimajia/swipedemo/MyActivity.java @@ -29,7 +29,6 @@ protected void onCreate(Bundle savedInstanceState) { sample1 = (SwipeLayout) findViewById(R.id.sample1); sample1.setShowMode(SwipeLayout.ShowMode.LayDown); sample1.setDragEdges(SwipeLayout.DragEdge.Left, SwipeLayout.DragEdge.Right); - Toast.makeText(this, sample1.getDragEdge() + " is the drag edge", Toast.LENGTH_LONG).show(); sample1.addRevealListener(R.id.delete, new SwipeLayout.OnRevealListener() { @Override public void onReveal(View child, SwipeLayout.DragEdge edge, float fraction, int distance) { From 046caa4a10a176bed0b210fd4952f8cf5012ced4 Mon Sep 17 00:00:00 2001 From: HarshEvilGeek Date: Tue, 23 Dec 2014 14:53:44 +0530 Subject: [PATCH 05/16] Fixing default direction --- library/src/main/java/com/daimajia/swipe/SwipeLayout.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/src/main/java/com/daimajia/swipe/SwipeLayout.java b/library/src/main/java/com/daimajia/swipe/SwipeLayout.java index 2c6bc03f..eabfed17 100644 --- a/library/src/main/java/com/daimajia/swipe/SwipeLayout.java +++ b/library/src/main/java/com/daimajia/swipe/SwipeLayout.java @@ -87,7 +87,7 @@ public SwipeLayout(Context context, AttributeSet attrs, int defStyle) mDragHelper = ViewDragHelper.create(this, mDragHelperCallback); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SwipeLayout); - int dragEdgeChoices = a.getInt(R.styleable.SwipeLayout_drag_edge, DragEdge.Right.ordinal()); + int dragEdgeChoices = a.getInt(R.styleable.SwipeLayout_drag_edge, DRAG_RIGHT); mLeftEdgeSwipeOffset = a.getDimension(R.styleable.SwipeLayout_leftEdgeSwipeOffset, 0); mRightEdgeSwipeOffset = a.getDimension(R.styleable.SwipeLayout_rightEdgeSwipeOffset, 0); mTopEdgeSwipeOffset = a.getDimension(R.styleable.SwipeLayout_topEdgeSwipeOffset, 0); From 7da3056130b66fa282059aa63491773ca7c57505 Mon Sep 17 00:00:00 2001 From: HarshEvilGeek Date: Wed, 24 Dec 2014 12:36:07 +0530 Subject: [PATCH 06/16] Formatting --- .../com/daimajia/swipedemo/MyActivity.java | 2 +- demo/src/main/res/layout/sample1.xml | 10 +- .../java/com/daimajia/swipe/SwipeLayout.java | 896 ++++++++---------- library/src/main/res/values/attrs.xml | 20 +- 4 files changed, 405 insertions(+), 523 deletions(-) diff --git a/demo/src/main/java/com/daimajia/swipedemo/MyActivity.java b/demo/src/main/java/com/daimajia/swipedemo/MyActivity.java index 3203a6b5..03ce69d7 100644 --- a/demo/src/main/java/com/daimajia/swipedemo/MyActivity.java +++ b/demo/src/main/java/com/daimajia/swipedemo/MyActivity.java @@ -135,7 +135,7 @@ public boolean onOptionsItemSelected(MenuItem item) { } else if (id == R.id.action_gridview) { startActivity(new Intent(this, GridViewExample.class)); return true; - } else if(id == R.id.action_nexted){ + } else if (id == R.id.action_nexted) { startActivity(new Intent(this, NestedExample.class)); return true; } diff --git a/demo/src/main/res/layout/sample1.xml b/demo/src/main/res/layout/sample1.xml index 9872c603..cb3a6fd6 100644 --- a/demo/src/main/res/layout/sample1.xml +++ b/demo/src/main/res/layout/sample1.xml @@ -2,7 +2,9 @@ + android:layout_width="match_parent" + android:layout_height="80dp"> + + + + + + + ()); + if (mRevealListeners.get(child) == null) + mRevealListeners.put(child, new ArrayList()); mRevealListeners.get(child).add(l); } @@ -196,20 +182,16 @@ public void addRevealListener(int childId, OnRevealListener l) /** * bind multiple views with an * {@link com.daimajia.swipe.SwipeLayout.OnRevealListener}. - * - * @param childIds - * the view id. - * @param l - * the {@link com.daimajia.swipe.SwipeLayout.OnRevealListener} + * + * @param childIds the view id. + * @param l the {@link com.daimajia.swipe.SwipeLayout.OnRevealListener} */ - public void addRevealListener(int[] childIds, OnRevealListener l) - { + public void addRevealListener(int[] childIds, OnRevealListener l) { for (int i : childIds) addRevealListener(i, l); } - public void removeRevealListener(int childId, OnRevealListener l) - { + public void removeRevealListener(int childId, OnRevealListener l) { View child = findViewById(childId); if (child == null) return; @@ -218,8 +200,7 @@ public void removeRevealListener(int childId, OnRevealListener l) if (mRevealListeners.containsKey(child)) mRevealListeners.get(child).remove(l); } - public void removeAllRevealListeners(int childId) - { + public void removeAllRevealListeners(int childId) { View child = findViewById(childId); if (child != null) { mRevealListeners.remove(child); @@ -230,132 +211,122 @@ public void removeAllRevealListeners(int childId) private ViewDragHelper.Callback mDragHelperCallback = new ViewDragHelper.Callback() { @Override - public int clampViewPositionHorizontal(View child, int left, int dx) - { + public int clampViewPositionHorizontal(View child, int left, int dx) { if (child == getSurfaceView()) { - switch (mDragEdges.get(currentDirectionIndex)) - { - case Top: - case Bottom: - return getPaddingLeft(); - case Left: - if (left < getPaddingLeft()) return getPaddingLeft(); - if (left > getPaddingLeft() + mDragDistance) return getPaddingLeft() + mDragDistance; - break; - case Right: - if (left > getPaddingLeft()) return getPaddingLeft(); - if (left < getPaddingLeft() - mDragDistance) return getPaddingLeft() - mDragDistance; - break; + switch (mDragEdges.get(currentDirectionIndex)) { + case Top: + case Bottom: + return getPaddingLeft(); + case Left: + if (left < getPaddingLeft()) return getPaddingLeft(); + if (left > getPaddingLeft() + mDragDistance) + return getPaddingLeft() + mDragDistance; + break; + case Right: + if (left > getPaddingLeft()) return getPaddingLeft(); + if (left < getPaddingLeft() - mDragDistance) + return getPaddingLeft() - mDragDistance; + break; } - } - else if (getBottomViews().get(currentDirectionIndex) == child) { + } else if (getBottomViews().get(currentDirectionIndex) == child) { - switch (mDragEdges.get(currentDirectionIndex)) - { - case Top: - case Bottom: - return getPaddingLeft(); - case Left: - if (mShowMode == ShowMode.PullOut) { - if (left > getPaddingLeft()) return getPaddingLeft(); - } - break; - case Right: - if (mShowMode == ShowMode.PullOut) { - if (left < getMeasuredWidth() - mDragDistance) { - return getMeasuredWidth() - mDragDistance; + switch (mDragEdges.get(currentDirectionIndex)) { + case Top: + case Bottom: + return getPaddingLeft(); + case Left: + if (mShowMode == ShowMode.PullOut) { + if (left > getPaddingLeft()) return getPaddingLeft(); } - } - break; + break; + case Right: + if (mShowMode == ShowMode.PullOut) { + if (left < getMeasuredWidth() - mDragDistance) { + return getMeasuredWidth() - mDragDistance; + } + } + break; } } return left; } @Override - public int clampViewPositionVertical(View child, int top, int dy) - { + public int clampViewPositionVertical(View child, int top, int dy) { if (child == getSurfaceView()) { - switch (mDragEdges.get(currentDirectionIndex)) - { - case Left: - case Right: - return getPaddingTop(); - case Top: - if (top < getPaddingTop()) return getPaddingTop(); - if (top > getPaddingTop() + mDragDistance) return getPaddingTop() + mDragDistance; - break; - case Bottom: - if (top < getPaddingTop() - mDragDistance) { - return getPaddingTop() - mDragDistance; - } - if (top > getPaddingTop()) { + switch (mDragEdges.get(currentDirectionIndex)) { + case Left: + case Right: return getPaddingTop(); - } - } - } - else { - switch (mDragEdges.get(currentDirectionIndex)) - { - case Left: - case Right: - return getPaddingTop(); - case Top: - if (mShowMode == ShowMode.PullOut) { - if (top > getPaddingTop()) return getPaddingTop(); - } - else { - if (getSurfaceView().getTop() + dy < getPaddingTop()) return getPaddingTop(); - if (getSurfaceView().getTop() + dy > getPaddingTop() + mDragDistance) + case Top: + if (top < getPaddingTop()) return getPaddingTop(); + if (top > getPaddingTop() + mDragDistance) return getPaddingTop() + mDragDistance; - } - break; - case Bottom: - if (mShowMode == ShowMode.PullOut) { - if (top < getMeasuredHeight() - mDragDistance) return getMeasuredHeight() - mDragDistance; - } - else { - if (getSurfaceView().getTop() + dy >= getPaddingTop()) return getPaddingTop(); - if (getSurfaceView().getTop() + dy <= getPaddingTop() - mDragDistance) + break; + case Bottom: + if (top < getPaddingTop() - mDragDistance) { return getPaddingTop() - mDragDistance; - } + } + if (top > getPaddingTop()) { + return getPaddingTop(); + } + } + } else { + switch (mDragEdges.get(currentDirectionIndex)) { + case Left: + case Right: + return getPaddingTop(); + case Top: + if (mShowMode == ShowMode.PullOut) { + if (top > getPaddingTop()) return getPaddingTop(); + } else { + if (getSurfaceView().getTop() + dy < getPaddingTop()) + return getPaddingTop(); + if (getSurfaceView().getTop() + dy > getPaddingTop() + mDragDistance) + return getPaddingTop() + mDragDistance; + } + break; + case Bottom: + if (mShowMode == ShowMode.PullOut) { + if (top < getMeasuredHeight() - mDragDistance) + return getMeasuredHeight() - mDragDistance; + } else { + if (getSurfaceView().getTop() + dy >= getPaddingTop()) + return getPaddingTop(); + if (getSurfaceView().getTop() + dy <= getPaddingTop() - mDragDistance) + return getPaddingTop() - mDragDistance; + } } } return top; } @Override - public boolean tryCaptureView(View child, int pointerId) - { + public boolean tryCaptureView(View child, int pointerId) { return child == getSurfaceView() || getBottomViews().contains(child); } @Override - public int getViewHorizontalDragRange(View child) - { + public int getViewHorizontalDragRange(View child) { return mDragDistance; } @Override - public int getViewVerticalDragRange(View child) - { + public int getViewVerticalDragRange(View child) { return mDragDistance; } @Override - public void onViewReleased(View releasedChild, float xvel, float yvel) - { + public void onViewReleased(View releasedChild, float xvel, float yvel) { super.onViewReleased(releasedChild, xvel, yvel); for (SwipeListener l : mSwipeListeners) l.onHandRelease(SwipeLayout.this, xvel, yvel); if (releasedChild == getSurfaceView()) { processSurfaceRelease(xvel, yvel); - } - else if (getBottomViews().contains(releasedChild)) { + } else if (getBottomViews().contains(releasedChild)) { if (getShowMode() == ShowMode.PullOut) { processBottomPullOutRelease(xvel, yvel); - } - else if (getShowMode() == ShowMode.LayDown) { + } else if (getShowMode() == ShowMode.LayDown) { processBottomLayDownMode(xvel, yvel); } } @@ -364,8 +335,7 @@ else if (getShowMode() == ShowMode.LayDown) { } @Override - public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) - { + public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) { int evLeft = getSurfaceView().getLeft(), evRight = getSurfaceView().getRight(), evTop = getSurfaceView() .getTop(), evBottom = getSurfaceView().getBottom(); if (changedView == getSurfaceView()) { @@ -377,14 +347,12 @@ public void onViewPositionChanged(View changedView, int left, int top, int dx, i else getBottomViews().get(currentDirectionIndex).offsetTopAndBottom(dy); } - } - else if (getBottomViews().contains(changedView)) { + } else if (getBottomViews().contains(changedView)) { if (mShowMode == ShowMode.PullOut) { getSurfaceView().offsetLeftAndRight(dx); getSurfaceView().offsetTopAndBottom(dy); - } - else { + } else { Rect rect = computeBottomLayDown(mDragEdges.get(currentDirectionIndex)); getBottomViews().get(currentDirectionIndex).layout(rect.left, rect.top, rect.right, rect.bottom); @@ -416,7 +384,7 @@ else if (mDragEdges.get(currentDirectionIndex) == DragEdge.Bottom && newTop > ge * the dispatchRevealEvent method may not always get accurate position, it * makes the view may not always get the event when the view is totally * show( fraction = 1), so , we need to calculate every time. - * + * * @param child * @param relativePosition * @param edge @@ -427,8 +395,7 @@ else if (mDragEdges.get(currentDirectionIndex) == DragEdge.Bottom && newTop > ge * @return */ protected boolean isViewTotallyFirstShowed(View child, Rect relativePosition, DragEdge edge, int surfaceLeft, - int surfaceTop, int surfaceRight, int surfaceBottom) - { + int surfaceTop, int surfaceRight, int surfaceBottom) { if (mShowEntirely.get(child)) return false; int childLeft = relativePosition.left; int childRight = relativePosition.right; @@ -440,8 +407,7 @@ protected boolean isViewTotallyFirstShowed(View child, Rect relativePosition, Dr || (edge == DragEdge.Left && surfaceLeft >= childRight) || (edge == DragEdge.Top && surfaceTop >= childBottom) || (edge == DragEdge.Bottom && surfaceBottom <= childTop)) r = true; - } - else if (getShowMode() == ShowMode.PullOut) { + } else if (getShowMode() == ShowMode.PullOut) { if ((edge == DragEdge.Right && childRight <= getWidth()) || (edge == DragEdge.Left && childLeft >= getPaddingLeft()) || (edge == DragEdge.Top && childTop >= getPaddingTop()) @@ -451,59 +417,54 @@ else if (getShowMode() == ShowMode.PullOut) { } protected boolean isViewShowing(View child, Rect relativePosition, DragEdge availableEdge, int surfaceLeft, - int surfaceTop, int surfaceRight, int surfaceBottom) - { + int surfaceTop, int surfaceRight, int surfaceBottom) { int childLeft = relativePosition.left; int childRight = relativePosition.right; int childTop = relativePosition.top; int childBottom = relativePosition.bottom; if (getShowMode() == ShowMode.LayDown) { - switch (availableEdge) - { - case Right: - if (surfaceRight > childLeft && surfaceRight <= childRight) { - return true; - } - break; - case Left: - if (surfaceLeft < childRight && surfaceLeft >= childLeft) { - return true; - } - break; - case Top: - if (surfaceTop >= childTop && surfaceTop < childBottom) { - return true; - } - break; - case Bottom: - if (surfaceBottom > childTop && surfaceBottom <= childBottom) { - return true; - } - break; + switch (availableEdge) { + case Right: + if (surfaceRight > childLeft && surfaceRight <= childRight) { + return true; + } + break; + case Left: + if (surfaceLeft < childRight && surfaceLeft >= childLeft) { + return true; + } + break; + case Top: + if (surfaceTop >= childTop && surfaceTop < childBottom) { + return true; + } + break; + case Bottom: + if (surfaceBottom > childTop && surfaceBottom <= childBottom) { + return true; + } + break; } - } - else if (getShowMode() == ShowMode.PullOut) { - switch (availableEdge) - { - case Right: - if (childLeft <= getWidth() && childRight > getWidth()) return true; - break; - case Left: - if (childRight >= getPaddingLeft() && childLeft < getPaddingLeft()) return true; - break; - case Top: - if (childTop < getPaddingTop() && childBottom >= getPaddingTop()) return true; - break; - case Bottom: - if (childTop < getHeight() && childTop >= getPaddingTop()) return true; - break; + } else if (getShowMode() == ShowMode.PullOut) { + switch (availableEdge) { + case Right: + if (childLeft <= getWidth() && childRight > getWidth()) return true; + break; + case Left: + if (childRight >= getPaddingLeft() && childLeft < getPaddingLeft()) return true; + break; + case Top: + if (childTop < getPaddingTop() && childBottom >= getPaddingTop()) return true; + break; + case Bottom: + if (childTop < getHeight() && childTop >= getPaddingTop()) return true; + break; } } return false; } - protected Rect getRelativePosition(View child) - { + protected Rect getRelativePosition(View child) { View t = child; Rect r = new Rect(t.getLeft(), t.getTop(), 0, 0); while (t.getParent() != null && t != getRootView()) { @@ -519,28 +480,23 @@ protected Rect getRelativePosition(View child) private int mEventCounter = 0; - protected void dispatchSwipeEvent(int surfaceLeft, int surfaceTop, int dx, int dy) - { + protected void dispatchSwipeEvent(int surfaceLeft, int surfaceTop, int dx, int dy) { DragEdge edge = getDragEdge(); boolean open = true; if (edge == DragEdge.Left) { if (dx < 0) open = false; - } - else if (edge == DragEdge.Right) { + } else if (edge == DragEdge.Right) { if (dx > 0) open = false; - } - else if (edge == DragEdge.Top) { + } else if (edge == DragEdge.Top) { if (dy < 0) open = false; - } - else if (edge == DragEdge.Bottom) { + } else if (edge == DragEdge.Bottom) { if (dy > 0) open = false; } dispatchSwipeEvent(surfaceLeft, surfaceTop, open); } - protected void dispatchSwipeEvent(int surfaceLeft, int surfaceTop, boolean open) - { + protected void dispatchSwipeEvent(int surfaceLeft, int surfaceTop, boolean open) { safeBottomView(); Status status = getOpenStatus(); @@ -550,8 +506,7 @@ protected void dispatchSwipeEvent(int surfaceLeft, int surfaceTop, boolean open) if (mEventCounter == 1) { if (open) { l.onStartOpen(this); - } - else { + } else { l.onStartClose(this); } } @@ -578,8 +533,7 @@ protected void dispatchSwipeEvent(int surfaceLeft, int surfaceTop, boolean open) /** * prevent bottom view get any touch event. Especially in LayDown mode. */ - private void safeBottomView() - { + private void safeBottomView() { Status status = getOpenStatus(); List bottoms = getBottomViews(); @@ -587,16 +541,14 @@ private void safeBottomView() for (ViewGroup bottom : bottoms) { if (bottom.getVisibility() != INVISIBLE) bottom.setVisibility(INVISIBLE); } - } - else { + } else { if (bottoms.get(currentDirectionIndex).getVisibility() != VISIBLE) bottoms.get(currentDirectionIndex).setVisibility(VISIBLE); } } protected void dispatchRevealEvent(final int surfaceLeft, final int surfaceTop, final int surfaceRight, - final int surfaceBottom) - { + final int surfaceBottom) { if (mRevealListeners.isEmpty()) return; for (Map.Entry> entry : mRevealListeners.entrySet()) { View child = entry.getKey(); @@ -607,45 +559,42 @@ protected void dispatchRevealEvent(final int surfaceLeft, final int surfaceTop, int distance = 0; float fraction = 0f; if (getShowMode() == ShowMode.LayDown) { - switch (mDragEdges.get(currentDirectionIndex)) - { - case Left: - distance = rect.left - surfaceLeft; - fraction = distance / (float) child.getWidth(); - break; - case Right: - distance = rect.right - surfaceRight; - fraction = distance / (float) child.getWidth(); - break; - case Top: - distance = rect.top - surfaceTop; - fraction = distance / (float) child.getHeight(); - break; - case Bottom: - distance = rect.bottom - surfaceBottom; - fraction = distance / (float) child.getHeight(); - break; + switch (mDragEdges.get(currentDirectionIndex)) { + case Left: + distance = rect.left - surfaceLeft; + fraction = distance / (float) child.getWidth(); + break; + case Right: + distance = rect.right - surfaceRight; + fraction = distance / (float) child.getWidth(); + break; + case Top: + distance = rect.top - surfaceTop; + fraction = distance / (float) child.getHeight(); + break; + case Bottom: + distance = rect.bottom - surfaceBottom; + fraction = distance / (float) child.getHeight(); + break; } - } - else if (getShowMode() == ShowMode.PullOut) { - switch (mDragEdges.get(currentDirectionIndex)) - { - case Left: - distance = rect.right - getPaddingLeft(); - fraction = distance / (float) child.getWidth(); - break; - case Right: - distance = rect.left - getWidth(); - fraction = distance / (float) child.getWidth(); - break; - case Top: - distance = rect.bottom - getPaddingTop(); - fraction = distance / (float) child.getHeight(); - break; - case Bottom: - distance = rect.top - getHeight(); - fraction = distance / (float) child.getHeight(); - break; + } else if (getShowMode() == ShowMode.PullOut) { + switch (mDragEdges.get(currentDirectionIndex)) { + case Left: + distance = rect.right - getPaddingLeft(); + fraction = distance / (float) child.getWidth(); + break; + case Right: + distance = rect.left - getWidth(); + fraction = distance / (float) child.getWidth(); + break; + case Top: + distance = rect.bottom - getPaddingTop(); + fraction = distance / (float) child.getHeight(); + break; + case Bottom: + distance = rect.top - getHeight(); + fraction = distance / (float) child.getHeight(); + break; } } @@ -664,7 +613,8 @@ else if (getShowMode() == ShowMode.PullOut) { if (mDragEdges.get(currentDirectionIndex) == DragEdge.Left || mDragEdges.get(currentDirectionIndex) == DragEdge.Right) l.onReveal(child, mDragEdges.get(currentDirectionIndex), 1, child.getWidth()); - else l.onReveal(child, mDragEdges.get(currentDirectionIndex), 1, child.getHeight()); + else + l.onReveal(child, mDragEdges.get(currentDirectionIndex), 1, child.getHeight()); } } @@ -672,8 +622,7 @@ else if (getShowMode() == ShowMode.PullOut) { } @Override - public void computeScroll() - { + public void computeScroll() { super.computeScroll(); if (mDragHelper.continueSettling(true)) { ViewCompat.postInvalidateOnAnimation(this); @@ -684,27 +633,23 @@ public void computeScroll() * {@link android.view.View.OnLayoutChangeListener} added in API 11. I need * to support it from API 8. */ - public interface OnLayout - { + public interface OnLayout { public void onLayout(SwipeLayout v); } private List mOnLayoutListeners; - public void addOnLayoutListener(OnLayout l) - { + public void addOnLayoutListener(OnLayout l) { if (mOnLayoutListeners == null) mOnLayoutListeners = new ArrayList(); mOnLayoutListeners.add(l); } - public void removeOnLayoutListener(OnLayout l) - { + public void removeOnLayoutListener(OnLayout l) { if (mOnLayoutListeners != null) mOnLayoutListeners.remove(l); } @Override - protected void onLayout(boolean changed, int l, int t, int r, int b) - { + protected void onLayout(boolean changed, int l, int t, int r, int b) { int childCount = getChildCount(); if (childCount != 1 + mDragEdges.size()) { throw new IllegalStateException("You need to have one surface view plus one view for each of your drag edges"); @@ -727,8 +672,7 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) } - void layoutPullOut() - { + void layoutPullOut() { Rect rect = computeSurfaceLayoutArea(false); getSurfaceView().layout(rect.left, rect.top, rect.right, rect.bottom); rect = computeBottomLayoutAreaViaSurface(ShowMode.PullOut, rect); @@ -736,8 +680,7 @@ void layoutPullOut() bringChildToFront(getSurfaceView()); } - void layoutLayDown() - { + void layoutLayDown() { Rect rect = computeSurfaceLayoutArea(false); getSurfaceView().layout(rect.left, rect.top, rect.right, rect.bottom); rect = computeBottomLayoutAreaViaSurface(ShowMode.LayDown, rect); @@ -746,8 +689,7 @@ void layoutLayDown() } @Override - protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) - { + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (mDragEdges.get(currentDirectionIndex) == DragEdge.Left @@ -761,8 +703,7 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) private boolean mTouchConsumedByChild = false; @Override - public boolean onInterceptTouchEvent(MotionEvent ev) - { + public boolean onInterceptTouchEvent(MotionEvent ev) { if (!isEnabled() || !isEnabledInAdapterView()) { return true; @@ -782,20 +723,18 @@ public boolean onInterceptTouchEvent(MotionEvent ev) // then let it do it. // int action = ev.getActionMasked(); - switch (action) - { - case MotionEvent.ACTION_DOWN: - Status status = getOpenStatus(); - if (status == Status.Close) { - mTouchConsumedByChild = childNeedHandleTouchEvent(getSurfaceView(), ev) != null; - } - else if (status == Status.Open) { - mTouchConsumedByChild = childNeedHandleTouchEvent(getBottomViews().get(currentDirectionIndex), ev) != null; - } - break; - case MotionEvent.ACTION_UP: - case MotionEvent.ACTION_CANCEL: - mTouchConsumedByChild = false; + switch (action) { + case MotionEvent.ACTION_DOWN: + Status status = getOpenStatus(); + if (status == Status.Close) { + mTouchConsumedByChild = childNeedHandleTouchEvent(getSurfaceView(), ev) != null; + } else if (status == Status.Open) { + mTouchConsumedByChild = childNeedHandleTouchEvent(getBottomViews().get(currentDirectionIndex), ev) != null; + } + break; + case MotionEvent.ACTION_UP: + case MotionEvent.ACTION_CANCEL: + mTouchConsumedByChild = false; } if (mTouchConsumedByChild) return false; @@ -804,13 +743,12 @@ else if (status == Status.Open) { /** * if the ViewGroup children want to handle this event. - * + * * @param v * @param event * @return */ - private View childNeedHandleTouchEvent(ViewGroup v, MotionEvent event) - { + private View childNeedHandleTouchEvent(ViewGroup v, MotionEvent event) { if (v == null) return null; if (v.onTouchEvent(event)) return v; @@ -820,8 +758,7 @@ private View childNeedHandleTouchEvent(ViewGroup v, MotionEvent event) if (child instanceof ViewGroup) { View grandChild = childNeedHandleTouchEvent((ViewGroup) child, event); if (grandChild != null) return grandChild; - } - else { + } else { if (childNeedHandleTouchEvent(v.getChildAt(i), event)) return v.getChildAt(i); } } @@ -830,13 +767,12 @@ private View childNeedHandleTouchEvent(ViewGroup v, MotionEvent event) /** * if the view (v) wants to handle this event. - * + * * @param v * @param event * @return */ - private boolean childNeedHandleTouchEvent(View v, MotionEvent event) - { + private boolean childNeedHandleTouchEvent(View v, MotionEvent event) { if (v == null) return false; int[] loc = new int[2]; @@ -854,8 +790,7 @@ private boolean childNeedHandleTouchEvent(View v, MotionEvent event) private float sX = -1, sY = -1; @Override - public boolean onTouchEvent(MotionEvent event) - { + public boolean onTouchEvent(MotionEvent event) { if (!isEnabledInAdapterView() || !isEnabled()) return true; if (!isSwipeEnabled()) return super.onTouchEvent(event); @@ -868,126 +803,123 @@ public boolean onTouchEvent(MotionEvent event) ViewGroup touching = null; if (status == Status.Close) { touching = getSurfaceView(); - } - else if (status == Status.Open) { + } else if (status == Status.Open) { touching = getBottomViews().get(currentDirectionIndex); } - switch (action) - { - case MotionEvent.ACTION_DOWN: - mDragHelper.processTouchEvent(event); - parent.requestDisallowInterceptTouchEvent(true); - - sX = event.getRawX(); - sY = event.getRawY(); - - if (touching != null) touching.setPressed(true); - - return true; - case MotionEvent.ACTION_MOVE: { - if (sX == -1 || sY == -1) { - // Trick: - // When in nested mode, we need to send a constructed - // ACTION_DOWN MotionEvent to mDragHelper, to help - // it initialize itself. - event.setAction(MotionEvent.ACTION_DOWN); + switch (action) { + case MotionEvent.ACTION_DOWN: mDragHelper.processTouchEvent(event); parent.requestDisallowInterceptTouchEvent(true); + sX = event.getRawX(); sY = event.getRawY(); + + if (touching != null) touching.setPressed(true); + return true; - } + case MotionEvent.ACTION_MOVE: { + if (sX == -1 || sY == -1) { + // Trick: + // When in nested mode, we need to send a constructed + // ACTION_DOWN MotionEvent to mDragHelper, to help + // it initialize itself. + event.setAction(MotionEvent.ACTION_DOWN); + mDragHelper.processTouchEvent(event); + parent.requestDisallowInterceptTouchEvent(true); + sX = event.getRawX(); + sY = event.getRawY(); + return true; + } - float distanceX = event.getRawX() - sX; - float distanceY = event.getRawY() - sY; - float angle = Math.abs(distanceY / distanceX); - angle = (float) Math.toDegrees(Math.atan(angle)); - if (getOpenStatus() == Status.Close) { - int lastCurrentDirectionIndex = currentDirectionIndex; - if (angle < 45) { - if (mLeftIndex != -1 && distanceX > 0) { - currentDirectionIndex = mLeftIndex; - } else if (mRightIndex != -1) { - currentDirectionIndex = mRightIndex; + float distanceX = event.getRawX() - sX; + float distanceY = event.getRawY() - sY; + float angle = Math.abs(distanceY / distanceX); + angle = (float) Math.toDegrees(Math.atan(angle)); + if (getOpenStatus() == Status.Close) { + int lastCurrentDirectionIndex = currentDirectionIndex; + if (angle < 45) { + if (mLeftIndex != -1 && distanceX > 0) { + currentDirectionIndex = mLeftIndex; + } else if (mRightIndex != -1) { + currentDirectionIndex = mRightIndex; + } + } else { + if (mTopIndex != -1 && distanceY < 0) { + currentDirectionIndex = mTopIndex; + } else if (mBottomIndex != -1) { + currentDirectionIndex = mBottomIndex; + } } - } else { - if (mTopIndex != -1 && distanceY < 0) { - currentDirectionIndex = mTopIndex; - } else if (mBottomIndex != -1) { - currentDirectionIndex = mBottomIndex; + if (lastCurrentDirectionIndex != currentDirectionIndex) { + updateBottomViews(); } } - if (lastCurrentDirectionIndex != currentDirectionIndex) { - updateBottomViews(); - } - } - boolean doNothing = false; - if (mDragEdges.get(currentDirectionIndex) == DragEdge.Right) { - boolean suitable = (status == Status.Open && distanceX > 0) - || (status == Status.Close && distanceX < 0); - suitable = suitable || (status == Status.Middle); + boolean doNothing = false; + if (mDragEdges.get(currentDirectionIndex) == DragEdge.Right) { + boolean suitable = (status == Status.Open && distanceX > 0) + || (status == Status.Close && distanceX < 0); + suitable = suitable || (status == Status.Middle); - if (angle > 30 || !suitable) { - doNothing = true; + if (angle > 30 || !suitable) { + doNothing = true; + } } - } - if (mDragEdges.get(currentDirectionIndex) == DragEdge.Left) { - boolean suitable = (status == Status.Open && distanceX < 0) - || (status == Status.Close && distanceX > 0); - suitable = suitable || status == Status.Middle; + if (mDragEdges.get(currentDirectionIndex) == DragEdge.Left) { + boolean suitable = (status == Status.Open && distanceX < 0) + || (status == Status.Close && distanceX > 0); + suitable = suitable || status == Status.Middle; - if (angle > 30 || !suitable) { - doNothing = true; + if (angle > 30 || !suitable) { + doNothing = true; + } } - } - if (mDragEdges.get(currentDirectionIndex) == DragEdge.Top) { - boolean suitable = (status == Status.Open && distanceY < 0) - || (status == Status.Close && distanceY > 0); - suitable = suitable || status == Status.Middle; + if (mDragEdges.get(currentDirectionIndex) == DragEdge.Top) { + boolean suitable = (status == Status.Open && distanceY < 0) + || (status == Status.Close && distanceY > 0); + suitable = suitable || status == Status.Middle; - if (angle < 60 || !suitable) { - doNothing = true; + if (angle < 60 || !suitable) { + doNothing = true; + } } - } - if (mDragEdges.get(currentDirectionIndex) == DragEdge.Bottom) { - boolean suitable = (status == Status.Open && distanceY > 0) - || (status == Status.Close && distanceY < 0); - suitable = suitable || status == Status.Middle; + if (mDragEdges.get(currentDirectionIndex) == DragEdge.Bottom) { + boolean suitable = (status == Status.Open && distanceY > 0) + || (status == Status.Close && distanceY < 0); + suitable = suitable || status == Status.Middle; - if (angle < 60 || !suitable) { - doNothing = true; + if (angle < 60 || !suitable) { + doNothing = true; + } } - } - if (doNothing) { - parent.requestDisallowInterceptTouchEvent(false); - return false; + if (doNothing) { + parent.requestDisallowInterceptTouchEvent(false); + return false; + } else { + if (touching != null) { + touching.setPressed(false); + } + parent.requestDisallowInterceptTouchEvent(true); + mDragHelper.processTouchEvent(event); + } + break; } - else { + case MotionEvent.ACTION_UP: + case MotionEvent.ACTION_CANCEL: { + sX = -1; + sY = -1; if (touching != null) { touching.setPressed(false); } + } + default: parent.requestDisallowInterceptTouchEvent(true); mDragHelper.processTouchEvent(event); - } - break; - } - case MotionEvent.ACTION_UP: - case MotionEvent.ACTION_CANCEL: { - sX = -1; - sY = -1; - if (touching != null) { - touching.setPressed(false); - } - } - default: - parent.requestDisallowInterceptTouchEvent(true); - mDragHelper.processTouchEvent(event); } return true; @@ -996,11 +928,10 @@ else if (status == Status.Open) { /** * if working in {@link android.widget.AdapterView}, we should response * {@link android.widget.Adapter} isEnable(int position). - * + * * @return true when item is enabled, else disabled. */ - private boolean isEnabledInAdapterView() - { + private boolean isEnabledInAdapterView() { AdapterView adapterView = getAdapterView(); boolean enable = true; if (adapterView != null) { @@ -1009,8 +940,7 @@ private boolean isEnabledInAdapterView() int p = adapterView.getPositionForView(SwipeLayout.this); if (adapter instanceof BaseAdapter) { enable = ((BaseAdapter) adapter).isEnabled(p); - } - else if (adapter instanceof ListAdapter) { + } else if (adapter instanceof ListAdapter) { enable = ((ListAdapter) adapter).isEnabled(p); } } @@ -1018,23 +948,19 @@ else if (adapter instanceof ListAdapter) { return enable; } - public void setSwipeEnabled(boolean enabled) - { + public void setSwipeEnabled(boolean enabled) { mSwipeEnabled = enabled; } - public boolean isSwipeEnabled() - { + public boolean isSwipeEnabled() { return mSwipeEnabled; } - private boolean insideAdapterView() - { + private boolean insideAdapterView() { return getAdapterView() != null; } - private AdapterView getAdapterView() - { + private AdapterView getAdapterView() { ViewParent t = getParent(); while (t != null) { if (t instanceof AdapterView) { @@ -1045,8 +971,7 @@ private AdapterView getAdapterView() return null; } - private void performAdapterViewItemClick(MotionEvent e) - { + private void performAdapterViewItemClick(MotionEvent e) { ViewParent t = getParent(); while (t != null) { if (t instanceof AdapterView) { @@ -1054,9 +979,8 @@ private void performAdapterViewItemClick(MotionEvent e) int p = view.getPositionForView(SwipeLayout.this); if (p != AdapterView.INVALID_POSITION && view.performItemClick(view.getChildAt(p - view.getFirstVisiblePosition()), p, view - .getAdapter().getItemId(p))) return; - } - else { + .getAdapter().getItemId(p))) return; + } else { if (t instanceof View && ((View) t).performClick()) return; } t = t.getParent(); @@ -1065,11 +989,9 @@ private void performAdapterViewItemClick(MotionEvent e) private GestureDetector gestureDetector = new GestureDetector(getContext(), new SwipeDetector()); - class SwipeDetector extends GestureDetector.SimpleOnGestureListener - { + class SwipeDetector extends GestureDetector.SimpleOnGestureListener { @Override - public boolean onDown(MotionEvent e) - { + public boolean onDown(MotionEvent e) { return true; } @@ -1079,13 +1001,12 @@ public boolean onDown(MotionEvent e) * {@link android.widget.GridView} etc.) It will manually call * {@link android.widget.AdapterView}.performItemClick, * performItemLongClick. - * + * * @param e * @return */ @Override - public boolean onSingleTapUp(MotionEvent e) - { + public boolean onSingleTapUp(MotionEvent e) { if (mDoubleClickListener == null) { performAdapterViewItemClick(e); } @@ -1093,8 +1014,7 @@ public boolean onSingleTapUp(MotionEvent e) } @Override - public boolean onSingleTapConfirmed(MotionEvent e) - { + public boolean onSingleTapConfirmed(MotionEvent e) { if (mDoubleClickListener != null) { performAdapterViewItemClick(e); } @@ -1102,14 +1022,12 @@ public boolean onSingleTapConfirmed(MotionEvent e) } @Override - public void onLongPress(MotionEvent e) - { + public void onLongPress(MotionEvent e) { performLongClick(); } @Override - public boolean onDoubleTap(MotionEvent e) - { + public boolean onDoubleTap(MotionEvent e) { if (mDoubleClickListener != null) { View target; ViewGroup bottom = getBottomViews().get(currentDirectionIndex); @@ -1117,8 +1035,7 @@ public boolean onDoubleTap(MotionEvent e) if (e.getX() > bottom.getLeft() && e.getX() < bottom.getRight() && e.getY() > bottom.getTop() && e.getY() < bottom.getBottom()) { target = bottom; - } - else { + } else { target = surface; } mDoubleClickListener.onDoubleClick(SwipeLayout.this, target == surface); @@ -1127,8 +1044,7 @@ public boolean onDoubleTap(MotionEvent e) } } - public void setDragEdge(DragEdge dragEdge) - { + public void setDragEdge(DragEdge dragEdge) { mDragEdges = new ArrayList(); mDragEdges.add(dragEdge); currentDirectionIndex = 0; @@ -1140,11 +1056,10 @@ public void setDragEdge(DragEdge dragEdge) /** * set the drag distance, it will force set the bottom view's width or * height via this value. - * + * * @param max */ - public void setDragDistance(int max) - { + public void setDragDistance(int max) { if (max < 0) throw new IllegalArgumentException("Drag distance can not be < 0"); mDragDistance = dp2px(max); requestLayout(); @@ -1154,37 +1069,31 @@ public void setDragDistance(int max) * There are 2 diffirent show mode. * {@link com.daimajia.swipe.SwipeLayout.ShowMode}.PullOut and * {@link com.daimajia.swipe.SwipeLayout.ShowMode}.LayDown. - * + * * @param mode */ - public void setShowMode(ShowMode mode) - { + public void setShowMode(ShowMode mode) { mShowMode = mode; requestLayout(); } - public DragEdge getDragEdge() - { + public DragEdge getDragEdge() { return mDragEdges.get(currentDirectionIndex); } - public int getDragDistance() - { + public int getDragDistance() { return mDragDistance; } - public ShowMode getShowMode() - { + public ShowMode getShowMode() { return mShowMode; } - public ViewGroup getSurfaceView() - { + public ViewGroup getSurfaceView() { return (ViewGroup) getChildAt(getChildCount() - 1); } - public List getBottomViews() - { + public List getBottomViews() { List lvg = new ArrayList(); for (int i = 0; i < (getChildCount() - 1); i++) { lvg.add((ViewGroup) getChildAt(i)); @@ -1192,8 +1101,7 @@ public List getBottomViews() return lvg; } - public enum Status - { + public enum Status { Middle, Open, Close @@ -1201,12 +1109,11 @@ public enum Status /** * get the open status. - * + * * @return {@link com.daimajia.swipe.SwipeLayout.Status} Open , Close or - * Middle. + * Middle. */ - public Status getOpenStatus() - { + public Status getOpenStatus() { int surfaceLeft = getSurfaceView().getLeft(); int surfaceTop = getSurfaceView().getTop(); if (surfaceLeft == getPaddingLeft() && surfaceTop == getPaddingTop()) return Status.Close; @@ -1220,12 +1127,11 @@ public Status getOpenStatus() /** * Process the surface release event. - * + * * @param xvel * @param yvel */ - private void processSurfaceRelease(float xvel, float yvel) - { + private void processSurfaceRelease(float xvel, float yvel) { if (xvel == 0 && getOpenStatus() == Status.Middle) close(); if (mDragEdges.get(currentDirectionIndex) == DragEdge.Left @@ -1240,8 +1146,7 @@ private void processSurfaceRelease(float xvel, float yvel) close(); else open(); } - } - else { + } else { if (yvel > 0) { if (mDragEdges.get(currentDirectionIndex) == DragEdge.Top) open(); @@ -1257,12 +1162,11 @@ private void processSurfaceRelease(float xvel, float yvel) /** * process bottom (PullOut mode) hand release event. - * + * * @param xvel * @param yvel */ - private void processBottomPullOutRelease(float xvel, float yvel) - { + private void processBottomPullOutRelease(float xvel, float yvel) { if (xvel == 0 && getOpenStatus() == Status.Middle) close(); @@ -1278,8 +1182,7 @@ private void processBottomPullOutRelease(float xvel, float yvel) close(); else open(); } - } - else { + } else { if (yvel > 0) { if (mDragEdges.get(currentDirectionIndex) == DragEdge.Top) open(); @@ -1296,12 +1199,11 @@ private void processBottomPullOutRelease(float xvel, float yvel) /** * process bottom (LayDown mode) hand release event. - * + * * @param xvel * @param yvel */ - private void processBottomLayDownMode(float xvel, float yvel) - { + private void processBottomLayDownMode(float xvel, float yvel) { if (xvel == 0 && getOpenStatus() == Status.Middle) close(); @@ -1311,7 +1213,8 @@ private void processBottomLayDownMode(float xvel, float yvel) if (xvel > 0 && mDragEdges.get(currentDirectionIndex) == DragEdge.Left) l += mDragDistance; if (yvel > 0 && mDragEdges.get(currentDirectionIndex) == DragEdge.Top) t += mDragDistance; - if (yvel < 0 && mDragEdges.get(currentDirectionIndex) == DragEdge.Bottom) t -= mDragDistance; + if (yvel < 0 && mDragEdges.get(currentDirectionIndex) == DragEdge.Bottom) + t -= mDragDistance; mDragHelper.smoothSlideViewTo(getSurfaceView(), l, t); invalidate(); @@ -1320,25 +1223,21 @@ private void processBottomLayDownMode(float xvel, float yvel) /** * smoothly open surface. */ - public void open() - { + public void open() { open(true, true); } - public void open(boolean smooth) - { + public void open(boolean smooth) { open(smooth, true); } - public void open(boolean smooth, boolean notify) - { + public void open(boolean smooth, boolean notify) { ViewGroup surface = getSurfaceView(), bottom = getBottomViews().get(currentDirectionIndex); int dx, dy; Rect rect = computeSurfaceLayoutArea(true); if (smooth) { mDragHelper.smoothSlideViewTo(getSurfaceView(), rect.left, rect.top); - } - else { + } else { dx = rect.left - surface.getLeft(); dy = rect.top - surface.getTop(); surface.layout(rect.left, rect.top, rect.right, rect.bottom); @@ -1349,8 +1248,7 @@ public void open(boolean smooth, boolean notify) if (notify) { dispatchRevealEvent(rect.left, rect.top, rect.right, rect.bottom); dispatchSwipeEvent(rect.left, rect.top, dx, dy); - } - else { + } else { safeBottomView(); } } @@ -1360,26 +1258,21 @@ public void open(boolean smooth, boolean notify) /** * smoothly close surface. */ - public void close() - { + public void close() { close(true, true); } - public void close(boolean smooth) - { + public void close(boolean smooth) { close(smooth, true); } /** * close surface - * - * @param smooth - * smoothly or not. - * @param notify - * if notify all the listeners. + * + * @param smooth smoothly or not. + * @param notify if notify all the listeners. */ - public void close(boolean smooth, boolean notify) - { + public void close(boolean smooth, boolean notify) { ViewGroup surface = getSurfaceView(); int dx, dy; if (smooth) @@ -1392,21 +1285,18 @@ public void close(boolean smooth, boolean notify) if (notify) { dispatchRevealEvent(rect.left, rect.top, rect.right, rect.bottom); dispatchSwipeEvent(rect.left, rect.top, dx, dy); - } - else { + } else { safeBottomView(); } } invalidate(); } - public void toggle() - { + public void toggle() { toggle(true); } - public void toggle(boolean smooth) - { + public void toggle(boolean smooth) { if (getOpenStatus() == Status.Open) close(smooth); else if (getOpenStatus() == Status.Close) open(smooth); @@ -1414,13 +1304,11 @@ public void toggle(boolean smooth) /** * a helper function to compute the Rect area that surface will hold in. - * - * @param open - * open status or close status. + * + * @param open open status or close status. * @return */ - private Rect computeSurfaceLayoutArea(boolean open) - { + private Rect computeSurfaceLayoutArea(boolean open) { int l = getPaddingLeft(), t = getPaddingTop(); if (open) { if (mDragEdges.get(currentDirectionIndex) == DragEdge.Left) @@ -1434,8 +1322,7 @@ else if (mDragEdges.get(currentDirectionIndex) == DragEdge.Top) return new Rect(l, t, l + getMeasuredWidth(), t + getMeasuredHeight()); } - private Rect computeBottomLayoutAreaViaSurface(ShowMode mode, Rect surfaceArea) - { + private Rect computeBottomLayoutAreaViaSurface(ShowMode mode, Rect surfaceArea) { Rect rect = surfaceArea; int bl = rect.left, bt = rect.top, br = rect.right, bb = rect.bottom; @@ -1451,13 +1338,11 @@ else if (mDragEdges.get(currentDirectionIndex) == DragEdge.Top) if (mDragEdges.get(currentDirectionIndex) == DragEdge.Left || mDragEdges.get(currentDirectionIndex) == DragEdge.Right) { bb = rect.bottom; br = bl + getBottomViews().get(currentDirectionIndex).getMeasuredWidth(); - } - else { + } else { bb = bt + getBottomViews().get(currentDirectionIndex).getMeasuredHeight(); br = rect.right; } - } - else if (mode == ShowMode.LayDown) { + } else if (mode == ShowMode.LayDown) { if (mDragEdges.get(currentDirectionIndex) == DragEdge.Left) br = bl + mDragDistance; else if (mDragEdges.get(currentDirectionIndex) == DragEdge.Right) @@ -1471,57 +1356,48 @@ else if (mDragEdges.get(currentDirectionIndex) == DragEdge.Top) } - private Rect computeBottomLayDown(DragEdge dragEdge) - { + private Rect computeBottomLayDown(DragEdge dragEdge) { int bl = getPaddingLeft(), bt = getPaddingTop(); int br, bb; if (dragEdge == DragEdge.Right) { bl = getMeasuredWidth() - mDragDistance; - } - else if (dragEdge == DragEdge.Bottom) { + } else if (dragEdge == DragEdge.Bottom) { bt = getMeasuredHeight() - mDragDistance; } if (dragEdge == DragEdge.Left || dragEdge == DragEdge.Right) { br = bl + mDragDistance; bb = bt + getMeasuredHeight(); - } - else { + } else { br = bl + getMeasuredWidth(); bb = bt + mDragDistance; } return new Rect(bl, bt, br, bb); } - public void setOnDoubleClickListener(DoubleClickListener doubleClickListener) - { + public void setOnDoubleClickListener(DoubleClickListener doubleClickListener) { mDoubleClickListener = doubleClickListener; } - public interface DoubleClickListener - { + public interface DoubleClickListener { public void onDoubleClick(SwipeLayout layout, boolean surface); } - private int dp2px(float dp) - { + private int dp2px(float dp) { return (int) (dp * getContext().getResources().getDisplayMetrics().density + 0.5f); } - public List getDragEdges() - { + public List getDragEdges() { return mDragEdges; } - public void setDragEdges(List mDragEdges) - { + public void setDragEdges(List mDragEdges) { this.mDragEdges = mDragEdges; currentDirectionIndex = 0; populateIndexes(); updateBottomViews(); } - public void setDragEdges(DragEdge... mDragEdges) - { + public void setDragEdges(DragEdge... mDragEdges) { this.mDragEdges = new ArrayList(); for (DragEdge e : mDragEdges) { this.mDragEdges.add(e); @@ -1531,24 +1407,22 @@ public void setDragEdges(DragEdge... mDragEdges) updateBottomViews(); } - private void populateIndexes() - { + private void populateIndexes() { mLeftIndex = this.mDragEdges.indexOf(DragEdge.Left); mRightIndex = this.mDragEdges.indexOf(DragEdge.Right); mTopIndex = this.mDragEdges.indexOf(DragEdge.Top); mBottomIndex = this.mDragEdges.indexOf(DragEdge.Bottom); } - private float getCurrentOffset() - { + private float getCurrentOffset() { if (mDragEdges.get(currentDirectionIndex) == DragEdge.Left) return mLeftEdgeSwipeOffset; - else if (mDragEdges.get(currentDirectionIndex) == DragEdge.Right) return mRightEdgeSwipeOffset; + else if (mDragEdges.get(currentDirectionIndex) == DragEdge.Right) + return mRightEdgeSwipeOffset; else if (mDragEdges.get(currentDirectionIndex) == DragEdge.Top) return mTopEdgeSwipeOffset; else return mLeftEdgeSwipeOffset; } - private void updateBottomViews() - { + private void updateBottomViews() { // removeAllViews(); // addView(getBottomViews().get(currentDirectionIndex)); // addView(getSurfaceView()); diff --git a/library/src/main/res/values/attrs.xml b/library/src/main/res/values/attrs.xml index c23e5a69..249b944e 100644 --- a/library/src/main/res/values/attrs.xml +++ b/library/src/main/res/values/attrs.xml @@ -2,18 +2,18 @@ - - - - + + + + - - - - + + + + - - + + \ No newline at end of file From df48a3c3871ce429b1604b67ea41418ca85e78a5 Mon Sep 17 00:00:00 2001 From: noinnion Date: Fri, 26 Dec 2014 10:16:07 +0100 Subject: [PATCH 07/16] add touchSlop for swiping --- .../java/com/daimajia/swipe/SwipeLayout.java | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/library/src/main/java/com/daimajia/swipe/SwipeLayout.java b/library/src/main/java/com/daimajia/swipe/SwipeLayout.java index 5addbc06..c73b131d 100644 --- a/library/src/main/java/com/daimajia/swipe/SwipeLayout.java +++ b/library/src/main/java/com/daimajia/swipe/SwipeLayout.java @@ -10,6 +10,7 @@ import android.view.GestureDetector; import android.view.MotionEvent; import android.view.View; +import android.view.ViewConfiguration; import android.view.ViewGroup; import android.view.ViewParent; import android.widget.Adapter; @@ -30,6 +31,8 @@ public class SwipeLayout extends FrameLayout { private static final int DRAG_TOP = 4; private static final int DRAG_BOTTOM = 8; + private int mTouchSlop; + private int mLeftIndex; private int mRightIndex; private int mTopIndex; @@ -81,6 +84,7 @@ public SwipeLayout(Context context, AttributeSet attrs) { public SwipeLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); mDragHelper = ViewDragHelper.create(this, mDragHelperCallback); + mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop(); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SwipeLayout); int dragEdgeChoices = a.getInt(R.styleable.SwipeLayout_drag_edge, DRAG_RIGHT); @@ -839,15 +843,15 @@ public boolean onTouchEvent(MotionEvent event) { if (getOpenStatus() == Status.Close) { int lastCurrentDirectionIndex = currentDirectionIndex; if (angle < 45) { - if (mLeftIndex != -1 && distanceX > 0) { + if (mLeftIndex != -1 && distanceX > mTouchSlop) { currentDirectionIndex = mLeftIndex; - } else if (mRightIndex != -1) { + } else if (mRightIndex != -1 && distanceX < -mTouchSlop) { currentDirectionIndex = mRightIndex; } } else { - if (mTopIndex != -1 && distanceY < 0) { + if (mTopIndex != -1 && distanceY < -mTouchSlop) { currentDirectionIndex = mTopIndex; - } else if (mBottomIndex != -1) { + } else if (mBottomIndex != -1 && distanceY > mTouchSlop) { currentDirectionIndex = mBottomIndex; } } @@ -858,8 +862,8 @@ public boolean onTouchEvent(MotionEvent event) { boolean doNothing = false; if (mDragEdges.get(currentDirectionIndex) == DragEdge.Right) { - boolean suitable = (status == Status.Open && distanceX > 0) - || (status == Status.Close && distanceX < 0); + boolean suitable = (status == Status.Open && distanceX > mTouchSlop) + || (status == Status.Close && distanceX < -mTouchSlop); suitable = suitable || (status == Status.Middle); if (angle > 30 || !suitable) { @@ -868,8 +872,8 @@ public boolean onTouchEvent(MotionEvent event) { } if (mDragEdges.get(currentDirectionIndex) == DragEdge.Left) { - boolean suitable = (status == Status.Open && distanceX < 0) - || (status == Status.Close && distanceX > 0); + boolean suitable = (status == Status.Open && distanceX < -mTouchSlop) + || (status == Status.Close && distanceX > mTouchSlop); suitable = suitable || status == Status.Middle; if (angle > 30 || !suitable) { @@ -878,8 +882,8 @@ public boolean onTouchEvent(MotionEvent event) { } if (mDragEdges.get(currentDirectionIndex) == DragEdge.Top) { - boolean suitable = (status == Status.Open && distanceY < 0) - || (status == Status.Close && distanceY > 0); + boolean suitable = (status == Status.Open && distanceY < -mTouchSlop) + || (status == Status.Close && distanceY > mTouchSlop); suitable = suitable || status == Status.Middle; if (angle < 60 || !suitable) { @@ -888,8 +892,8 @@ public boolean onTouchEvent(MotionEvent event) { } if (mDragEdges.get(currentDirectionIndex) == DragEdge.Bottom) { - boolean suitable = (status == Status.Open && distanceY > 0) - || (status == Status.Close && distanceY < 0); + boolean suitable = (status == Status.Open && distanceY > mTouchSlop) + || (status == Status.Close && distanceY < -mTouchSlop); suitable = suitable || status == Status.Middle; if (angle < 60 || !suitable) { From 6fcf27d4cec75b59afb5ffd9fe919a6b775d4083 Mon Sep 17 00:00:00 2001 From: HarshEvilGeek Date: Wed, 11 Feb 2015 13:53:56 +0530 Subject: [PATCH 08/16] fixing touchslop changes --- library/src/main/java/com/daimajia/swipe/SwipeLayout.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/library/src/main/java/com/daimajia/swipe/SwipeLayout.java b/library/src/main/java/com/daimajia/swipe/SwipeLayout.java index c73b131d..15377fa6 100644 --- a/library/src/main/java/com/daimajia/swipe/SwipeLayout.java +++ b/library/src/main/java/com/daimajia/swipe/SwipeLayout.java @@ -843,15 +843,15 @@ public boolean onTouchEvent(MotionEvent event) { if (getOpenStatus() == Status.Close) { int lastCurrentDirectionIndex = currentDirectionIndex; if (angle < 45) { - if (mLeftIndex != -1 && distanceX > mTouchSlop) { + if (mLeftIndex != -1 && distanceX > 0) { currentDirectionIndex = mLeftIndex; - } else if (mRightIndex != -1 && distanceX < -mTouchSlop) { + } else if (mRightIndex != -1) { currentDirectionIndex = mRightIndex; } } else { - if (mTopIndex != -1 && distanceY < -mTouchSlop) { + if (mTopIndex != -1 && distanceY < 0) { currentDirectionIndex = mTopIndex; - } else if (mBottomIndex != -1 && distanceY > mTouchSlop) { + } else if (mBottomIndex != -1) { currentDirectionIndex = mBottomIndex; } } From f8350fce20b0a55feeedbe157767c86aa28143f1 Mon Sep 17 00:00:00 2001 From: HarshEvilGeek Date: Wed, 11 Feb 2015 14:51:05 +0530 Subject: [PATCH 09/16] adding support for the user to pass the ids of bottom views --- .../com/daimajia/swipedemo/MyActivity.java | 2 + demo/src/main/res/layout/sample1.xml | 1 + .../java/com/daimajia/swipe/SwipeLayout.java | 68 +++++++++++++++++-- 3 files changed, 67 insertions(+), 4 deletions(-) diff --git a/demo/src/main/java/com/daimajia/swipedemo/MyActivity.java b/demo/src/main/java/com/daimajia/swipedemo/MyActivity.java index 03ce69d7..f006a86e 100644 --- a/demo/src/main/java/com/daimajia/swipedemo/MyActivity.java +++ b/demo/src/main/java/com/daimajia/swipedemo/MyActivity.java @@ -29,6 +29,8 @@ protected void onCreate(Bundle savedInstanceState) { sample1 = (SwipeLayout) findViewById(R.id.sample1); sample1.setShowMode(SwipeLayout.ShowMode.LayDown); sample1.setDragEdges(SwipeLayout.DragEdge.Left, SwipeLayout.DragEdge.Right); + // When using multiple drag edges it's a good idea to pass the ids of the views that you're using for the left, right, top bottom views (-1 if you're not using a particular view) + sample1.setBottomViewIds(R.id.bottom_wrapper, R.id.bottom_wrapper_2, -1, -1); sample1.addRevealListener(R.id.delete, new SwipeLayout.OnRevealListener() { @Override public void onReveal(View child, SwipeLayout.DragEdge edge, float fraction, int distance) { diff --git a/demo/src/main/res/layout/sample1.xml b/demo/src/main/res/layout/sample1.xml index cb3a6fd6..cc989aad 100644 --- a/demo/src/main/res/layout/sample1.xml +++ b/demo/src/main/res/layout/sample1.xml @@ -35,6 +35,7 @@ diff --git a/library/src/main/java/com/daimajia/swipe/SwipeLayout.java b/library/src/main/java/com/daimajia/swipe/SwipeLayout.java index 15377fa6..f8199be1 100644 --- a/library/src/main/java/com/daimajia/swipe/SwipeLayout.java +++ b/library/src/main/java/com/daimajia/swipe/SwipeLayout.java @@ -6,7 +6,6 @@ import android.support.v4.view.ViewCompat; import android.support.v4.widget.ViewDragHelper; import android.util.AttributeSet; -import android.util.Log; import android.view.GestureDetector; import android.view.MotionEvent; import android.view.View; @@ -50,6 +49,9 @@ public class SwipeLayout extends FrameLayout { private float mTopEdgeSwipeOffset; private float mBottomEdgeSwipeOffset; + private Map mBottomViewIdMap = new HashMap(); + private boolean mBottomViewIdsSet = false; + private List mSwipeListeners = new ArrayList(); private List mSwipeDeniers = new ArrayList(); private Map> mRevealListeners = new HashMap>(); @@ -1099,12 +1101,70 @@ public ViewGroup getSurfaceView() { public List getBottomViews() { List lvg = new ArrayList(); - for (int i = 0; i < (getChildCount() - 1); i++) { - lvg.add((ViewGroup) getChildAt(i)); + // If the user has provided a map for views to + if (mBottomViewIdsSet) { + if (mDragEdges.contains(DragEdge.Left)) { + lvg.add(mLeftIndex, ((ViewGroup) findViewById(mBottomViewIdMap.get(DragEdge.Left)))); + } + if (mDragEdges.contains(DragEdge.Right)) { + lvg.add(mRightIndex, ((ViewGroup) findViewById(mBottomViewIdMap.get(DragEdge.Right)))); + } + if (mDragEdges.contains(DragEdge.Top)) { + lvg.add(mTopIndex, ((ViewGroup) findViewById(mBottomViewIdMap.get(DragEdge.Top)))); + } + if (mDragEdges.contains(DragEdge.Bottom)) { + lvg.add(mBottomIndex, ((ViewGroup) findViewById(mBottomViewIdMap.get(DragEdge.Bottom)))); + } + } + // Default behaviour is to simply use the first n-1 children in the order they're listed in the layout + // and return them in + else { + for (int i = 0; i < (getChildCount() - 1); i++) { + lvg.add((ViewGroup) getChildAt(i)); + } } return lvg; } + // Pass the id of the view if set, otherwise pass -1 + public void setBottomViewIds (int left, int right, int top, int bottom) { + if (mDragEdges.contains(DragEdge.Left)) { + if (left == -1) { + mBottomViewIdsSet = false; + } + else { + mBottomViewIdMap.put(DragEdge.Left, left); + mBottomViewIdsSet = true; + } + } + if (mDragEdges.contains(DragEdge.Right)) { + if (right == -1) { + mBottomViewIdsSet = false; + } + else { + mBottomViewIdMap.put(DragEdge.Right, right); + mBottomViewIdsSet = true; + } + } + if (mDragEdges.contains(DragEdge.Top)) { + if (top == -1) { + mBottomViewIdsSet = false; + } + else { + mBottomViewIdMap.put(DragEdge.Top, top); + mBottomViewIdsSet = true; + } + } + if (mDragEdges.contains(DragEdge.Bottom)) { + if (bottom == -1) { + mBottomViewIdsSet = false; + } + else { + mBottomViewIdMap.put(DragEdge.Bottom, bottom); + mBottomViewIdsSet = true; + } + } + } public enum Status { Middle, Open, @@ -1423,7 +1483,7 @@ private float getCurrentOffset() { else if (mDragEdges.get(currentDirectionIndex) == DragEdge.Right) return mRightEdgeSwipeOffset; else if (mDragEdges.get(currentDirectionIndex) == DragEdge.Top) return mTopEdgeSwipeOffset; - else return mLeftEdgeSwipeOffset; + else return mBottomEdgeSwipeOffset; } private void updateBottomViews() { From ea5a0156dc7a3a13e53d97908a0557203d814924 Mon Sep 17 00:00:00 2001 From: HarshEvilGeek Date: Wed, 11 Feb 2015 17:53:10 +0530 Subject: [PATCH 10/16] fixing bug from horizontal swipe offset and adding preliminary code to restrict particular drag edges --- demo/build.gradle | 1 - demo/src/main/res/layout/listview_item.xml | 3 +- .../src/main/res/layout/recyclerview_item.xml | 3 +- .../java/com/daimajia/swipe/SwipeLayout.java | 36 +++++++++++++++++++ 4 files changed, 40 insertions(+), 3 deletions(-) diff --git a/demo/build.gradle b/demo/build.gradle index 2b122cd6..586f375b 100644 --- a/demo/build.gradle +++ b/demo/build.gradle @@ -7,7 +7,6 @@ repositories { android { compileSdkVersion Integer.parseInt(project.ANDROID_BUILD_SDK_VERSION) buildToolsVersion project.ANDROID_BUILD_TOOLS_VERSION - defaultConfig { minSdkVersion Integer.parseInt(project.ANDROID_BUILD_MIN_SDK_VERSION) targetSdkVersion Integer.parseInt(project.ANDROID_BUILD_TARGET_SDK_VERSION) diff --git a/demo/src/main/res/layout/listview_item.xml b/demo/src/main/res/layout/listview_item.xml index 092f7868..89620277 100644 --- a/demo/src/main/res/layout/listview_item.xml +++ b/demo/src/main/res/layout/listview_item.xml @@ -5,7 +5,8 @@ android:id="@+id/swipe" android:layout_width="match_parent" android:layout_height="wrap_content" - swipe:horizontalSwipeOffset="0dp"> + swipe:leftEdgeSwipeOffset="0dp" + swipe:rightEdgeSwipeOffset="0dp"> + swipe:leftEdgeSwipeOffset="0dp" + swipe:rightEdgeSwipeOffset="0dp"> Date: Wed, 11 Feb 2015 18:25:31 +0530 Subject: [PATCH 11/16] Adding support for enabling and disabling particular swipe edges. One bug fix from merge --- .../java/com/daimajia/swipe/SwipeLayout.java | 187 +++++++++--------- 1 file changed, 98 insertions(+), 89 deletions(-) diff --git a/library/src/main/java/com/daimajia/swipe/SwipeLayout.java b/library/src/main/java/com/daimajia/swipe/SwipeLayout.java index 969a6ca8..2bdb74cc 100644 --- a/library/src/main/java/com/daimajia/swipe/SwipeLayout.java +++ b/library/src/main/java/com/daimajia/swipe/SwipeLayout.java @@ -37,7 +37,7 @@ public class SwipeLayout extends FrameLayout { private int mTopIndex; private int mBottomIndex; - private int currentDirectionIndex = 0; + private int mCurrentDirectionIndex = 0; private ViewDragHelper mDragHelper; private int mDragDistance = 0; @@ -223,7 +223,7 @@ public void removeAllRevealListeners(int childId) { @Override public int clampViewPositionHorizontal(View child, int left, int dx) { if (child == getSurfaceView()) { - switch (mDragEdges.get(currentDirectionIndex)) { + switch (mDragEdges.get(mCurrentDirectionIndex)) { case Top: case Bottom: return getPaddingLeft(); @@ -238,9 +238,9 @@ public int clampViewPositionHorizontal(View child, int left, int dx) { return getPaddingLeft() - mDragDistance; break; } - } else if (getBottomViews().get(currentDirectionIndex) == child) { + } else if (getBottomViews().get(mCurrentDirectionIndex) == child) { - switch (mDragEdges.get(currentDirectionIndex)) { + switch (mDragEdges.get(mCurrentDirectionIndex)) { case Top: case Bottom: return getPaddingLeft(); @@ -264,7 +264,7 @@ public int clampViewPositionHorizontal(View child, int left, int dx) { @Override public int clampViewPositionVertical(View child, int top, int dy) { if (child == getSurfaceView()) { - switch (mDragEdges.get(currentDirectionIndex)) { + switch (mDragEdges.get(mCurrentDirectionIndex)) { case Left: case Right: return getPaddingTop(); @@ -282,7 +282,7 @@ public int clampViewPositionVertical(View child, int top, int dy) { } } } else { - switch (mDragEdges.get(currentDirectionIndex)) { + switch (mDragEdges.get(mCurrentDirectionIndex)) { case Left: case Right: return getPaddingTop(); @@ -351,10 +351,10 @@ public void onViewPositionChanged(View changedView, int left, int top, int dx, i if (changedView == getSurfaceView()) { if (mShowMode == ShowMode.PullOut) { - if (mDragEdges.get(currentDirectionIndex) == DragEdge.Left - || mDragEdges.get(currentDirectionIndex) == DragEdge.Right) - getBottomViews().get(currentDirectionIndex).offsetLeftAndRight(dx); - else getBottomViews().get(currentDirectionIndex).offsetTopAndBottom(dy); + if (mDragEdges.get(mCurrentDirectionIndex) == DragEdge.Left + || mDragEdges.get(mCurrentDirectionIndex) == DragEdge.Right) + getBottomViews().get(mCurrentDirectionIndex).offsetLeftAndRight(dx); + else getBottomViews().get(mCurrentDirectionIndex).offsetTopAndBottom(dy); } } else if (getBottomViews().contains(changedView)) { @@ -363,18 +363,18 @@ public void onViewPositionChanged(View changedView, int left, int top, int dx, i getSurfaceView().offsetLeftAndRight(dx); getSurfaceView().offsetTopAndBottom(dy); } else { - Rect rect = computeBottomLayDown(mDragEdges.get(currentDirectionIndex)); - getBottomViews().get(currentDirectionIndex).layout(rect.left, rect.top, rect.right, rect.bottom); + Rect rect = computeBottomLayDown(mDragEdges.get(mCurrentDirectionIndex)); + getBottomViews().get(mCurrentDirectionIndex).layout(rect.left, rect.top, rect.right, rect.bottom); int newLeft = getSurfaceView().getLeft() + dx, newTop = getSurfaceView().getTop() + dy; - if (mDragEdges.get(currentDirectionIndex) == DragEdge.Left && newLeft < getPaddingLeft()) + if (mDragEdges.get(mCurrentDirectionIndex) == DragEdge.Left && newLeft < getPaddingLeft()) newLeft = getPaddingLeft(); - else if (mDragEdges.get(currentDirectionIndex) == DragEdge.Right && newLeft > getPaddingLeft()) + else if (mDragEdges.get(mCurrentDirectionIndex) == DragEdge.Right && newLeft > getPaddingLeft()) newLeft = getPaddingLeft(); - else if (mDragEdges.get(currentDirectionIndex) == DragEdge.Top && newTop < getPaddingTop()) + else if (mDragEdges.get(mCurrentDirectionIndex) == DragEdge.Top && newTop < getPaddingTop()) newTop = getPaddingTop(); - else if (mDragEdges.get(currentDirectionIndex) == DragEdge.Bottom && newTop > getPaddingTop()) + else if (mDragEdges.get(mCurrentDirectionIndex) == DragEdge.Bottom && newTop > getPaddingTop()) newTop = getPaddingTop(); getSurfaceView() @@ -531,7 +531,7 @@ protected void dispatchSwipeEvent(int surfaceLeft, int surfaceTop, boolean open) } if (status == Status.Open) { - getBottomViews().get(currentDirectionIndex).setEnabled(true); + getBottomViews().get(mCurrentDirectionIndex).setEnabled(true); for (SwipeListener l : mSwipeListeners) { l.onOpen(SwipeLayout.this); } @@ -552,8 +552,8 @@ private void safeBottomView() { if (bottom.getVisibility() != INVISIBLE) bottom.setVisibility(INVISIBLE); } } else { - if (bottoms.get(currentDirectionIndex).getVisibility() != VISIBLE) - bottoms.get(currentDirectionIndex).setVisibility(VISIBLE); + if (bottoms.get(mCurrentDirectionIndex).getVisibility() != VISIBLE) + bottoms.get(mCurrentDirectionIndex).setVisibility(VISIBLE); } } @@ -563,13 +563,13 @@ protected void dispatchRevealEvent(final int surfaceLeft, final int surfaceTop, for (Map.Entry> entry : mRevealListeners.entrySet()) { View child = entry.getKey(); Rect rect = getRelativePosition(child); - if (isViewShowing(child, rect, mDragEdges.get(currentDirectionIndex), surfaceLeft, surfaceTop, + if (isViewShowing(child, rect, mDragEdges.get(mCurrentDirectionIndex), surfaceLeft, surfaceTop, surfaceRight, surfaceBottom)) { mShowEntirely.put(child, false); int distance = 0; float fraction = 0f; if (getShowMode() == ShowMode.LayDown) { - switch (mDragEdges.get(currentDirectionIndex)) { + switch (mDragEdges.get(mCurrentDirectionIndex)) { case Left: distance = rect.left - surfaceLeft; fraction = distance / (float) child.getWidth(); @@ -588,7 +588,7 @@ protected void dispatchRevealEvent(final int surfaceLeft, final int surfaceTop, break; } } else if (getShowMode() == ShowMode.PullOut) { - switch (mDragEdges.get(currentDirectionIndex)) { + switch (mDragEdges.get(mCurrentDirectionIndex)) { case Left: distance = rect.right - getPaddingLeft(); fraction = distance / (float) child.getWidth(); @@ -609,22 +609,22 @@ protected void dispatchRevealEvent(final int surfaceLeft, final int surfaceTop, } for (OnRevealListener l : entry.getValue()) { - l.onReveal(child, mDragEdges.get(currentDirectionIndex), Math.abs(fraction), distance); + l.onReveal(child, mDragEdges.get(mCurrentDirectionIndex), Math.abs(fraction), distance); if (Math.abs(fraction) == 1) { mShowEntirely.put(child, true); } } } - if (isViewTotallyFirstShowed(child, rect, mDragEdges.get(currentDirectionIndex), surfaceLeft, surfaceTop, + if (isViewTotallyFirstShowed(child, rect, mDragEdges.get(mCurrentDirectionIndex), surfaceLeft, surfaceTop, surfaceRight, surfaceBottom)) { mShowEntirely.put(child, true); for (OnRevealListener l : entry.getValue()) { - if (mDragEdges.get(currentDirectionIndex) == DragEdge.Left - || mDragEdges.get(currentDirectionIndex) == DragEdge.Right) - l.onReveal(child, mDragEdges.get(currentDirectionIndex), 1, child.getWidth()); + if (mDragEdges.get(mCurrentDirectionIndex) == DragEdge.Left + || mDragEdges.get(mCurrentDirectionIndex) == DragEdge.Right) + l.onReveal(child, mDragEdges.get(mCurrentDirectionIndex), 1, child.getWidth()); else - l.onReveal(child, mDragEdges.get(currentDirectionIndex), 1, child.getHeight()); + l.onReveal(child, mDragEdges.get(mCurrentDirectionIndex), 1, child.getHeight()); } } @@ -686,7 +686,7 @@ void layoutPullOut() { Rect rect = computeSurfaceLayoutArea(false); getSurfaceView().layout(rect.left, rect.top, rect.right, rect.bottom); rect = computeBottomLayoutAreaViaSurface(ShowMode.PullOut, rect); - getBottomViews().get(currentDirectionIndex).layout(rect.left, rect.top, rect.right, rect.bottom); + getBottomViews().get(mCurrentDirectionIndex).layout(rect.left, rect.top, rect.right, rect.bottom); bringChildToFront(getSurfaceView()); } @@ -694,7 +694,7 @@ void layoutLayDown() { Rect rect = computeSurfaceLayoutArea(false); getSurfaceView().layout(rect.left, rect.top, rect.right, rect.bottom); rect = computeBottomLayoutAreaViaSurface(ShowMode.LayDown, rect); - getBottomViews().get(currentDirectionIndex).layout(rect.left, rect.top, rect.right, rect.bottom); + getBottomViews().get(mCurrentDirectionIndex).layout(rect.left, rect.top, rect.right, rect.bottom); bringChildToFront(getSurfaceView()); } @@ -702,11 +702,11 @@ void layoutLayDown() { protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); - if (mDragEdges.get(currentDirectionIndex) == DragEdge.Left - || mDragEdges.get(currentDirectionIndex) == DragEdge.Right) - mDragDistance = getBottomViews().get(currentDirectionIndex).getMeasuredWidth() + if (mDragEdges.get(mCurrentDirectionIndex) == DragEdge.Left + || mDragEdges.get(mCurrentDirectionIndex) == DragEdge.Right) + mDragDistance = getBottomViews().get(mCurrentDirectionIndex).getMeasuredWidth() - dp2px(getCurrentOffset()); - else mDragDistance = getBottomViews().get(currentDirectionIndex).getMeasuredHeight() + else mDragDistance = getBottomViews().get(mCurrentDirectionIndex).getMeasuredHeight() - dp2px(getCurrentOffset()); } @@ -739,7 +739,7 @@ public boolean onInterceptTouchEvent(MotionEvent ev) { if (status == Status.Close) { mTouchConsumedByChild = childNeedHandleTouchEvent(getSurfaceView(), ev) != null; } else if (status == Status.Open) { - mTouchConsumedByChild = childNeedHandleTouchEvent(getBottomViews().get(currentDirectionIndex), ev) != null; + mTouchConsumedByChild = childNeedHandleTouchEvent(getBottomViews().get(mCurrentDirectionIndex), ev) != null; } break; case MotionEvent.ACTION_UP: @@ -799,6 +799,14 @@ private boolean childNeedHandleTouchEvent(View v, MotionEvent event) { private float sX = -1, sY = -1; + private boolean shouldAllowSwipe() { + if (mCurrentDirectionIndex == mLeftIndex && !mLeftSwipeEnabled) return false; + if (mCurrentDirectionIndex == mRightIndex && !mRightSwipeEnabled) return false; + if (mCurrentDirectionIndex == mTopIndex && !mTopSwipeEnabled) return false; + if (mCurrentDirectionIndex == mBottomIndex && !mBottomSwipeEnabled) return false; + return true; + } + @Override public boolean onTouchEvent(MotionEvent event) { if (!isEnabledInAdapterView() || !isEnabled()) return true; @@ -814,7 +822,7 @@ public boolean onTouchEvent(MotionEvent event) { if (status == Status.Close) { touching = getSurfaceView(); } else if (status == Status.Open) { - touching = getBottomViews().get(currentDirectionIndex); + touching = getBottomViews().get(mCurrentDirectionIndex); } switch (action) { @@ -833,27 +841,28 @@ public boolean onTouchEvent(MotionEvent event) { float angle = Math.abs(distanceY / distanceX); angle = (float) Math.toDegrees(Math.atan(angle)); if (getOpenStatus() == Status.Close) { - int lastCurrentDirectionIndex = currentDirectionIndex; + int lastCurrentDirectionIndex = mCurrentDirectionIndex; if (angle < 45) { - if (mLeftIndex != -1 && distanceX > 0) { - currentDirectionIndex = mLeftIndex; - } else if (mRightIndex != -1) { - currentDirectionIndex = mRightIndex; + if (mLeftIndex != -1 && distanceX > 0 && isLeftSwipeEnabled()) { + mCurrentDirectionIndex = mLeftIndex; + } else if (mRightIndex != -1 && isRightSwipeEnabled()) { + mCurrentDirectionIndex = mRightIndex; } } else { - if (mTopIndex != -1 && distanceY < 0) { - currentDirectionIndex = mTopIndex; - } else if (mBottomIndex != -1) { - currentDirectionIndex = mBottomIndex; + if (mTopIndex != -1 && distanceY < 0 && isTopSwipeEnabled()) { + mCurrentDirectionIndex = mTopIndex; + } else if (mBottomIndex != -1 && isBottomSwipeEnabled()) { + mCurrentDirectionIndex = mBottomIndex; } } - if (lastCurrentDirectionIndex != currentDirectionIndex) { + if (lastCurrentDirectionIndex != mCurrentDirectionIndex) { updateBottomViews(); } } + if (!shouldAllowSwipe()) return super.onTouchEvent(event); boolean doNothing = false; - if (mDragEdges.get(currentDirectionIndex) == DragEdge.Right) { + if (mDragEdges.get(mCurrentDirectionIndex) == DragEdge.Right) { boolean suitable = (status == Status.Open && distanceX > mTouchSlop) || (status == Status.Close && distanceX < -mTouchSlop); suitable = suitable || (status == Status.Middle); @@ -863,7 +872,7 @@ public boolean onTouchEvent(MotionEvent event) { } } - if (mDragEdges.get(currentDirectionIndex) == DragEdge.Left) { + if (mDragEdges.get(mCurrentDirectionIndex) == DragEdge.Left) { boolean suitable = (status == Status.Open && distanceX < -mTouchSlop) || (status == Status.Close && distanceX > mTouchSlop); suitable = suitable || status == Status.Middle; @@ -873,7 +882,7 @@ public boolean onTouchEvent(MotionEvent event) { } } - if (mDragEdges.get(currentDirectionIndex) == DragEdge.Top) { + if (mDragEdges.get(mCurrentDirectionIndex) == DragEdge.Top) { boolean suitable = (status == Status.Open && distanceY < -mTouchSlop) || (status == Status.Close && distanceY > mTouchSlop); suitable = suitable || status == Status.Middle; @@ -883,7 +892,7 @@ public boolean onTouchEvent(MotionEvent event) { } } - if (mDragEdges.get(currentDirectionIndex) == DragEdge.Bottom) { + if (mDragEdges.get(mCurrentDirectionIndex) == DragEdge.Bottom) { boolean suitable = (status == Status.Open && distanceY > mTouchSlop) || (status == Status.Close && distanceY < -mTouchSlop); suitable = suitable || status == Status.Middle; @@ -1058,7 +1067,7 @@ public void onLongPress(MotionEvent e) { public boolean onDoubleTap(MotionEvent e) { if (mDoubleClickListener != null) { View target; - ViewGroup bottom = getBottomViews().get(currentDirectionIndex); + ViewGroup bottom = getBottomViews().get(mCurrentDirectionIndex); ViewGroup surface = getSurfaceView(); if (e.getX() > bottom.getLeft() && e.getX() < bottom.getRight() && e.getY() > bottom.getTop() && e.getY() < bottom.getBottom()) { @@ -1075,7 +1084,7 @@ public boolean onDoubleTap(MotionEvent e) { public void setDragEdge(DragEdge dragEdge) { mDragEdges = new ArrayList(); mDragEdges.add(dragEdge); - currentDirectionIndex = 0; + mCurrentDirectionIndex = 0; populateIndexes(); requestLayout(); updateBottomViews(); @@ -1106,7 +1115,7 @@ public void setShowMode(ShowMode mode) { } public DragEdge getDragEdge() { - return mDragEdges.get(currentDirectionIndex); + return mDragEdges.get(mCurrentDirectionIndex); } public int getDragDistance() { @@ -1220,26 +1229,26 @@ public Status getOpenStatus() { private void processSurfaceRelease(float xvel, float yvel) { if (xvel == 0 && getOpenStatus() == Status.Middle) close(); - if (mDragEdges.get(currentDirectionIndex) == DragEdge.Left - || mDragEdges.get(currentDirectionIndex) == DragEdge.Right) { + if (mDragEdges.get(mCurrentDirectionIndex) == DragEdge.Left + || mDragEdges.get(mCurrentDirectionIndex) == DragEdge.Right) { if (xvel > 0) { - if (mDragEdges.get(currentDirectionIndex) == DragEdge.Left) + if (mDragEdges.get(mCurrentDirectionIndex) == DragEdge.Left) open(); else close(); } if (xvel < 0) { - if (mDragEdges.get(currentDirectionIndex) == DragEdge.Left) + if (mDragEdges.get(mCurrentDirectionIndex) == DragEdge.Left) close(); else open(); } } else { if (yvel > 0) { - if (mDragEdges.get(currentDirectionIndex) == DragEdge.Top) + if (mDragEdges.get(mCurrentDirectionIndex) == DragEdge.Top) open(); else close(); } if (yvel < 0) { - if (mDragEdges.get(currentDirectionIndex) == DragEdge.Top) + if (mDragEdges.get(mCurrentDirectionIndex) == DragEdge.Top) close(); else open(); } @@ -1256,27 +1265,27 @@ private void processBottomPullOutRelease(float xvel, float yvel) { if (xvel == 0 && getOpenStatus() == Status.Middle) close(); - if (mDragEdges.get(currentDirectionIndex) == DragEdge.Left - || mDragEdges.get(currentDirectionIndex) == DragEdge.Right) { + if (mDragEdges.get(mCurrentDirectionIndex) == DragEdge.Left + || mDragEdges.get(mCurrentDirectionIndex) == DragEdge.Right) { if (xvel > 0) { - if (mDragEdges.get(currentDirectionIndex) == DragEdge.Left) + if (mDragEdges.get(mCurrentDirectionIndex) == DragEdge.Left) open(); else close(); } if (xvel < 0) { - if (mDragEdges.get(currentDirectionIndex) == DragEdge.Left) + if (mDragEdges.get(mCurrentDirectionIndex) == DragEdge.Left) close(); else open(); } } else { if (yvel > 0) { - if (mDragEdges.get(currentDirectionIndex) == DragEdge.Top) + if (mDragEdges.get(mCurrentDirectionIndex) == DragEdge.Top) open(); else close(); } if (yvel < 0) { - if (mDragEdges.get(currentDirectionIndex) == DragEdge.Top) + if (mDragEdges.get(mCurrentDirectionIndex) == DragEdge.Top) close(); else open(); } @@ -1295,11 +1304,11 @@ private void processBottomLayDownMode(float xvel, float yvel) { int l = getPaddingLeft(), t = getPaddingTop(); - if (xvel < 0 && mDragEdges.get(currentDirectionIndex) == DragEdge.Right) l -= mDragDistance; - if (xvel > 0 && mDragEdges.get(currentDirectionIndex) == DragEdge.Left) l += mDragDistance; + if (xvel < 0 && mDragEdges.get(mCurrentDirectionIndex) == DragEdge.Right) l -= mDragDistance; + if (xvel > 0 && mDragEdges.get(mCurrentDirectionIndex) == DragEdge.Left) l += mDragDistance; - if (yvel > 0 && mDragEdges.get(currentDirectionIndex) == DragEdge.Top) t += mDragDistance; - if (yvel < 0 && mDragEdges.get(currentDirectionIndex) == DragEdge.Bottom) + if (yvel > 0 && mDragEdges.get(mCurrentDirectionIndex) == DragEdge.Top) t += mDragDistance; + if (yvel < 0 && mDragEdges.get(mCurrentDirectionIndex) == DragEdge.Bottom) t -= mDragDistance; mDragHelper.smoothSlideViewTo(getSurfaceView(), l, t); @@ -1318,7 +1327,7 @@ public void open(boolean smooth) { } public void open(boolean smooth, boolean notify) { - ViewGroup surface = getSurfaceView(), bottom = getBottomViews().get(currentDirectionIndex); + ViewGroup surface = getSurfaceView(), bottom = getBottomViews().get(mCurrentDirectionIndex); int dx, dy; Rect rect = computeSurfaceLayoutArea(true); if (smooth) { @@ -1397,11 +1406,11 @@ public void toggle(boolean smooth) { private Rect computeSurfaceLayoutArea(boolean open) { int l = getPaddingLeft(), t = getPaddingTop(); if (open) { - if (mDragEdges.get(currentDirectionIndex) == DragEdge.Left) + if (mDragEdges.get(mCurrentDirectionIndex) == DragEdge.Left) l = getPaddingLeft() + mDragDistance; - else if (mDragEdges.get(currentDirectionIndex) == DragEdge.Right) + else if (mDragEdges.get(mCurrentDirectionIndex) == DragEdge.Right) l = getPaddingLeft() - mDragDistance; - else if (mDragEdges.get(currentDirectionIndex) == DragEdge.Top) + else if (mDragEdges.get(mCurrentDirectionIndex) == DragEdge.Top) t = getPaddingTop() + mDragDistance; else t = getPaddingTop() - mDragDistance; } @@ -1413,27 +1422,27 @@ private Rect computeBottomLayoutAreaViaSurface(ShowMode mode, Rect surfaceArea) int bl = rect.left, bt = rect.top, br = rect.right, bb = rect.bottom; if (mode == ShowMode.PullOut) { - if (mDragEdges.get(currentDirectionIndex) == DragEdge.Left) + if (mDragEdges.get(mCurrentDirectionIndex) == DragEdge.Left) bl = rect.left - mDragDistance; - else if (mDragEdges.get(currentDirectionIndex) == DragEdge.Right) + else if (mDragEdges.get(mCurrentDirectionIndex) == DragEdge.Right) bl = rect.right; - else if (mDragEdges.get(currentDirectionIndex) == DragEdge.Top) + else if (mDragEdges.get(mCurrentDirectionIndex) == DragEdge.Top) bt = rect.top - mDragDistance; else bt = rect.bottom; - if (mDragEdges.get(currentDirectionIndex) == DragEdge.Left || mDragEdges.get(currentDirectionIndex) == DragEdge.Right) { + if (mDragEdges.get(mCurrentDirectionIndex) == DragEdge.Left || mDragEdges.get(mCurrentDirectionIndex) == DragEdge.Right) { bb = rect.bottom; - br = bl + getBottomViews().get(currentDirectionIndex).getMeasuredWidth(); + br = bl + getBottomViews().get(mCurrentDirectionIndex).getMeasuredWidth(); } else { - bb = bt + getBottomViews().get(currentDirectionIndex).getMeasuredHeight(); + bb = bt + getBottomViews().get(mCurrentDirectionIndex).getMeasuredHeight(); br = rect.right; } } else if (mode == ShowMode.LayDown) { - if (mDragEdges.get(currentDirectionIndex) == DragEdge.Left) + if (mDragEdges.get(mCurrentDirectionIndex) == DragEdge.Left) br = bl + mDragDistance; - else if (mDragEdges.get(currentDirectionIndex) == DragEdge.Right) + else if (mDragEdges.get(mCurrentDirectionIndex) == DragEdge.Right) bl = br - mDragDistance; - else if (mDragEdges.get(currentDirectionIndex) == DragEdge.Top) + else if (mDragEdges.get(mCurrentDirectionIndex) == DragEdge.Top) bb = bt + mDragDistance; else bt = bb - mDragDistance; @@ -1478,7 +1487,7 @@ public List getDragEdges() { public void setDragEdges(List mDragEdges) { this.mDragEdges = mDragEdges; - currentDirectionIndex = 0; + mCurrentDirectionIndex = 0; populateIndexes(); updateBottomViews(); } @@ -1488,7 +1497,7 @@ public void setDragEdges(DragEdge... mDragEdges) { for (DragEdge e : mDragEdges) { this.mDragEdges.add(e); } - currentDirectionIndex = 0; + mCurrentDirectionIndex = 0; populateIndexes(); updateBottomViews(); } @@ -1501,18 +1510,18 @@ private void populateIndexes() { } private float getCurrentOffset() { - if (mDragEdges.get(currentDirectionIndex) == DragEdge.Left) return mLeftEdgeSwipeOffset; - else if (mDragEdges.get(currentDirectionIndex) == DragEdge.Right) + if (mDragEdges.get(mCurrentDirectionIndex) == DragEdge.Left) return mLeftEdgeSwipeOffset; + else if (mDragEdges.get(mCurrentDirectionIndex) == DragEdge.Right) return mRightEdgeSwipeOffset; - else if (mDragEdges.get(currentDirectionIndex) == DragEdge.Top) return mTopEdgeSwipeOffset; + else if (mDragEdges.get(mCurrentDirectionIndex) == DragEdge.Top) return mTopEdgeSwipeOffset; else return mBottomEdgeSwipeOffset; } private void updateBottomViews() { // removeAllViews(); -// addView(getBottomViews().get(currentDirectionIndex)); +// addView(getBottomViews().get(mCurrentDirectionIndex)); // addView(getSurfaceView()); -// getBottomViews().get(currentDirectionIndex).bringToFront(); +// getBottomViews().get(mCurrentDirectionIndex).bringToFront(); // getSurfaceView().bringToFront(); if (mShowMode == ShowMode.PullOut) layoutPullOut(); From e8f4ec1c98d4c72f7e62435471018cb7ccc799d7 Mon Sep 17 00:00:00 2001 From: daimajia Date: Wed, 11 Feb 2015 23:17:19 +0800 Subject: [PATCH 12/16] remove useless files --- .../daimajia/swipedemo/MyActivity.java.orig | 174 -- .../main/res/layout/listview_item.xml.orig | 68 - .../com/daimajia/swipe/SwipeLayout.java.orig | 1512 ----------------- 3 files changed, 1754 deletions(-) delete mode 100644 demo/src/main/java/com/daimajia/swipedemo/MyActivity.java.orig delete mode 100644 demo/src/main/res/layout/listview_item.xml.orig delete mode 100644 library/src/main/java/com/daimajia/swipe/SwipeLayout.java.orig diff --git a/demo/src/main/java/com/daimajia/swipedemo/MyActivity.java.orig b/demo/src/main/java/com/daimajia/swipedemo/MyActivity.java.orig deleted file mode 100644 index 54ff83a8..00000000 --- a/demo/src/main/java/com/daimajia/swipedemo/MyActivity.java.orig +++ /dev/null @@ -1,174 +0,0 @@ -package com.daimajia.swipedemo; - -import android.app.Activity; -import android.content.Intent; -import android.graphics.Color; -import android.os.Bundle; -import android.view.Menu; -import android.view.MenuItem; -import android.view.View; -import android.widget.Toast; - -import com.daimajia.swipe.SwipeLayout; -import com.nineoldandroids.view.ViewHelper; - -public class MyActivity extends Activity { - - private SwipeLayout sample1, sample2, sample3; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.main); - -// SwipeLayout swipeLayout = (SwipeLayout)findViewById(R.id.godfather); -// swipeLayout.setDragEdge(SwipeLayout.DragEdge.Bottom); // Set in XML - - //sample1 - - sample1 = (SwipeLayout) findViewById(R.id.sample1); - sample1.setShowMode(SwipeLayout.ShowMode.LayDown); - sample1.setDragEdges(SwipeLayout.DragEdge.Left, SwipeLayout.DragEdge.Right); - // When using multiple drag edges it's a good idea to pass the ids of the views that you're using for the left, right, top bottom views (-1 if you're not using a particular view) - sample1.setBottomViewIds(R.id.bottom_wrapper, R.id.bottom_wrapper_2, -1, -1); - sample1.addRevealListener(R.id.delete, new SwipeLayout.OnRevealListener() { - @Override - public void onReveal(View child, SwipeLayout.DragEdge edge, float fraction, int distance) { - - } - }); - - sample1.findViewById(R.id.star2).setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - Toast.makeText(MyActivity.this, "Star", Toast.LENGTH_SHORT).show(); - } - }); - - sample1.findViewById(R.id.trash2).setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - Toast.makeText(MyActivity.this, "Trash Bin", Toast.LENGTH_SHORT).show(); - } - }); - - sample1.findViewById(R.id.magnifier2).setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - Toast.makeText(MyActivity.this, "Magnifier", Toast.LENGTH_SHORT).show(); - } - }); - - //sample2 - - sample2 = (SwipeLayout) findViewById(R.id.sample2); - sample2.setShowMode(SwipeLayout.ShowMode.LayDown); - sample2.setDragEdge(SwipeLayout.DragEdge.Right); -// sample2.setShowMode(SwipeLayout.ShowMode.PullOut); - sample2.findViewById(R.id.star).setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - Toast.makeText(MyActivity.this, "Star", Toast.LENGTH_SHORT).show(); - } - }); - - sample2.findViewById(R.id.trash).setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - Toast.makeText(MyActivity.this, "Trash Bin", Toast.LENGTH_SHORT).show(); - } - }); - - sample2.findViewById(R.id.magnifier).setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - Toast.makeText(MyActivity.this, "Magnifier", Toast.LENGTH_SHORT).show(); - } - }); - - sample2.findViewById(R.id.click).setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - Toast.makeText(MyActivity.this, "Yo", Toast.LENGTH_SHORT).show(); - } - }); - - //sample3 - - sample3 = (SwipeLayout) findViewById(R.id.sample3); - sample3.setDragEdge(SwipeLayout.DragEdge.Top); - sample3.addRevealListener(R.id.bottom_wrapper_child1, new SwipeLayout.OnRevealListener() { - @Override - public void onReveal(View child, SwipeLayout.DragEdge edge, float fraction, int distance) { - View star = child.findViewById(R.id.star); - float d = child.getHeight() / 2 - star.getHeight() / 2; - ViewHelper.setTranslationY(star, d * fraction); - ViewHelper.setScaleX(star, fraction + 0.6f); - ViewHelper.setScaleY(star, fraction + 0.6f); - int c = (Integer) evaluate(fraction, Color.parseColor("#dddddd"), Color.parseColor("#4C535B")); - child.setBackgroundColor(c); - } - }); - sample3.findViewById(R.id.star).setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - Toast.makeText(MyActivity.this, "Yo!", Toast.LENGTH_SHORT).show(); - } - }); - - } - - @Override - public boolean onCreateOptionsMenu(Menu menu) { - // Inflate the menu; this adds items to the action bar if it is present. - getMenuInflater().inflate(R.menu.my, menu); - return true; - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - // Handle action bar item clicks here. The action bar will - // automatically handle clicks on the Home/Up button, so long - // as you specify a parent activity in AndroidManifest.xml. - int id = item.getItemId(); - if (id == R.id.action_listview) { - startActivity(new Intent(this, ListViewExample.class)); - return true; - } else if (id == R.id.action_gridview) { - startActivity(new Intent(this, GridViewExample.class)); - return true; -<<<<<<< HEAD - } else if (id == R.id.action_nexted) { -======= - } else if (id == R.id.action_nested) { ->>>>>>> upstream/master - startActivity(new Intent(this, NestedExample.class)); - return true; - } else if (id == R.id.action_recycler) { - startActivity(new Intent(this, RecyclerViewExample.class)); - } - return super.onOptionsItemSelected(item); - } - - /* - Color transition method. - */ - public Object evaluate(float fraction, Object startValue, Object endValue) { - int startInt = (Integer) startValue; - int startA = (startInt >> 24) & 0xff; - int startR = (startInt >> 16) & 0xff; - int startG = (startInt >> 8) & 0xff; - int startB = startInt & 0xff; - - int endInt = (Integer) endValue; - int endA = (endInt >> 24) & 0xff; - int endR = (endInt >> 16) & 0xff; - int endG = (endInt >> 8) & 0xff; - int endB = endInt & 0xff; - - return (int) ((startA + (int) (fraction * (endA - startA))) << 24) | - (int) ((startR + (int) (fraction * (endR - startR))) << 16) | - (int) ((startG + (int) (fraction * (endG - startG))) << 8) | - (int) ((startB + (int) (fraction * (endB - startB)))); - } -} diff --git a/demo/src/main/res/layout/listview_item.xml.orig b/demo/src/main/res/layout/listview_item.xml.orig deleted file mode 100644 index 516b240b..00000000 --- a/demo/src/main/res/layout/listview_item.xml.orig +++ /dev/null @@ -1,68 +0,0 @@ - - - - -======= - android:layout_height="80dp" - android:background="#FF5534" - android:gravity="center" - android:tag="Bottom3" - android:weightSum="10"> ->>>>>>> upstream/master - - - - - -