在ScrollView中使用onOverScrolled

时间:2013-11-22 13:30:36

标签: android scrollview

如何在onOverScrolled中使用ScrollView

protected void onOverScrolled (int scrollX, int scrollY, boolean clampedX, boolean clampedY)

clampedXclampedY布尔确实会告诉我是否发生过度滚动,但滚动值没有说明过多滚动(它们总是0或最大滚动)。他们有什么意义?

此外,它一直被调用。我预计只有当其中一个钳位值为真时才会调用它。

1 个答案:

答案 0 :(得分:5)

我已经将ScrollView子类化为提供更有用的onOverScrolled。它看起来像这样:

public interface OnOverScrolledListener {
    void onOverScrolled(android.widget.ScrollView scrollView, 
                         int deltaX, int deltaY, boolean clampedX, boolean clampedY);
}

private OnOverScrolledListener mOnOverScrolledListener;

private int mOverScrollByDeltaX;
private int mOverScrollByDeltaY;    

@Override protected boolean overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX, int scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {
    this.mOverScrollByDeltaX = deltaX;
    this.mOverScrollByDeltaY = deltaY;
    final boolean result = super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY, maxOverScrollX, maxOverScrollY, isTouchEvent);
    return result;
};

@Override
protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
    super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
    if (mOnOverScrolledListener != null && (clampedX || clampedY)) {
        mOnOverScrolledListener.onOverScrolled(this, mOverScrollByDeltaX, mOverScrollByDeltaY, clampedX, clampedY);
    }
}

public OnOverScrolledListener getOnOverScrolledListener() {
    return mOnOverScrolledListener;
}

public void setOnOverScrolledListener(OnOverScrolledListener onOverScrolledListener) {
    this.mOnOverScrolledListener = onOverScrolledListener;
}