Android-ObservableScrollView
Android-ObservableScrollView copied to clipboard
getCurrentScrollY returns invalid value after orientation change
- 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
- Launch an activity with this layout
- Rotate the device to the landscape mode
- Scroll the screen a little bit (let's assume that the currentScrollY is now 123)
- Rotate the device to the portrait mode
- Wait until the view state is restored
- 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)
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);
}
}
`