0

我知道有一个示例如何在 ScrollView 上实现反弹效果,但在这里我整天都在为一个问题苦苦挣扎。我在 Parallax ScrollView 和反弹效果之间创建了一些东西。Parallax ScrollView 取自这里:Parallax Android ScrollView并实现反弹效果ObservableScrollView,现在看起来像这样:

public class ObservableScrollView extends ScrollView {
private ScrollCallbacks mCallbacks;

private static final int MAX_Y_OVERSCROLL_DISTANCE = 150;

private Context mContext;
private int mMaxYOverscrollDistance;

public ObservableScrollView(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.mContext = context;
    initBounceScrollView();
}

@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
    super.onScrollChanged(l, t, oldl, oldt);
    if (mCallbacks != null) {
        mCallbacks.onScrollChanged(l, t, oldl, oldt);
    }
}

@Override
public int computeVerticalScrollRange() {
    return super.computeVerticalScrollRange();
}

public void setCallbacks(ScrollCallbacks listener) {
    mCallbacks = listener;
}

private void initBounceScrollView() {
    // get the density of the screen and do some maths with it on the max
    // overscroll distance
    // variable so that you get similar behaviors no matter what the screen
    // size

    final DisplayMetrics metrics = mContext.getResources()
            .getDisplayMetrics();
    final float density = metrics.density;

    mMaxYOverscrollDistance = (int) (density * MAX_Y_OVERSCROLL_DISTANCE);

}

@Override
public void draw(Canvas canvas) {
    super.draw(canvas);
}

static interface ScrollCallbacks {
    public void onScrollChanged(int l, int t, int oldl, int oldt);
}

@SuppressLint("NewApi")
@Override
protected boolean overScrollBy(int deltaX, int deltaY, int scrollX,
        int scrollY, int scrollRangeX, int scrollRangeY,
        int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {
    // This is where the magic happens, we have replaced the incoming
    // maxOverScrollY with our own custom variable mMaxYOverscrollDistance;
    return super.overScrollBy(deltaX, deltaY, scrollX, scrollY,
            scrollRangeX, scrollRangeY, maxOverScrollX,
            mMaxYOverscrollDistance, isTouchEvent);
}

现在我想要实现的是视差滚动视图上的反弹效果,但我希望反弹效果只有在我拉下滚动视图时才会出现。例如,当我在 ScrollView 的开头并将其拉下时。我想在结束时禁用反弹效果ScrollView

任何建议/想法我该如何实现这一点..这是创建这样的自定义的最佳方式View吗?

4

1 回答 1

1

onOverscrolledapi 9 中引入了一种方法。您可以像这样达到预期的效果:

 @Override
    protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
        if (scrollY < 0)
            super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
    }

因此,只有当您向下拉滚动视图时才会发生过度滚动,并且 scrollY 变为负数。切换它(scrollY > 0)以达到相反的效果。

于 2013-10-24T08:16:59.687 回答