Android的ViewPager屏幕旋转 [英] Android ViewPager Screen Rotation

查看:741
本文介绍了Android的ViewPager屏幕旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有ActionBarSherlock标签和ViewPager里面的活动。当页面滚动,选项卡切换和标签被改变,当前页面被更改过。 我使用的扩展FragmentStatePagerAdapter作为适配器的浏览量一类。 的问题是,当装置旋转时,从页适配器为getItem不叫,看起来像片段的引用不正确。 这是一个巨大的问题,因为用户必须履行寻呼机里面的一些领域。这些字段正确地恢复在转动,但因为我的片段的引用不正确的,我不能保存在正确的方式这些值。 关于旋转任何想法?

I have an Activity with ActionBarSherlock tabs and a ViewPager inside it. When the pages are scrolled, the tabs are switched and tabs are changed, the current page is changed too. I am using a class that extends FragmentStatePagerAdapter as adapter in the pageview. The problem is that when the device rotates, the getItem from the page adapter is not called and looks like the fragments references are not right. That's a huge problem, since the user must fulfill some fields inside the pager. These fields are recovered in correctly on the rotation, but since I the references for the fragments are not right, I can't save these values in right way. Any idea about the rotation?

推荐答案

保存当前页面数onSavedInstanceState:

Save the current page number in onSavedInstanceState:

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("item", mViewPager.getCurrentItem());
}

然后在的onCreate:

Then in onCreate:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if(savedInstanceState != null) {
         mViewPager.setCurrentItem(savedInstanceState.getInt("item"));
    }
}

这篇关于Android的ViewPager屏幕旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆