onPageSelected调用setCurrentItem时,不触发(0) [英] onPageSelected isn't triggered when calling setCurrentItem(0)
问题描述
我有一个ViewPager它显示了一堆照片的活动。当它启动ViewPager的位置是基于在一个previous活动什么用户选择设定。类似的画廊。
I have an Activity with a ViewPager which displays a bunch of pictures. When it starts the ViewPager's position is set based on what the user selected in a previous Activity. Similar to a gallery.
我希望onPageSelected被称为一个新的页面每次选择,即活动第一次打开时或当用户滑动到一个新的页面。
I want the onPageSelected to be called every time a new page is selected, i.e. when the Activity is first opened or when the user slides to a new page.
我设置的出发点,像这样:
I set the starting point like so:
mPager.setCurrentItem(index);
一切正常,只是setCurrentItem调用索引设置为0,因为这将不会触发onPageSelected工作。
Everything works, except when setCurrentItem called with index set to 0 as this will not trigger onPageSelected.
mPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int index) {
Log.d(TAG, "onPageSelected " + index);
}
...
}
所以我的问题是;这是一个错误,如果是的话我该怎么办呢?
So my question is; is this a bug, and if so what can I do about it?
推荐答案
好了,我一直没能弄清楚这是否是一个错误(或要素)。不过,我想我会分享怎么可能解决这个问题可以看..
Ok, so I haven't been able to figure out whether this is a bug (or a feature). But I thought I'd share how a possible solution to the problem could look..
写你想在活动的方法来执行,然后在功能,称这种现象为onPageSelected方法。
Write the functionality you wish to perform in a method in the Activity and then call this in the onPageSelected method.
mPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int index) {
myOnPageSelectedLogic(index);
}
...
}
,然后调用之后
And then right after calling
setCurrentItem(index);
在活动中,添加下面的if语句
in the Activity, add the following if statement
if(index == 0) {
myOnPageSelectedLogic(0);
}
这不是超pretty的,但我希望它可以帮助别人:)
It's not super pretty but I hope it helps someone :)
这篇关于onPageSelected调用setCurrentItem时,不触发(0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!