public

VerticalSmoothScrollView from http://stackoverflow.com/questions/5193678/android-horizontalscrollview-smoothscroll-animation-time/13639106 Added vertical computation. NOTE: to use with API 8 or lower you need to find a compat Scroller, ViewPagerIndicator is a good source to look for a compat Scroller.

  • Download Gist
VerticalSmoothScrollView.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
public class VerticalSmoothScrollView extends ScrollView
{
private OverScroller myScroller; //TODO implement our own scroller, Scroller and OverScroller can usually be swapped
 
 
public VerticalSmoothScrollView( Context context, AttributeSet attrs, int defStyle )
{
super( context, attrs, defStyle );
init();
}
public VerticalSmoothScrollView( Context context, AttributeSet attrs )
{
super( context, attrs);
init();
}
public VerticalSmoothScrollView( Context context)
{
super( context );
init();
}
private void init()
{
try
{
Class parent = this.getClass();
do
{
parent = parent.getSuperclass();
} while (!parent.getName().equals("android.widget.ScrollView"));
 
Log.i("Scroller", "class: " + parent.getName());
Field field = parent.getDeclaredField("mScroller");
field.setAccessible(true);
myScroller = (OverScroller) field.get(this);
 
} catch (NoSuchFieldException e)
{
e.printStackTrace();
} catch (IllegalArgumentException e)
{
e.printStackTrace();
} catch (IllegalAccessException e)
{
e.printStackTrace();
}
}
 
/**
* TODO! OverScroller is API 9 and NOT subclass of Scroller
*/
@SuppressLint("NewApi")
public void customSmoothScrollBy(int dx, int dy)
{
if (myScroller == null)
{
smoothScrollBy(dx, dy);
return;
}
 
if (getChildCount() == 0)
return;
 
final int width = getWidth() - getPaddingRight() - getPaddingLeft();
final int right = getChildAt(0).getWidth();
final int maxX = Math.max(0, right - width);
final int scrollX = getScrollX();
final int height = getHeight() - getPaddingTop() - getPaddingBottom();
final int bottom = getChildAt(0).getBottom();
final int maxY = Math.max(0, bottom - height);
final int scrollY = getScrollY();
dx = Math.max(0, Math.min(scrollX + dx, maxX)) - scrollX;
dy = Math.max(0, Math.min(scrollY + dy, maxY)) - scrollY;
 
myScroller.startScroll(scrollX, scrollY, dx, dy, 1500);
invalidate();
}
 
/**
* TODO! OverScroller is API 9 and NOT subclass of Scroller
*/
public void customSmoothScrollTo(int x, int y)
{
customSmoothScrollBy(x - getScrollX(), y - getScrollY());
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.