From 3abb0504676e9837f278312ede516e9fcaf992b8 Mon Sep 17 00:00:00 2001 From: Jared Rummler Date: Tue, 14 Jun 2016 05:45:02 -0700 Subject: [PATCH] fix scrollY when the GridView has vertical spacing --- .../observablescrollview/ObservableGridView.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/library/src/main/java/com/github/ksoichiro/android/observablescrollview/ObservableGridView.java b/library/src/main/java/com/github/ksoichiro/android/observablescrollview/ObservableGridView.java index 8a90016d..2a2d0fd5 100644 --- a/library/src/main/java/com/github/ksoichiro/android/observablescrollview/ObservableGridView.java +++ b/library/src/main/java/com/github/ksoichiro/android/observablescrollview/ObservableGridView.java @@ -386,6 +386,14 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { } } + @Override public int getVerticalSpacing() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { + return super.getVerticalSpacing(); + } + // getVerticalSpacing was added in 16. We could use reflection to get the value on pre-JB, but that would be expensive. + return 0; + } + private void init() { mChildrenHeights = new SparseIntArray(); mHeaderViewInfos = new ArrayList<>(); @@ -435,7 +443,7 @@ private void onScrollChanged() { } } } - mPrevScrolledChildrenHeight += mPrevFirstVisibleChildHeight + skippedChildrenHeight; + mPrevScrolledChildrenHeight += mPrevFirstVisibleChildHeight + skippedChildrenHeight + getVerticalSpacing(); mPrevFirstVisibleChildHeight = firstVisibleChild.getHeight(); } else if (firstVisiblePosition < mPrevFirstVisiblePosition) { // scroll up @@ -447,7 +455,7 @@ private void onScrollChanged() { } } } - mPrevScrolledChildrenHeight -= firstVisibleChild.getHeight() + skippedChildrenHeight; + mPrevScrolledChildrenHeight -= firstVisibleChild.getHeight() + skippedChildrenHeight + getVerticalSpacing(); mPrevFirstVisibleChildHeight = firstVisibleChild.getHeight(); } else if (firstVisiblePosition == 0) { mPrevFirstVisibleChildHeight = firstVisibleChild.getHeight();