Skip to content

Commit def8389

Browse files
Fixed bug when switching pages before the current page change animation is finished
1 parent 13987de commit def8389

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

app/src/main/java/com/heinrichreimersoftware/materialintro/demo/CanteenIntroActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ protected void onCreate(Bundle savedInstanceState) {
2424
label.setSpan(labelSpan, 0, label.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
2525
setButtonCtaLabel(label);
2626

27-
setPageScrollDuration(800);
27+
setPageScrollDuration(500);
2828
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
2929
setPagerInterpolator(android.R.interpolator.fast_out_slow_in);
3030
}

library/src/main/java/com/heinrichreimersoftware/materialintro/app/IntroActivity.java

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,9 @@ public void onClick(View v) {
287287
}
288288

289289
private void smoothScrollPagerTo(final int position) {
290+
if (pager.isFakeDragging())
291+
return;
292+
290293
ValueAnimator animator = ValueAnimator.ofFloat(pager.getCurrentItem(), position);
291294
animator.addListener(new AnimatorListenerAdapter() {
292295
@Override
@@ -329,11 +332,17 @@ else if (position < currentPosition && Math.ceil(position) != currentPosition &&
329332
}
330333
});
331334

335+
int distance = Math.abs(position - pager.getCurrentItem());
336+
332337
animator.setInterpolator(pageScrollInterpolator);
333-
animator.setDuration(pageScrollDuration);
338+
animator.setDuration(calculateScrollDuration(distance));
334339
animator.start();
335340
}
336341

342+
private long calculateScrollDuration(int distance) {
343+
return Math.round(pageScrollDuration * (distance + Math.sqrt(distance)) / 2);
344+
}
345+
337346
public void nextSlide() {
338347
int currentItem = pager.getCurrentItem();
339348
if (currentItem > adapter.getCount() - 1) finishIfNeeded();
@@ -343,16 +352,15 @@ public void nextSlide() {
343352
}
344353
else {
345354
AnimUtils.applyShakeAnimation(this, buttonNext);
346-
347355
}
348356
}
349357

350-
private boolean nextSlideAuto() {
358+
private int nextSlideAuto() {
351359
int endPosition = pager.getCurrentItem();
352360
int count = getCount();
353361

354362
if (count == 1) {
355-
return false;
363+
return 0;
356364
}
357365
else if (pager.getCurrentItem() >= count - 1) {
358366
while (endPosition >= 0 && canGoBackward(endPosition, true)) {
@@ -365,12 +373,16 @@ else if (canGoForward(endPosition, true)) {
365373
endPosition++;
366374
}
367375

376+
int distance = Math.abs(endPosition - pager.getCurrentItem());
377+
368378
if (endPosition == pager.getCurrentItem())
369-
return false;
379+
return 0;
370380

371381
smoothScrollPagerTo(endPosition);
372382

373-
return autoplayCounter != 0;
383+
if (autoplayCounter == 0)
384+
return 0;
385+
return distance;
374386

375387
}
376388

@@ -793,8 +805,9 @@ public void run() {
793805
cancelAutoplay();
794806
return;
795807
}
796-
if (nextSlideAuto())
797-
autoplayHandler.postDelayed(autoplayCallback, autoplayDelay);
808+
int distance = nextSlideAuto();
809+
if (distance != 0)
810+
autoplayHandler.postDelayed(autoplayCallback, autoplayDelay + calculateScrollDuration(distance));
798811
}
799812
};
800813
autoplayHandler.postDelayed(autoplayCallback, autoplayDelay);

0 commit comments

Comments
 (0)