10

I'm looking into adding overscrolling (not the color changing) into my project, but even after reading the API docs and several questions on here I still don't really understand what I need to do to get it to work.

According to the API doc for OverScroller: "this class encapsulates scrolling with the ability to overshoot the bounds of a scrolling operation. This class is a drop-in replacement for Scroller in most cases."

I had a Scroller object before in my parent class:

public boolean onTouchEvent(MotionEvent ev) {   
    ....// code to calculate deltas
     if (deltaY != 0 || deltaX != 0)
            scrollBy(deltaX, deltaY);
}

And I went to change it to this:

if (deltaY != 0 || deltaX != 0)
            overScrollBy(deltaX, deltaY, getScrollX(), getScrollY(),
                child.getRight() - getWidth(), child.getBottom() - getHeight(), 
                    OVERSCROLL_DISTANCE, OVERSCROLL_DISTANCE, false);

And now scrolling doesn't work at all! fling() works fine as a drop in replacement but not scroll....

In summary I have two questions:

  1. Why is scrolling not working with overScrollBy? Do I have to add additional code to make this work?
  2. Why do I have to call mScroller.fling() and only overScrollBy()? Why not mScroller.overScrollBy()? Is it somehow built into the View class?

This may be potentially be quite obvious, but I'm struggling here. Any help would be most appreciated, thanks!

kburbach
  • 761
  • 10
  • 26

2 Answers2

0

maybe because of this?

public class ViewConfiguration{
...
    /**
     * Max distance in dips to overscroll for edge effects
     */
    private static final int OVERSCROLL_DISTANCE = 0;
...
}
Wackaloon
  • 2,285
  • 1
  • 16
  • 33
0

About overScrollBy the documentation says:

Views that call this method should override onOverScrolled(int, int, boolean, boolean) to respond to the results of an over-scroll operation.

As you can find reading the source code, overScrollBy doesn't perform any scroll per se, but it calls onOverScrolled at the end.
So to make the scroll happens you should add to your View class something like:

@Override
protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
    scrollTo(scrollX, scrollY);
}

overScrollBy is a method of the View class, intended to scroll the view content (through onOverScrolled).
The OverScroller class just makes some calculations of scrolling, e.g. with mScroller.fling() or mScroller.springBack(), but needs a further call to actually scroll the view content, tipically with:

@Override
public void computeScroll() {
    if(mScroller.computeScrollOffset()) {
        scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
        invalidate();
    }
}
Salvador
  • 786
  • 8
  • 21