Skip to content

Commit 1a7b6d3

Browse files
authored
Merge pull request #114 from gildor/fix-for-issue-100
Try to fix issue #100
2 parents 6255482 + f4d5fa0 commit 1a7b6d3

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

lib/src/main/java/com/lsjwzh/widget/recyclerviewpager/RecyclerViewPager.java

+17-15
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ public void scrollToPosition(int position) {
238238
super.scrollToPosition(position);
239239

240240
getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
241+
@SuppressWarnings("deprecation")
241242
@Override
242243
public void onGlobalLayout() {
243244
if (Build.VERSION.SDK_INT < 16) {
@@ -246,7 +247,7 @@ public void onGlobalLayout() {
246247
getViewTreeObserver().removeOnGlobalLayoutListener(this);
247248
}
248249

249-
if (mSmoothScrollTargetPosition >= 0 && mSmoothScrollTargetPosition < mViewPagerAdapter.getItemCount()) {
250+
if (mSmoothScrollTargetPosition >= 0 && mSmoothScrollTargetPosition < getItemCount()) {
250251
if (mOnPageChangedListeners != null) {
251252
for (OnPageChangedListener onPageChangedListener : mOnPageChangedListeners) {
252253
if (onPageChangedListener != null) {
@@ -259,11 +260,15 @@ public void onGlobalLayout() {
259260
});
260261
}
261262

263+
private int getItemCount() {
264+
return mViewPagerAdapter == null ? 0 : mViewPagerAdapter.getItemCount();
265+
}
266+
262267
/**
263268
* get item position in center of viewpager
264269
*/
265270
public int getCurrentPosition() {
266-
int curPosition = -1;
271+
int curPosition;
267272
if (getLayoutManager().canScrollHorizontally()) {
268273
curPosition = ViewUtils.getCenterXChildPosition(this);
269274
} else {
@@ -296,17 +301,15 @@ protected void adjustPositionX(int velocityX) {
296301
}
297302
}
298303
targetPosition = Math.max(targetPosition, 0);
299-
targetPosition = Math.min(targetPosition, mViewPagerAdapter.getItemCount() - 1);
304+
targetPosition = Math.min(targetPosition, getItemCount() - 1);
300305
if (targetPosition == curPosition
301-
&& ((mSinglePageFling
302-
&& mPositionOnTouchDown == curPosition)
303-
|| !mSinglePageFling)) {
306+
&& (!mSinglePageFling || mPositionOnTouchDown == curPosition)) {
304307
View centerXChild = ViewUtils.getCenterXChild(this);
305308
if (centerXChild != null) {
306309
if (mTouchSpan > centerXChild.getWidth() * mTriggerOffset * mTriggerOffset && targetPosition != 0) {
307310
if (!reverseLayout) targetPosition--;
308311
else targetPosition++;
309-
} else if (mTouchSpan < centerXChild.getWidth() * -mTriggerOffset && targetPosition != mViewPagerAdapter.getItemCount() - 1) {
312+
} else if (mTouchSpan < centerXChild.getWidth() * -mTriggerOffset && targetPosition != getItemCount() - 1) {
310313
if (!reverseLayout) targetPosition++;
311314
else targetPosition--;
312315
}
@@ -316,7 +319,7 @@ protected void adjustPositionX(int velocityX) {
316319
Log.d("@", "mTouchSpan:" + mTouchSpan);
317320
Log.d("@", "adjustPositionX:" + targetPosition);
318321
}
319-
smoothScrollToPosition(safeTargetPosition(targetPosition, mViewPagerAdapter.getItemCount()));
322+
smoothScrollToPosition(safeTargetPosition(targetPosition, getItemCount()));
320323
}
321324
}
322325

@@ -357,17 +360,15 @@ protected void adjustPositionY(int velocityY) {
357360
}
358361

359362
targetPosition = Math.max(targetPosition, 0);
360-
targetPosition = Math.min(targetPosition, mViewPagerAdapter.getItemCount() - 1);
363+
targetPosition = Math.min(targetPosition, getItemCount() - 1);
361364
if (targetPosition == curPosition
362-
&& ((mSinglePageFling
363-
&& mPositionOnTouchDown == curPosition)
364-
|| !mSinglePageFling)) {
365+
&& (!mSinglePageFling || mPositionOnTouchDown == curPosition)) {
365366
View centerYChild = ViewUtils.getCenterYChild(this);
366367
if (centerYChild != null) {
367368
if (mTouchSpan > centerYChild.getHeight() * mTriggerOffset && targetPosition != 0) {
368369
if (!reverseLayout) targetPosition--;
369370
else targetPosition++;
370-
} else if (mTouchSpan < centerYChild.getHeight() * -mTriggerOffset && targetPosition != mViewPagerAdapter.getItemCount() - 1) {
371+
} else if (mTouchSpan < centerYChild.getHeight() * -mTriggerOffset && targetPosition != getItemCount() - 1) {
371372
if (!reverseLayout) targetPosition++;
372373
else targetPosition--;
373374
}
@@ -377,7 +378,7 @@ protected void adjustPositionY(int velocityY) {
377378
Log.d("@", "mTouchSpan:" + mTouchSpan);
378379
Log.d("@", "adjustPositionY:" + targetPosition);
379380
}
380-
smoothScrollToPosition(safeTargetPosition(targetPosition, mViewPagerAdapter.getItemCount()));
381+
smoothScrollToPosition(safeTargetPosition(targetPosition, getItemCount()));
381382
}
382383
}
383384

@@ -469,7 +470,7 @@ public void onScrollStateChanged(int state) {
469470
}
470471
}
471472
}
472-
smoothScrollToPosition(safeTargetPosition(targetPosition, mViewPagerAdapter.getItemCount()));
473+
smoothScrollToPosition(safeTargetPosition(targetPosition, getItemCount()));
473474
mCurView = null;
474475
} else if (mSmoothScrollTargetPosition != mPositionBeforeScroll) {
475476
if (DEBUG) {
@@ -493,6 +494,7 @@ public void onScrollStateChanged(int state) {
493494
}
494495
}
495496

497+
@SuppressWarnings("unchecked")
496498
@NonNull
497499
protected RecyclerViewPagerAdapter ensureRecyclerViewPagerAdapter(Adapter adapter) {
498500
return (adapter instanceof RecyclerViewPagerAdapter)

0 commit comments

Comments
 (0)