我希望在我的自定义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
,所以如果你有更好的关于如何实现这一目标的建议请分享!
答案 0 :(得分:0)
在默认的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的滚动效果,你必须这样做:
为第一个孩子和右孩子添加左边距给右孩子。比方说,例如,边距为mOverScrollSpace
您需要在自己的isOverScrolled
中编写方法LayoutManager
,以了解左侧滚动空间是否小于mOverScrollSpace
然后在onTouchEvent
中,当您遇到事件ACTION_MOVE
时,您会写下这样的内容
if(isOverScrolled) {
scrollByInternal(canScrollHorizontally ? -dx : 0,
canScrollVertically ? -dy : 0)
}
else {
scrollByInternal(canScrollHorizontally ? -dx/4 : 0,
canScrollVertically ? -dy/4 : 0)
}