5

我希望在我的 custom 中使用这些方法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,所以如果您对如何实现这一点有更好的建议,请分享!

4

1 回答 1

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 的滚动效果,你必须这样做:

  1. 将左边距添加到第一个孩子,右边孩子添加到右边孩子。比方说,例如,边距是mOverScrollSpace

  2. 你需要自己写一个方法isOverScrolledLayoutManager知道左边剩余滚动空间是否小于 mOverScrollSpace

  3. 然后onTouchEvent,当你遇到事件时ACTION_MOVE,你会写这样的东西

    if(isOverScrolled) {
        scrollByInternal(canScrollHorizontally ? -dx : 0,
            canScrollVertically ? -dy : 0)
    }
    else {
        scrollByInternal(canScrollHorizontally ? -dx/4 : 0,
            canScrollVertically ? -dy/4 : 0)
    }
    
于 2014-11-15T03:20:10.020 回答