我希望在我的自定义 RecyclerView
中使用这些方法,但由于某些原因它不起作用,我不确定为什么...
@Override
protected boolean overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX, int scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {
return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY, mOverscrollDistance, mOverscrollDistance, isTouchEvent);
}
@Override
protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
}
我实际上希望做的是在列表的开头添加空白空间,这样我就可以滚动经过第一个项目并设置距离 mOverscrollDistance
,所以如果您有关于如何实现这一目标的更好建议,请分享!
最佳答案
在默认的 android 实现中,android 只会在过度滚动时绘制发光:
@Override
public void draw(Canvas c) {
super.draw(c);
final int count = mItemDecorations.size();
for (int i = 0; i < count; i++) {
mItemDecorations.get(i).onDrawOver(c, this);
}
boolean needsInvalidate = false;
if (mLeftGlow != null && !mLeftGlow.isFinished()) {
final int restore = c.save();
c.rotate(270);
c.translate(-getHeight() + getPaddingTop(), 0);
needsInvalidate = mLeftGlow != null && mLeftGlow.draw(c);
c.restoreToCount(restore);
}
if (mTopGlow != null && !mTopGlow.isFinished()) {
c.translate(getPaddingLeft(), getPaddingTop());
needsInvalidate |= mTopGlow != null && mTopGlow.draw(c);
}
if (mRightGlow != null && !mRightGlow.isFinished()) {
final int restore = c.save();
final int width = getWidth();
c.rotate(90);
c.translate(-getPaddingTop(), -width);
needsInvalidate |= mRightGlow != null && mRightGlow.draw(c);
c.restoreToCount(restore);
}
if (mBottomGlow != null && !mBottomGlow.isFinished()) {
final int restore = c.save();
c.rotate(180);
c.translate(-getWidth() + getPaddingLeft(), -getHeight()
+ getPaddingTop());
needsInvalidate |= mBottomGlow != null && mBottomGlow.draw(c);
c.restoreToCount(restore);
}
if (needsInvalidate) {
ViewCompat.postInvalidateOnAnimation(this);
}
}
如果你想有类似iphone的滚动效果,你必须这样做:
给第一个 child 添加左边距,给右边的 child 添加右边距。比方说,边距是
mOverScrollSpace
你需要在自己的
LayoutManager
中写一个isOverScrolled
方法来判断剩余滚动空间是否小于mOverScrollSpace
然后在
onTouchEvent
中,遇到事件ACTION_MOVE
,就这样写if(isOverScrolled) { scrollByInternal(canScrollHorizontally ? -dx : 0, canScrollVertically ? -dy : 0) } else { scrollByInternal(canScrollHorizontally ? -dx/4 : 0, canScrollVertically ? -dy/4 : 0) }
关于android - 为什么 overScrollBy() 和 onOverScrolled() 不能与 RecyclerView 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25261806/