Skip to content

Instantly share code, notes, and snippets.

@chris-carneiro
Created January 17, 2018 16:45
Show Gist options
  • Save chris-carneiro/38ec5d0c5829997db49c836b7b09ccf5 to your computer and use it in GitHub Desktop.
Save chris-carneiro/38ec5d0c5829997db49c836b7b09ccf5 to your computer and use it in GitHub Desktop.
Endless autoscrolling for viewPager
public static ObjectAnimator scrollDownSmoothlyIndefinitely(@NonNull WeakReference<NestedScrollView> scrollViewRef, int scrollingDuration) {
NestedScrollView scrollView = scrollViewRef.get();
if (scrollView != null) {
final ObjectAnimator animScrollDown = ObjectAnimator.ofInt(scrollView, "scrollY",
scrollView.getChildAt(0).getHeight());
animScrollDown.setInterpolator(new LinearInterpolator());
animScrollDown.setDuration(scrollingDuration);
final ObjectAnimator animScrollToTop = ObjectAnimator.ofInt(scrollView, "scrollY",
0);
animScrollToTop.setDuration(500);
animScrollDown.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animator) {
}
@Override
public void onAnimationEnd(Animator animator) {
if (!animScrollToTop.isStarted()) {
animScrollToTop.start();
}
}
@Override
public void onAnimationCancel(Animator animator) {
}
@Override
public void onAnimationRepeat(Animator animator) {
}
});
animScrollToTop.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animator) {
}
@Override
public void onAnimationEnd(Animator animator) {
if (!animScrollDown.isStarted()) {
animScrollDown.start();
}
}
@Override
public void onAnimationCancel(Animator animator) {
}
@Override
public void onAnimationRepeat(Animator animator) {
}
});
if (!animScrollDown.isStarted()) {
animScrollDown.start();
}
return animScrollDown;
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment