Android RealViewSwitcher shows view partially

百般思念 提交于 2019-12-12 00:23:29

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/12315388/android-realviewswitcher-shows-view-partially

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!