How do I add rebound effect to HorizontalScrollView when I press right direction key in android?

343 Views Asked by At

I want to add some rebound effect when HorizontalScrollView scroll to right edge. For example, a view like this:

--------------------------------------------------------------------------------------
|                                     Content                                        |
|                                     Content                                        |
|                                     Content                                        |
|                                     Content                                        |
|                                     Content                                        |
--------------------------------------------------------------------------------------

With the normal HorizontalScrollView, I press right direction key, this view will have no effect, but I want to this view will rebound like a jelly.

How can I implement this kind animation?

1

There are 1 best solutions below

1
On

For HorizontalScrollView, if it's width is bigger than it's child view, it won't scroll, so there's no edge effect. You can try this to add rebound effect.

public class OverScrollHorizontalScrollView extends HorizontalScrollView {
    ...
    public OverScrollHorizontalScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setOverScrollMode(OVER_SCROLL_ALWAYS);
    }
    ...
    @Override
    protected boolean overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY,
                                   int scrollRangeX, int scrollRangeY, int maxOverScrollX,
                                   int maxOverScrollY, boolean isTouchEvent) {
        maxOverScrollX = 90; // this is the rebound distance, set yours
        return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY,
                maxOverScrollX, maxOverScrollY, isTouchEvent);
    }
    ...
}