Skip to content

Commit

Permalink
Merge pull request #114 from gildor/fix-for-issue-100
Browse files Browse the repository at this point in the history
Try to fix issue #100
  • Loading branch information
lsjwzh authored Aug 12, 2016
2 parents 6255482 + f4d5fa0 commit 1a7b6d3
Showing 1 changed file with 17 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ public void scrollToPosition(int position) {
super.scrollToPosition(position);

getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@SuppressWarnings("deprecation")
@Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT < 16) {
Expand All @@ -246,7 +247,7 @@ public void onGlobalLayout() {
getViewTreeObserver().removeOnGlobalLayoutListener(this);
}

if (mSmoothScrollTargetPosition >= 0 && mSmoothScrollTargetPosition < mViewPagerAdapter.getItemCount()) {
if (mSmoothScrollTargetPosition >= 0 && mSmoothScrollTargetPosition < getItemCount()) {
if (mOnPageChangedListeners != null) {
for (OnPageChangedListener onPageChangedListener : mOnPageChangedListeners) {
if (onPageChangedListener != null) {
Expand All @@ -259,11 +260,15 @@ public void onGlobalLayout() {
});
}

private int getItemCount() {
return mViewPagerAdapter == null ? 0 : mViewPagerAdapter.getItemCount();
}

/**
* get item position in center of viewpager
*/
public int getCurrentPosition() {
int curPosition = -1;
int curPosition;
if (getLayoutManager().canScrollHorizontally()) {
curPosition = ViewUtils.getCenterXChildPosition(this);
} else {
Expand Down Expand Up @@ -296,17 +301,15 @@ protected void adjustPositionX(int velocityX) {
}
}
targetPosition = Math.max(targetPosition, 0);
targetPosition = Math.min(targetPosition, mViewPagerAdapter.getItemCount() - 1);
targetPosition = Math.min(targetPosition, getItemCount() - 1);
if (targetPosition == curPosition
&& ((mSinglePageFling
&& mPositionOnTouchDown == curPosition)
|| !mSinglePageFling)) {
&& (!mSinglePageFling || mPositionOnTouchDown == curPosition)) {
View centerXChild = ViewUtils.getCenterXChild(this);
if (centerXChild != null) {
if (mTouchSpan > centerXChild.getWidth() * mTriggerOffset * mTriggerOffset && targetPosition != 0) {
if (!reverseLayout) targetPosition--;
else targetPosition++;
} else if (mTouchSpan < centerXChild.getWidth() * -mTriggerOffset && targetPosition != mViewPagerAdapter.getItemCount() - 1) {
} else if (mTouchSpan < centerXChild.getWidth() * -mTriggerOffset && targetPosition != getItemCount() - 1) {
if (!reverseLayout) targetPosition++;
else targetPosition--;
}
Expand All @@ -316,7 +319,7 @@ protected void adjustPositionX(int velocityX) {
Log.d("@", "mTouchSpan:" + mTouchSpan);
Log.d("@", "adjustPositionX:" + targetPosition);
}
smoothScrollToPosition(safeTargetPosition(targetPosition, mViewPagerAdapter.getItemCount()));
smoothScrollToPosition(safeTargetPosition(targetPosition, getItemCount()));
}
}

Expand Down Expand Up @@ -357,17 +360,15 @@ protected void adjustPositionY(int velocityY) {
}

targetPosition = Math.max(targetPosition, 0);
targetPosition = Math.min(targetPosition, mViewPagerAdapter.getItemCount() - 1);
targetPosition = Math.min(targetPosition, getItemCount() - 1);
if (targetPosition == curPosition
&& ((mSinglePageFling
&& mPositionOnTouchDown == curPosition)
|| !mSinglePageFling)) {
&& (!mSinglePageFling || mPositionOnTouchDown == curPosition)) {
View centerYChild = ViewUtils.getCenterYChild(this);
if (centerYChild != null) {
if (mTouchSpan > centerYChild.getHeight() * mTriggerOffset && targetPosition != 0) {
if (!reverseLayout) targetPosition--;
else targetPosition++;
} else if (mTouchSpan < centerYChild.getHeight() * -mTriggerOffset && targetPosition != mViewPagerAdapter.getItemCount() - 1) {
} else if (mTouchSpan < centerYChild.getHeight() * -mTriggerOffset && targetPosition != getItemCount() - 1) {
if (!reverseLayout) targetPosition++;
else targetPosition--;
}
Expand All @@ -377,7 +378,7 @@ protected void adjustPositionY(int velocityY) {
Log.d("@", "mTouchSpan:" + mTouchSpan);
Log.d("@", "adjustPositionY:" + targetPosition);
}
smoothScrollToPosition(safeTargetPosition(targetPosition, mViewPagerAdapter.getItemCount()));
smoothScrollToPosition(safeTargetPosition(targetPosition, getItemCount()));
}
}

Expand Down Expand Up @@ -469,7 +470,7 @@ public void onScrollStateChanged(int state) {
}
}
}
smoothScrollToPosition(safeTargetPosition(targetPosition, mViewPagerAdapter.getItemCount()));
smoothScrollToPosition(safeTargetPosition(targetPosition, getItemCount()));
mCurView = null;
} else if (mSmoothScrollTargetPosition != mPositionBeforeScroll) {
if (DEBUG) {
Expand All @@ -493,6 +494,7 @@ public void onScrollStateChanged(int state) {
}
}

@SuppressWarnings("unchecked")
@NonNull
protected RecyclerViewPagerAdapter ensureRecyclerViewPagerAdapter(Adapter adapter) {
return (adapter instanceof RecyclerViewPagerAdapter)
Expand Down

0 comments on commit 1a7b6d3

Please sign in to comment.