我知道有一个例子如何在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时才会出现反弹效果。例如,当我在ScrollView的开头并将其拉下来时。我想在ScrollView
结束时禁用跳出效果。
任何建议/想法我如何才能实现这一点......这是创建这样的自定义View
的最佳方式吗?
答案 0 :(得分:1)
在api 9中引入了onOverscrolled
方法。您可以达到如下所需的效果:
@Override
protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
if (scrollY < 0)
super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
}
因此只有当您向下拉动滚动视图时才会发生过度滚动,并且滚动变为负数。
将其切换为(scrollY > 0)
以达到相反的效果。