Why overScrollBy () and onOverScrolled () do not work with RecyclerView

I was hoping to use these methods inside my custom RecyclerView , but for some reason it doesn't work, and I'm not sure why ...

 @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); } 

What I really hope to do is add an empty space at the top of the list so that I can scroll the previous item with the given mOverscrollDistance distance, so if you have a better suggestion on how to do this, please share!

+5
source share
1 answer

in the standard android version, the android will only display luminous signals when reprogramming:

 @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); } } 

if you want to have a scroll effect like iphone, you have to do this:

  • add the left margin to the first child and the right child to the right child. let's say for example the mOverScrollSpace field

  • you need to write the isOverScrolled method in your own LayoutManager to find out if there is less scroll space left than mOverScrollSpace

  • Then in onTouchEvent , when you encounter the ACTION_MOVE event, you write something like this

     if(isOverScrolled) { scrollByInternal(canScrollHorizontally ? -dx : 0, canScrollVertically ? -dy : 0) } else { scrollByInternal(canScrollHorizontally ? -dx/4 : 0, canScrollVertically ? -dy/4 : 0) } 
0
source

Source: https://habr.com/ru/post/1200110/


All Articles