9
9
import android .os .Build ;
10
10
import android .os .Parcelable ;
11
11
import android .support .annotation .NonNull ;
12
+ import android .support .v7 .widget .LinearLayoutManager ;
12
13
import android .support .v7 .widget .RecyclerView ;
13
14
import android .util .AttributeSet ;
14
15
import android .util .Log ;
@@ -44,6 +45,7 @@ public class RecyclerViewPager extends RecyclerView {
44
45
int mMinTopWhenDragging = Integer .MAX_VALUE ;
45
46
private int mPositionOnTouchDown = -1 ;
46
47
private boolean mHasCalledOnPageChanged = true ;
48
+ private boolean reverseLayout = false ;
47
49
48
50
public RecyclerViewPager (Context context ) {
49
51
this (context , null );
@@ -138,6 +140,15 @@ public RecyclerViewPagerAdapter getWrapperAdapter() {
138
140
return mViewPagerAdapter ;
139
141
}
140
142
143
+ @ Override
144
+ public void setLayoutManager (LayoutManager layout ) {
145
+ super .setLayoutManager (layout );
146
+
147
+ if (layout instanceof LinearLayoutManager ) {
148
+ reverseLayout = ((LinearLayoutManager ) layout ).getReverseLayout ();
149
+ }
150
+ }
151
+
141
152
@ Override
142
153
public boolean fling (int velocityX , int velocityY ) {
143
154
boolean flinging = super .fling ((int ) (velocityX * mFlingFactor ), (int ) (velocityY * mFlingFactor ));
@@ -216,6 +227,8 @@ public int getCurrentPosition() {
216
227
* adjust position before Touch event complete and fling action start.
217
228
*/
218
229
protected void adjustPositionX (int velocityX ) {
230
+ if (reverseLayout ) velocityX *= -1 ;
231
+
219
232
int childCount = getChildCount ();
220
233
if (childCount > 0 ) {
221
234
int curPosition = ViewUtils .getCenterXChildPosition (this );
@@ -239,9 +252,11 @@ protected void adjustPositionX(int velocityX) {
239
252
View centerXChild = ViewUtils .getCenterXChild (this );
240
253
if (centerXChild != null ) {
241
254
if (mTouchSpan > centerXChild .getWidth () * mTriggerOffset * mTriggerOffset && targetPosition != 0 ) {
242
- targetPosition --;
255
+ if (!reverseLayout ) targetPosition --;
256
+ else targetPosition ++;
243
257
} else if (mTouchSpan < centerXChild .getWidth () * -mTriggerOffset && targetPosition != mViewPagerAdapter .getItemCount () - 1 ) {
244
- targetPosition ++;
258
+ if (!reverseLayout ) targetPosition ++;
259
+ else targetPosition --;
245
260
}
246
261
}
247
262
}
@@ -276,6 +291,8 @@ public void clearOnPageChangedListeners() {
276
291
* adjust position before Touch event complete and fling action start.
277
292
*/
278
293
protected void adjustPositionY (int velocityY ) {
294
+ if (reverseLayout ) velocityY *= -1 ;
295
+
279
296
int childCount = getChildCount ();
280
297
if (childCount > 0 ) {
281
298
int curPosition = ViewUtils .getCenterYChildPosition (this );
@@ -296,9 +313,11 @@ protected void adjustPositionY(int velocityY) {
296
313
View centerYChild = ViewUtils .getCenterYChild (this );
297
314
if (centerYChild != null ) {
298
315
if (mTouchSpan > centerYChild .getHeight () * mTriggerOffset && targetPosition != 0 ) {
299
- targetPosition --;
316
+ if (!reverseLayout ) targetPosition --;
317
+ else targetPosition ++;
300
318
} else if (mTouchSpan < centerYChild .getHeight () * -mTriggerOffset && targetPosition != mViewPagerAdapter .getItemCount () - 1 ) {
301
- targetPosition ++;
319
+ if (!reverseLayout ) targetPosition ++;
320
+ else targetPosition --;
302
321
}
303
322
}
304
323
}
@@ -381,16 +400,20 @@ public void onScrollStateChanged(int state) {
381
400
int spanX = mCurView .getLeft () - mFisrtLeftWhenDragging ;
382
401
// if user is tending to cancel paging action, don't perform position changing
383
402
if (spanX > mCurView .getWidth () * mTriggerOffset && mCurView .getLeft () >= mMaxLeftWhenDragging ) {
384
- targetPosition --;
403
+ if (!reverseLayout ) targetPosition --;
404
+ else targetPosition ++;
385
405
} else if (spanX < mCurView .getWidth () * -mTriggerOffset && mCurView .getLeft () <= mMinLeftWhenDragging ) {
386
- targetPosition ++;
406
+ if (!reverseLayout ) targetPosition ++;
407
+ else targetPosition --;
387
408
}
388
409
} else {
389
410
int spanY = mCurView .getTop () - mFirstTopWhenDragging ;
390
411
if (spanY > mCurView .getHeight () * mTriggerOffset && mCurView .getTop () >= mMaxTopWhenDragging ) {
391
- targetPosition --;
412
+ if (!reverseLayout ) targetPosition --;
413
+ else targetPosition ++;
392
414
} else if (spanY < mCurView .getHeight () * -mTriggerOffset && mCurView .getTop () <= mMinTopWhenDragging ) {
393
- targetPosition ++;
415
+ if (!reverseLayout ) targetPosition ++;
416
+ else targetPosition --;
394
417
}
395
418
}
396
419
}
0 commit comments