@@ -44,6 +44,7 @@ public class SwipeStack extends ViewGroup {
44
44
public static final float DEFAULT_SWIPE_OPACITY = 1f ;
45
45
public static final float DEFAULT_SCALE_FACTOR = 1f ;
46
46
public static final boolean DEFAULT_DISABLE_HW_ACCELERATION = true ;
47
+ public static final boolean DEFAULT_IS_LOOPED = false ;
47
48
48
49
private static final String KEY_SUPER_STATE = "superState" ;
49
50
private static final String KEY_CURRENT_INDEX = "currentIndex" ;
@@ -53,7 +54,7 @@ public class SwipeStack extends ViewGroup {
53
54
54
55
private int mAllowedSwipeDirections ;
55
56
private int mAnimationDuration ;
56
- private int mCurrentViewIndex ;
57
+ private int mCurrentIndex ;
57
58
private int mNumberOfStackedViews ;
58
59
private int mViewSpacing ;
59
60
private int mViewRotation ;
@@ -62,6 +63,7 @@ public class SwipeStack extends ViewGroup {
62
63
private float mScaleFactor ;
63
64
private boolean mDisableHwAcceleration ;
64
65
private boolean mIsFirstLayout = true ;
66
+ private boolean mIsLooped ;
65
67
66
68
private View mTopView ;
67
69
private SwipeHelper mSwipeHelper ;
@@ -109,6 +111,9 @@ private void readAttributes(AttributeSet attributeSet) {
109
111
mDisableHwAcceleration =
110
112
attrs .getBoolean (R .styleable .SwipeStack_disable_hw_acceleration ,
111
113
DEFAULT_DISABLE_HW_ACCELERATION );
114
+ mIsLooped =
115
+ attrs .getBoolean (R .styleable .SwipeStack_looped ,
116
+ DEFAULT_IS_LOOPED );
112
117
} finally {
113
118
attrs .recycle ();
114
119
}
@@ -139,32 +144,36 @@ public void onChanged() {
139
144
public Parcelable onSaveInstanceState () {
140
145
Bundle bundle = new Bundle ();
141
146
bundle .putParcelable (KEY_SUPER_STATE , super .onSaveInstanceState ());
142
- bundle .putInt (KEY_CURRENT_INDEX , mCurrentViewIndex - getChildCount ());
147
+ bundle .putInt (KEY_CURRENT_INDEX , mCurrentIndex - getChildCount ());
143
148
return bundle ;
144
149
}
145
150
146
151
@ Override
147
152
public void onRestoreInstanceState (Parcelable state ) {
148
153
if (state instanceof Bundle ) {
149
154
Bundle bundle = (Bundle ) state ;
150
- mCurrentViewIndex = bundle .getInt (KEY_CURRENT_INDEX );
155
+ mCurrentIndex = bundle .getInt (KEY_CURRENT_INDEX );
151
156
state = bundle .getParcelable (KEY_SUPER_STATE );
152
157
}
153
158
154
159
super .onRestoreInstanceState (state );
155
160
}
156
161
162
+ private int getCurrentViewIndex () {
163
+ return mCurrentIndex % mAdapter .getCount ();
164
+ }
165
+
157
166
@ Override
158
167
protected void onLayout (boolean changed , int l , int t , int r , int b ) {
159
168
160
169
if (mAdapter == null || mAdapter .isEmpty ()) {
161
- mCurrentViewIndex = 0 ;
170
+ mCurrentIndex = 0 ;
162
171
removeAllViewsInLayout ();
163
172
return ;
164
173
}
165
174
166
175
for (int x = getChildCount ();
167
- x < mNumberOfStackedViews && mCurrentViewIndex < mAdapter .getCount ();
176
+ x < mNumberOfStackedViews && ( mIsLooped || mCurrentIndex < mAdapter .getCount () );
168
177
x ++) {
169
178
addNextView ();
170
179
}
@@ -175,8 +184,8 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) {
175
184
}
176
185
177
186
private void addNextView () {
178
- if (mCurrentViewIndex < mAdapter .getCount ()) {
179
- View bottomView = mAdapter .getView (mCurrentViewIndex , null , this );
187
+ if (getCurrentViewIndex () < mAdapter .getCount ()) {
188
+ View bottomView = mAdapter .getView (getCurrentViewIndex () , null , this );
180
189
bottomView .setTag (R .id .new_view , true );
181
190
182
191
if (!mDisableHwAcceleration ) {
@@ -211,7 +220,7 @@ private void addNextView() {
211
220
bottomView .measure (measureSpecWidth | width , measureSpecHeight | height );
212
221
addViewInLayout (bottomView , 0 , params , true );
213
222
214
- mCurrentViewIndex ++;
223
+ mCurrentIndex ++;
215
224
}
216
225
}
217
226
@@ -301,7 +310,7 @@ public void onSwipeEnd() {
301
310
}
302
311
303
312
public void onViewSwipedToLeft () {
304
- if (mListener != null ) mListener .onViewSwipedToLeft (getCurrentPosition ());
313
+ if (mListener != null ) mListener .onViewSwipedToRight (getCurrentPosition ());
305
314
removeTopView ();
306
315
}
307
316
@@ -316,7 +325,7 @@ public void onViewSwipedToRight() {
316
325
* @return The current position.
317
326
*/
318
327
public int getCurrentPosition () {
319
- return mCurrentViewIndex - getChildCount ();
328
+ return ( mCurrentIndex - getChildCount ()) % mAdapter . getCount ();
320
329
}
321
330
322
331
/**
@@ -410,7 +419,7 @@ public void swipeTopViewToLeft() {
410
419
* Resets the current adapter position and repopulates the stack.
411
420
*/
412
421
public void resetStack () {
413
- mCurrentViewIndex = 0 ;
422
+ mCurrentIndex = 0 ;
414
423
removeAllViewsInLayout ();
415
424
requestLayout ();
416
425
}
0 commit comments