如何更改 ViewPager 的页面? [英] How to change ViewPager's page?

查看:12
本文介绍了如何更改 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 的页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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