Question

I've got the following problem. I'm using the RealViewSwitcher class (which extends ViewGroup and can be founded here ). I've got three views and swiping between them works well in both orientatons. However, when I change orientation dynamically (for example, I'm on the screen 1 in vertical view and change the orientation to horizontal) - the view is showing only partially (one third of the screen is first view, other two third - second view).

The code:

// my activity method
@Override
public void onConfigurationChanged(Configuration newConfig)
{
// ...
realViewSwitcher.setCurrentScreen(1);
// ...
}

// realViewSwitcher method
public void setCurrentScreen(int currentScreen) {
    mCurrentScreen = Math.max(0, Math.min(currentScreen, getChildCount() - 1));
    scrollTo(mCurrentScreen * getWidth(), 0);
    invalidate();
}

UPD(10.09.2012): Added some code

Was it helpful?

Solution

Add the following snippet to your RealViewSwitcher Class,

public void orientationSnapToScreen(Context ctx)
        {
            Display display = ((WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); 
            int displayWidth = display.getWidth();

            mNextScreen = Math.max(0, Math.min(getCurrentScreen(), getChildCount() - 1));
            final int newX = mNextScreen * displayWidth;
            final int delta = newX - getScrollX();

            mScroller.startScroll(getScrollX(), 0, delta, 0, 0);
        }

and add the following method to your main activity from where you are calling the RealViewSwitcher class

@Override
    public void onConfigurationChanged(Configuration newConfig) 
    {
        super.onConfigurationChanged(newConfig);
        realViewSwitcher.orientationSnapToScreen(this);
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top