Skip to content
This repository was archived by the owner on Jul 22, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}
Expand Down
35 changes: 23 additions & 12 deletions library/src/main/java/link/fls/swipestack/SwipeStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -139,32 +144,36 @@ 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;
}

@Override
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();
}
Expand All @@ -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) {
Expand Down Expand Up @@ -211,7 +220,7 @@ private void addNextView() {
bottomView.measure(measureSpecWidth | width, measureSpecHeight | height);
addViewInLayout(bottomView, 0, params, true);

mCurrentViewIndex++;
mCurrentIndex++;
}
}

Expand Down Expand Up @@ -301,13 +310,15 @@ public void onSwipeEnd() {
}

public void onViewSwipedToLeft() {
if (mListener != null) mListener.onViewSwipedToLeft(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);
}

/**
Expand All @@ -316,7 +327,7 @@ public void onViewSwipedToRight() {
* @return The current position.
*/
public int getCurrentPosition() {
return mCurrentViewIndex - getChildCount();
return (mCurrentIndex - getChildCount()) % mAdapter.getCount();
}

/**
Expand Down Expand Up @@ -410,7 +421,7 @@ public void swipeTopViewToLeft() {
* Resets the current adapter position and repopulates the stack.
*/
public void resetStack() {
mCurrentViewIndex = 0;
mCurrentIndex = 0;
removeAllViewsInLayout();
requestLayout();
}
Expand Down
1 change: 1 addition & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
<attr name="swipe_opacity" format="float"/>
<attr name="scale_factor" format="float"/>
<attr name="disable_hw_acceleration" format="boolean"/>
<attr name="looped" format="boolean"/>
</declare-styleable>
</resources>