How to change ViewPager#39;s page?(如何更改 ViewPager 的页面?)
问题描述
我在我的应用程序中使用 ViewPager 并在主 Activity 中定义它.在 onCreate 方法中,我从 SharedPreferences 加载一些页面,然后将其传递给 PagerAdapter:
I'm using ViewPager in my app and define it in the main Activity. Inside onCreate
method I load some number of pages from SharedPreferences and then pass it to PagerAdapter:
@Override
public int getCount() {
return numberOfPages;
}
问题是,如果我将 Preferences(或另一个 Activity)中的这个数字更改为我之前查看过的其他页面索引,我的应用程序会崩溃,因为当我使用这个 ViewPager 返回到 Activity 时,这个索引超出了范围.只需更改活动 ViewPager 的页面即可解决此问题.有什么办法吗?
The problem is that if I would change this number in Preferences (or another Activity) to some other less then page index I viewed before, my app crashes because this index is out of bounds when I return to the activity with this ViewPager. It can be fixed simply by changing active ViewPager's page. Is there any way to do it?
推荐答案
我不确定我是否完全理解这个问题,但从你的问题的标题来看,我猜你要找的是 <代码>pager.setCurrentItem(num).这允许您以编程方式切换到 ViewPager
中的另一个页面.
I'm not sure that I fully understand the question, but from the title of your question, I'm guessing that what you're looking for is pager.setCurrentItem( num )
. That allows you to programatically switch to another page within the ViewPager
.
如果这不是问题,我需要查看来自 logcat 的堆栈跟踪以更具体.
I'd need to see a stack trace from logcat to be more specific if this is not the problem.
这篇关于如何更改 ViewPager 的页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何更改 ViewPager 的页面?


基础教程推荐
- LocationClient 与 LocationManager 2022-01-01
- “让"到底是怎么回事?关键字在 Swift 中的作用? 2022-01-01
- Android ViewPager:在 ViewPager 中更新屏幕外但缓存的片段 2022-01-01
- Android文本颜色不会改变颜色 2022-01-01
- 在 iOS 上默认是 char 签名还是 unsigned? 2022-01-01
- 固定小数的Android Money Input 2022-01-01
- :hover 状态不会在 iOS 上结束 2022-01-01
- 使用 Ryzen 处理器同时运行 WSL2 和 Android Studio 2022-01-01
- 如何使用 YouTube API V3? 2022-01-01
- 如何使 UINavigationBar 背景透明? 2022-01-01