Android-ObservableScrollView icon indicating copy to clipboard operation
Android-ObservableScrollView copied to clipboard

getCurrentScrollY returns invalid value after orientation change

Open jakubkrolewski opened this issue 8 years ago • 1 comments

  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 avatar Mar 02 '16 15:03 jakubkrolewski

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

`

jakubkrolewski avatar Mar 02 '16 15:03 jakubkrolewski