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 的页面?
基础教程推荐
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
