I wish to move smoothly to next scroll position in my custom view (on button press or event). How to implement this? First of all I can't see scroll animation class (only alpha, rotate, scale and translate). Secondly, having animation class, I can't see iterative one (say to scroll 100 pixels rights whatever position we have) only absolute ones (i.e. to animate from one constant value to another).
4 Answers
Assuming you are using a ScrollView, does smoothScrollTo(...) work for you?
http://developer.android.com/reference/android/widget/ScrollView.html#smoothScrollTo%28int,%20int%29

- 4,774
- 3
- 27
- 27
-
Should I use `ScrollView`? I am using `MyClass extends View` now. – Suzan Cioc May 16 '12 at 15:18
-
I think it depends on what you are doing, if a ScrollView works for you then definetly use it. – Ian Warwick May 16 '12 at 15:26
-
I have tried extending `ScrollView` but found that `onScrollChanged()` doesn't called in the case. I need this method to be called since I calculate some parameters in it. I.e. `smoothScroll` doesn't work for me. – Suzan Cioc May 16 '12 at 16:38
-
I assume you have subclassed ScrollView and overriden onScrollChanged? – Ian Warwick May 17 '12 at 07:06
-
This also works in a `RecyclerView`, with `smoothScrollToPosition()`. – Ben Grant Jan 11 '18 at 02:54
-
Smooth scroll isn't so smooth. I am using @anton46's answer as that allows you to specify the duration of the scrool: https://stackoverflow.com/a/21574048/1617737 – ban-geoengineering Mar 08 '19 at 19:05
Using ObjectAnimator
, this is a sample for scrolling to top:
public void scrollToTop() {
int x = 0;
int y = 0;
ObjectAnimator xTranslate = ObjectAnimator.ofInt(mScrollView, "scrollX", x);
ObjectAnimator yTranslate = ObjectAnimator.ofInt(mScrollView, "scrollY", y);
AnimatorSet animators = new AnimatorSet();
animators.setDuration(1000L);
animators.playTogether(xTranslate, yTranslate);
animators.addListener(new AnimatorListener() {
@Override
public void onAnimationStart(Animator arg0) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationRepeat(Animator arg0) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animator arg0) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationCancel(Animator arg0) {
// TODO Auto-generated method stub
}
});
animators.start();
}
Animating scroll is done through a combination of using Scroller/OverScroller (to compute the time interpolated values of your scroll offsets), GestureDetectors (to start the scroller object) and the onComputeScroll method of a View (which implicitly is your animation loop).
The official android docs now have a detailed tutorial on precisely this topic. http://developer.android.com/training/gestures/scroll.html

- 19,394
- 9
- 63
- 66
-
1that is explaining how to implement a ScrollView from scratch! that's not the answer – Shayan_Aryan Jun 23 '21 at 14:57
See the view_cache_demo sample code to see how to do animated scrolling. It works in 2D, caches complex drawing and also handles fling gestures, but you can simplify all that as necessary.

- 2,768
- 1
- 15
- 13