android - 为什么 overScrollBy() 和 onOverScrolled() 不能与 RecyclerView 一起使用

标签 android android-recyclerview

我希望在我的自定义 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的滚动效果,你必须这样做:

  1. 给第一个 child 添加左边距,给右边的 child 添加右边距。比方说,边距是 mOverScrollSpace

  2. 你需要在自己的LayoutManager中写一个isOverScrolled方法来判断剩余滚动空间是否小于mOverScrollSpace

  3. 然后在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/

相关文章:

java - LayoutInflater 不更新 TextView

java - 如何将 "MM-dd-yyyy hh:mm"字符串日期格式转换为 GMT 格式?

android - 具有十进制值的 SeekBar

android - 如何防止在 Android 中的 RecyclerView 上向左或向右滑动

android - 回收站 View 中每个项目的视差效果?

android - 在 recyclerview 适配器 android 中选择适配器中的项目

java - 在 facebook 墙上发帖总是显示登录页面..问题 android

javascript - 大约 2 分钟后,Webaudio 声音在 Chrome for Android 上停止

android - 在 recyclerview Gridlayoutmanager 中创建总线布局时出现间距问题

java - RecyclerView 在关闭并返回 Activity 后显示数据