Skip to content

Commit

Permalink
Behavior change: page scroll change now reacts to the distance to scr…
Browse files Browse the repository at this point in the history
  • Loading branch information
heinrichreimer committed Jun 24, 2016
1 parent 13987de commit def8389
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected void onCreate(Bundle savedInstanceState) {
label.setSpan(labelSpan, 0, label.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
setButtonCtaLabel(label);

setPageScrollDuration(800);
setPageScrollDuration(500);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
setPagerInterpolator(android.R.interpolator.fast_out_slow_in);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,9 @@ public void onClick(View v) {
}

private void smoothScrollPagerTo(final int position) {
if (pager.isFakeDragging())
return;

ValueAnimator animator = ValueAnimator.ofFloat(pager.getCurrentItem(), position);
animator.addListener(new AnimatorListenerAdapter() {
@Override
Expand Down Expand Up @@ -329,11 +332,17 @@ else if (position < currentPosition && Math.ceil(position) != currentPosition &&
}
});

int distance = Math.abs(position - pager.getCurrentItem());

animator.setInterpolator(pageScrollInterpolator);
animator.setDuration(pageScrollDuration);
animator.setDuration(calculateScrollDuration(distance));
animator.start();
}

private long calculateScrollDuration(int distance) {
return Math.round(pageScrollDuration * (distance + Math.sqrt(distance)) / 2);
}

public void nextSlide() {
int currentItem = pager.getCurrentItem();
if (currentItem > adapter.getCount() - 1) finishIfNeeded();
Expand All @@ -343,16 +352,15 @@ public void nextSlide() {
}
else {
AnimUtils.applyShakeAnimation(this, buttonNext);

}
}

private boolean nextSlideAuto() {
private int nextSlideAuto() {
int endPosition = pager.getCurrentItem();
int count = getCount();

if (count == 1) {
return false;
return 0;
}
else if (pager.getCurrentItem() >= count - 1) {
while (endPosition >= 0 && canGoBackward(endPosition, true)) {
Expand All @@ -365,12 +373,16 @@ else if (canGoForward(endPosition, true)) {
endPosition++;
}

int distance = Math.abs(endPosition - pager.getCurrentItem());

if (endPosition == pager.getCurrentItem())
return false;
return 0;

smoothScrollPagerTo(endPosition);

return autoplayCounter != 0;
if (autoplayCounter == 0)
return 0;
return distance;

}

Expand Down Expand Up @@ -793,8 +805,9 @@ public void run() {
cancelAutoplay();
return;
}
if (nextSlideAuto())
autoplayHandler.postDelayed(autoplayCallback, autoplayDelay);
int distance = nextSlideAuto();
if (distance != 0)
autoplayHandler.postDelayed(autoplayCallback, autoplayDelay + calculateScrollDuration(distance));
}
};
autoplayHandler.postDelayed(autoplayCallback, autoplayDelay);
Expand Down

0 comments on commit def8389

Please sign in to comment.