@@ -238,6 +238,7 @@ public void scrollToPosition(int position) {
238
238
super .scrollToPosition (position );
239
239
240
240
getViewTreeObserver ().addOnGlobalLayoutListener (new ViewTreeObserver .OnGlobalLayoutListener () {
241
+ @ SuppressWarnings ("deprecation" )
241
242
@ Override
242
243
public void onGlobalLayout () {
243
244
if (Build .VERSION .SDK_INT < 16 ) {
@@ -246,7 +247,7 @@ public void onGlobalLayout() {
246
247
getViewTreeObserver ().removeOnGlobalLayoutListener (this );
247
248
}
248
249
249
- if (mSmoothScrollTargetPosition >= 0 && mSmoothScrollTargetPosition < mViewPagerAdapter . getItemCount ()) {
250
+ if (mSmoothScrollTargetPosition >= 0 && mSmoothScrollTargetPosition < getItemCount ()) {
250
251
if (mOnPageChangedListeners != null ) {
251
252
for (OnPageChangedListener onPageChangedListener : mOnPageChangedListeners ) {
252
253
if (onPageChangedListener != null ) {
@@ -259,11 +260,15 @@ public void onGlobalLayout() {
259
260
});
260
261
}
261
262
263
+ private int getItemCount () {
264
+ return mViewPagerAdapter == null ? 0 : mViewPagerAdapter .getItemCount ();
265
+ }
266
+
262
267
/**
263
268
* get item position in center of viewpager
264
269
*/
265
270
public int getCurrentPosition () {
266
- int curPosition = - 1 ;
271
+ int curPosition ;
267
272
if (getLayoutManager ().canScrollHorizontally ()) {
268
273
curPosition = ViewUtils .getCenterXChildPosition (this );
269
274
} else {
@@ -296,17 +301,15 @@ protected void adjustPositionX(int velocityX) {
296
301
}
297
302
}
298
303
targetPosition = Math .max (targetPosition , 0 );
299
- targetPosition = Math .min (targetPosition , mViewPagerAdapter . getItemCount () - 1 );
304
+ targetPosition = Math .min (targetPosition , getItemCount () - 1 );
300
305
if (targetPosition == curPosition
301
- && ((mSinglePageFling
302
- && mPositionOnTouchDown == curPosition )
303
- || !mSinglePageFling )) {
306
+ && (!mSinglePageFling || mPositionOnTouchDown == curPosition )) {
304
307
View centerXChild = ViewUtils .getCenterXChild (this );
305
308
if (centerXChild != null ) {
306
309
if (mTouchSpan > centerXChild .getWidth () * mTriggerOffset * mTriggerOffset && targetPosition != 0 ) {
307
310
if (!reverseLayout ) targetPosition --;
308
311
else targetPosition ++;
309
- } else if (mTouchSpan < centerXChild .getWidth () * -mTriggerOffset && targetPosition != mViewPagerAdapter . getItemCount () - 1 ) {
312
+ } else if (mTouchSpan < centerXChild .getWidth () * -mTriggerOffset && targetPosition != getItemCount () - 1 ) {
310
313
if (!reverseLayout ) targetPosition ++;
311
314
else targetPosition --;
312
315
}
@@ -316,7 +319,7 @@ protected void adjustPositionX(int velocityX) {
316
319
Log .d ("@" , "mTouchSpan:" + mTouchSpan );
317
320
Log .d ("@" , "adjustPositionX:" + targetPosition );
318
321
}
319
- smoothScrollToPosition (safeTargetPosition (targetPosition , mViewPagerAdapter . getItemCount ()));
322
+ smoothScrollToPosition (safeTargetPosition (targetPosition , getItemCount ()));
320
323
}
321
324
}
322
325
@@ -357,17 +360,15 @@ protected void adjustPositionY(int velocityY) {
357
360
}
358
361
359
362
targetPosition = Math .max (targetPosition , 0 );
360
- targetPosition = Math .min (targetPosition , mViewPagerAdapter . getItemCount () - 1 );
363
+ targetPosition = Math .min (targetPosition , getItemCount () - 1 );
361
364
if (targetPosition == curPosition
362
- && ((mSinglePageFling
363
- && mPositionOnTouchDown == curPosition )
364
- || !mSinglePageFling )) {
365
+ && (!mSinglePageFling || mPositionOnTouchDown == curPosition )) {
365
366
View centerYChild = ViewUtils .getCenterYChild (this );
366
367
if (centerYChild != null ) {
367
368
if (mTouchSpan > centerYChild .getHeight () * mTriggerOffset && targetPosition != 0 ) {
368
369
if (!reverseLayout ) targetPosition --;
369
370
else targetPosition ++;
370
- } else if (mTouchSpan < centerYChild .getHeight () * -mTriggerOffset && targetPosition != mViewPagerAdapter . getItemCount () - 1 ) {
371
+ } else if (mTouchSpan < centerYChild .getHeight () * -mTriggerOffset && targetPosition != getItemCount () - 1 ) {
371
372
if (!reverseLayout ) targetPosition ++;
372
373
else targetPosition --;
373
374
}
@@ -377,7 +378,7 @@ protected void adjustPositionY(int velocityY) {
377
378
Log .d ("@" , "mTouchSpan:" + mTouchSpan );
378
379
Log .d ("@" , "adjustPositionY:" + targetPosition );
379
380
}
380
- smoothScrollToPosition (safeTargetPosition (targetPosition , mViewPagerAdapter . getItemCount ()));
381
+ smoothScrollToPosition (safeTargetPosition (targetPosition , getItemCount ()));
381
382
}
382
383
}
383
384
@@ -469,7 +470,7 @@ public void onScrollStateChanged(int state) {
469
470
}
470
471
}
471
472
}
472
- smoothScrollToPosition (safeTargetPosition (targetPosition , mViewPagerAdapter . getItemCount ()));
473
+ smoothScrollToPosition (safeTargetPosition (targetPosition , getItemCount ()));
473
474
mCurView = null ;
474
475
} else if (mSmoothScrollTargetPosition != mPositionBeforeScroll ) {
475
476
if (DEBUG ) {
@@ -493,6 +494,7 @@ public void onScrollStateChanged(int state) {
493
494
}
494
495
}
495
496
497
+ @ SuppressWarnings ("unchecked" )
496
498
@ NonNull
497
499
protected RecyclerViewPagerAdapter ensureRecyclerViewPagerAdapter (Adapter adapter ) {
498
500
return (adapter instanceof RecyclerViewPagerAdapter )
0 commit comments