ksoichiro / Android-ObservableScrollView

Android library to observe scroll events on scrollable views.
http://ksoichiro.github.io/Android-ObservableScrollView/
Apache License 2.0
9.65k stars 2.06k forks source link

getCurrentScrollY returns invalid value after orientation change #238

Open jakubkrolewski opened 8 years ago

jakubkrolewski commented 8 years ago
  1. Create a layout with ObservableScrollView that fits screen in portrait (so it's not scrollable), but it's longer than the screen in landscape (so it's scrollable), e.g. modify the layout of the HandleTouchScrollViewActivity from the demo to fit your screen in portrait
  2. Launch an activity with this layout
  3. Rotate the device to the landscape mode
  4. Scroll the screen a little bit (let's assume that the currentScrollY is now 123)
  5. Rotate the device to the portrait mode
  6. Wait until the view state is restored
  7. Print the output of the getCurrentScrollY() method

Expected result: the view is not scrolled, so the output of the getCurrentScrollY() method should be 0 Actual result: getCurrentScrollY() method returns the previously stored value (so 123 as in the point 4)

jakubkrolewski commented 8 years ago

I've managed to fix the problem by adding the following code to the ObservableScrollView

`

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    trimScrollY();
}

private void trimScrollY() {
    View child = getChildAt(0);
    if (child != null) {
        int childHeight = child.getHeight();
        int verticalSpaceForChild = getHeight() - getPaddingTop() - getPaddingBottom();
        int maxScrollY = Math.max(0, childHeight - verticalSpaceForChild);

        mScrollY = Math.min(mScrollY, maxScrollY);
    }
}

`