问题描述:

I wanna add overscrolling to a ListView, so you can scroll a bit further than the first or last element and when you release, it snaps back. There are quite some tutorials and questions out there. All of them say to create a custom ListView class that overrides overScrollBy() and pass my custom max Y overscroll distance to the parent class.

@Override

protected boolean overScrollBy(final int deltaX, final int deltaY, final int scrollX, final int scrollY,

final int scrollRangeX, final int scrollRangeY, final int maxOverScrollX, final int maxOverScrollY,

final 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, mMyMaxYOverscrollDistance, isTouchEvent);

}

Thing is that this only snaps back if you overscroll really carefully. Often it gets stuck in an overscroll position. Any ideas on how to make it snap back in any case?

Thanks

网友答案:

I could solve it by decreasing the max overscroll distance like this:

@Override
protected boolean overScrollBy(final int deltaX, final int deltaY, final int scrollX, final int scrollY,
        final int scrollRangeX, final int scrollRangeY, final int maxOverScrollX, final int maxOverScrollY,
        final boolean isTouchEvent) {

    final int maxYOverscroll = isTouchEvent ? mMaxYOverscrollDistance : (int) (Math.abs(scrollY) * 0.9f);
    return super.overScrollBy(deltaX, deltaY / 2, scrollX, scrollY, scrollRangeX, scrollRangeY, maxOverScrollX,
            maxYOverscroll, isTouchEvent);
}
相关阅读:
Top