pakerfeldt / android-viewflow

A horizontal view scroller library for Android
1.78k stars 694 forks source link

TitleFlowIndicator doesn't retain indicator position when screen orientation changes #30

Closed franciscojunior closed 13 years ago

franciscojunior commented 13 years ago

I noticed that when I have a screen rotation, TitleFlowIndicator doesn't keep the position when the screen changes.

For example, if I have a titleindicator with the configuration:

Title1 Title2(selected) Title3

and a screen orientation change occur, the new presentation is the following:

Title1 (selected) Title2 Title3

As soon as you start to fling, the state is fixed.

This is caused by the fact that when the widget is recreated because of the Activity recreation, currentScroll is set back to the default value of 0. As soon as a fling event occurs the value is corrected but before that the title is not synchronized with the view content.

The solution I got, and I don't know if it is a hack or not is to update the currentScroll as soon as the widget knows its width.

This is done in the method measureWidth.

I just added the line which updates the currentScroll value and it worked like a charm.

If you think there is a better place to update this value, please, let me know. I couldn't find any event which would be called after the measureWidth method would be called and so the getWidth() method could return another value different of 0.

private int measureWidth(int measureSpec) { int result = 0; int specMode = MeasureSpec.getMode(measureSpec); int specSize = MeasureSpec.getSize(measureSpec);

    if (specMode != MeasureSpec.EXACTLY) {
        throw new IllegalStateException(
                "ViewFlow can only be used in EXACTLY mode.");
    }
    result = specSize;

    // Restore previous position of title indicator in case of screen orientation change.
    currentScroll = currentPosition * result;

    return result;
}
pakerfeldt commented 13 years ago

I don't experience this at all (running HEAD version of the master repository). Can you please make sure you've pulled the latest changes and check if the issue persists?

franciscojunior commented 13 years ago

I'll make sure to use the latest code and return you back.

Thanks!