[英]Android: ViewPager always show the second page at the start
I created a small Android applications, which use Viewpager for reading a story. 我创建了一个小型Android应用程序,该应用程序使用Viewpager来阅读故事。 Here is my code:
这是我的代码:
Override the getItem() method of FragmentStatePagerAdapter: 重写FragmentStatePagerAdapter的getItem()方法:
@Override
public Fragment getItem(int position) {
TextStoryChapter chapter = (TextStoryChapter) getIntent().getSerializableExtra("chapter");
return ScreenSlidePageFragment.create(position,chapter);
}
The create() method of ScreenSlidePageFragment
class: ScreenSlidePageFragment
类的create()方法:
public static final String ARG_PAGE = "page";
private int mPageNumber;
private static String chapterName;
private static String content;
public static ScreenSlidePageFragment create(int pageNumber, TextStoryChapter chapter) {
ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();
Bundle args = new Bundle();
args.putInt(ARG_PAGE, pageNumber);
TextStoryChapterDetail detail = chapter.pages.get(pageNumber);
chapterName = "Page " + Integer.toString(detail.pageNo);
content = detail.content;
fragment.setArguments(args);
return fragment;
}
And the onCreate() method of ScreenSlidePageFragment
class: 还有
ScreenSlidePageFragment
类的onCreate()方法:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPageNumber = getArguments().getInt(ARG_PAGE);
Log.e("PageNumber", Integer.toString(mPageNumber));
}
But when I run the applications, the ViewPager always display the content of page 2 (I created a TextStoryChapter
with 5 content pages) at the start and the same content at the next page. 但是,当我运行应用程序时,ViewPager总是在开始时显示页面2的内容(我创建了具有5个内容页面的
TextStoryChapter
),而在下一页显示相同的内容。 The content of page 1 just disappear. 第1页的内容刚刚消失。 When I navigated to page 3 and slided back to page 1, everything became normal again, no more duplicate content of page 2, and I don't know why and how it happen.
当我导航到第3页并滑回第1页时,一切又恢复了正常,第2页的内容不再重复,而且我也不知道为什么以及如何发生。 Anyone can help me?
有人可以帮助我吗? Thanks a lot!
非常感谢!
Take a lock at your code. 锁定您的代码。 When ViewPager created, 2 variables
chapterName
and chapterContent
data will be re-assign and your page 1 data will be lost. 创建
chapterName
,将重新分配2个变量chapterName
和chapterContent
数据,并且第1页数据将丢失。 You should post your data into a Bundle
, this will help. 您应该将数据发布到
Bundle
,这将有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.