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

查看:104
本文介绍了如何改变ViewPager的页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中使用ViewPager和主要活动定义它。在的onCreate 方法我加载从共享preferences页一些号码,然后把它传递给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这个数字(或其他活动),以其他一些不太那么网页的索引我看之前,我的应用程序崩溃,因为该指数是出界,当我回到活动与此ViewPager。它可以简单地通过改变活性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天全站免登陆