From b85a0a81e423b435523866a2c4db63c5efcc94cc Mon Sep 17 00:00:00 2001 From: Alexander Mikhalevich Date: Tue, 31 May 2016 21:04:26 +0300 Subject: [PATCH 1/2] Added looping feature. --- README.md | 2 ++ build.gradle | 2 +- .../java/link/fls/swipestack/SwipeStack.java | 31 ++++++++++++------- library/src/main/res/values/attrs.xml | 1 + 4 files changed, 24 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index be0a363..9c98345 100644 --- a/README.md +++ b/README.md @@ -125,6 +125,8 @@ Currently SwipeStack implements the following callbacks: `disable_hw_acceleration` set to `true` disables hardware acceleration. *Default: false* +`looped` set to `true` enables looping. *Default: false* + ## Copyright Notice ## ``` Copyright (C) 2016 Frederik Schweiger diff --git a/build.gradle b/build.gradle index 23b974a..bc63746 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:2.0.0-beta5' + classpath 'com.android.tools.build:gradle:2.1.0' classpath 'com.novoda:bintray-release:0.3.4' } } diff --git a/library/src/main/java/link/fls/swipestack/SwipeStack.java b/library/src/main/java/link/fls/swipestack/SwipeStack.java index 175e28d..b554838 100644 --- a/library/src/main/java/link/fls/swipestack/SwipeStack.java +++ b/library/src/main/java/link/fls/swipestack/SwipeStack.java @@ -44,6 +44,7 @@ public class SwipeStack extends ViewGroup { public static final float DEFAULT_SWIPE_OPACITY = 1f; public static final float DEFAULT_SCALE_FACTOR = 1f; public static final boolean DEFAULT_DISABLE_HW_ACCELERATION = true; + public static final boolean DEFAULT_IS_LOOPED = false; private static final String KEY_SUPER_STATE = "superState"; private static final String KEY_CURRENT_INDEX = "currentIndex"; @@ -53,7 +54,7 @@ public class SwipeStack extends ViewGroup { private int mAllowedSwipeDirections; private int mAnimationDuration; - private int mCurrentViewIndex; + private int mCurrentIndex; private int mNumberOfStackedViews; private int mViewSpacing; private int mViewRotation; @@ -62,6 +63,7 @@ public class SwipeStack extends ViewGroup { private float mScaleFactor; private boolean mDisableHwAcceleration; private boolean mIsFirstLayout = true; + private boolean mIsLooped; private View mTopView; private SwipeHelper mSwipeHelper; @@ -109,6 +111,9 @@ private void readAttributes(AttributeSet attributeSet) { mDisableHwAcceleration = attrs.getBoolean(R.styleable.SwipeStack_disable_hw_acceleration, DEFAULT_DISABLE_HW_ACCELERATION); + mIsLooped = + attrs.getBoolean(R.styleable.SwipeStack_looped, + DEFAULT_IS_LOOPED); } finally { attrs.recycle(); } @@ -139,7 +144,7 @@ public void onChanged() { public Parcelable onSaveInstanceState() { Bundle bundle = new Bundle(); bundle.putParcelable(KEY_SUPER_STATE, super.onSaveInstanceState()); - bundle.putInt(KEY_CURRENT_INDEX, mCurrentViewIndex - getChildCount()); + bundle.putInt(KEY_CURRENT_INDEX, mCurrentIndex - getChildCount()); return bundle; } @@ -147,24 +152,28 @@ public Parcelable onSaveInstanceState() { public void onRestoreInstanceState(Parcelable state) { if (state instanceof Bundle) { Bundle bundle = (Bundle) state; - mCurrentViewIndex = bundle.getInt(KEY_CURRENT_INDEX); + mCurrentIndex = bundle.getInt(KEY_CURRENT_INDEX); state = bundle.getParcelable(KEY_SUPER_STATE); } super.onRestoreInstanceState(state); } + private int getCurrentViewIndex() { + return mCurrentIndex % mAdapter.getCount(); + } + @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { if (mAdapter == null || mAdapter.isEmpty()) { - mCurrentViewIndex = 0; + mCurrentIndex = 0; removeAllViewsInLayout(); return; } for (int x = getChildCount(); - x < mNumberOfStackedViews && mCurrentViewIndex < mAdapter.getCount(); + x < mNumberOfStackedViews && (mIsLooped || mCurrentIndex < mAdapter.getCount()); x++) { addNextView(); } @@ -175,8 +184,8 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) { } private void addNextView() { - if (mCurrentViewIndex < mAdapter.getCount()) { - View bottomView = mAdapter.getView(mCurrentViewIndex, null, this); + if (getCurrentViewIndex() < mAdapter.getCount()) { + View bottomView = mAdapter.getView(getCurrentViewIndex(), null, this); bottomView.setTag(R.id.new_view, true); if (!mDisableHwAcceleration) { @@ -211,7 +220,7 @@ private void addNextView() { bottomView.measure(measureSpecWidth | width, measureSpecHeight | height); addViewInLayout(bottomView, 0, params, true); - mCurrentViewIndex++; + mCurrentIndex++; } } @@ -301,7 +310,7 @@ public void onSwipeEnd() { } public void onViewSwipedToLeft() { - if (mListener != null) mListener.onViewSwipedToLeft(getCurrentPosition()); + if (mListener != null) mListener.onViewSwipedToRight(getCurrentPosition()); removeTopView(); } @@ -316,7 +325,7 @@ public void onViewSwipedToRight() { * @return The current position. */ public int getCurrentPosition() { - return mCurrentViewIndex - getChildCount(); + return (mCurrentIndex - getChildCount()) % mAdapter.getCount(); } /** @@ -410,7 +419,7 @@ public void swipeTopViewToLeft() { * Resets the current adapter position and repopulates the stack. */ public void resetStack() { - mCurrentViewIndex = 0; + mCurrentIndex = 0; removeAllViewsInLayout(); requestLayout(); } diff --git a/library/src/main/res/values/attrs.xml b/library/src/main/res/values/attrs.xml index 0ec3fcd..ea44e48 100644 --- a/library/src/main/res/values/attrs.xml +++ b/library/src/main/res/values/attrs.xml @@ -14,5 +14,6 @@ + \ No newline at end of file From 95a446ba2ea8f3a01ba3ec5adaaf3f75baad98c1 Mon Sep 17 00:00:00 2001 From: Alexander Mikhalevich Date: Tue, 31 May 2016 21:14:00 +0300 Subject: [PATCH 2/2] on(SomeAction) callbacks should be invoked after the action. --- library/src/main/java/link/fls/swipestack/SwipeStack.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/library/src/main/java/link/fls/swipestack/SwipeStack.java b/library/src/main/java/link/fls/swipestack/SwipeStack.java index b554838..ce7590d 100644 --- a/library/src/main/java/link/fls/swipestack/SwipeStack.java +++ b/library/src/main/java/link/fls/swipestack/SwipeStack.java @@ -310,13 +310,15 @@ public void onSwipeEnd() { } public void onViewSwipedToLeft() { - if (mListener != null) mListener.onViewSwipedToRight(getCurrentPosition()); + int swipedPosition = getCurrentPosition(); removeTopView(); + if (mListener != null) mListener.onViewSwipedToRight(swipedPosition); } public void onViewSwipedToRight() { - if (mListener != null) mListener.onViewSwipedToRight(getCurrentPosition()); + int swipedPosition = getCurrentPosition(); removeTopView(); + if (mListener != null) mListener.onViewSwipedToRight(swipedPosition); } /**