问题描述
香港专业教育学院得到了这个奇怪的问题,ViewPager的setCurrentItem(位置,假)工作完全正常,然后即时切换到另一个活动,和IM回第一个活动之后,ViewPager总是在第一项结束。虽然香港专业教育学院添加setCurrentItem到onResume方法,它仍然忽略它。它甚至不是扔的时候我尝试设置项出界指数的任何异常。虽然后来当我把这种方法时,下一步按钮被点击时,它就像预期。经过我的code的10倍任何可能调用setCurrentItem(0)或不服,但它只是不存在的。
Ive got this strange issue, ViewPager's setCurrentItem(position, false) works perfectly fine, then im switching to another activity, and after im back to the first activity, the ViewPager always ends up on the first item. Even though ive added setCurrentItem to onResume method it still ignores it.Its not even throwing any exception when im trying to set item to out of bounds index.Though later on when i call this method, when the button "next" is clicked, it works like expected.Checked my code 10 times for any possible calls to setCurrentItem(0) or smth but its just not there at all.
推荐答案
我真的不能回答到底为什么发生这种情况,但如果延迟setCurrentItem呼吁几毫秒就应该工作。我的猜测是因为在 onResume
还没有呈现过呢,和ViewPager需要一个或类似的东西。
i can't really answer WHY exactly this happens, but if you delay the setCurrentItem call for a few milliseconds it should work. My guess is that because during onResume
there hasn't been a rendering pass yet, and the ViewPager needs one or something like that.
private ViewPager viewPager;
@Override
public void onResume() {
final int pos = 3;
viewPager.postDelayed(new Runnable() {
@Override
public void run() {
viewPager.setCurrentItem(pos);
}
}, 100);
}
更新:光阴的故事
所以,今天我有这样的viewpager忽略了我setCurrentItem行动的问题,我搜索计算器的解决方案。我发现有人用同样的问题,修复;我实现了修复,并没有奏效。哇!回到计算器,以downvote的人造修复提供者,和...
so today i had the problem that the viewpager ignored my setCurrentItem action, and i searched stackoverflow for a solution. i found someone with the same problem and a fix; i implemented the fix and it didn't work. whoa! back to stackoverflow to downvote that faux-fix-provider, and ...
这是我的。我实现了自己的错误的不固定,我想出了我第一次绊倒的问题(这是后来忘了)。现在我将不得不downvote自己提供不良信息。
it was me. i implemented my own faulty non-fix, which i came up with the first time i stumbled over the problem (and which was later forgotten). i'll now have to downvote myself for providing bad information.
我最初的修复工作的原因是不是因为渲染通道的;的问题是,该寻呼机的含量通过一个旋转器来控制。无论是纺纱和寻呼机状态恢复了onResume,正因为如此的纺织onItemSelected监听器在未来的事件传播周期,这并重新填充viewpager叫 - 使用不同的默认值,此时
删除和初始状态恢复时重新听众固定的问题。
the reason my initial "fix" worked was not because of of a "rendering pass"; the problem was that the pager's content was controlled by a spinner. both the spinners and the pagers state were restored onResume, and because of this the spinners onItemSelected listener was called during the next event propagation cycle, which did repopulate the viewpager - this time using a different default value.
removing and resetting the listener during the initial state restoration fixed the issue.
上面的修复工作的第一次,因为它设置之后,的的onItemSelected事件触发的寻呼机当前位置的。后来,它停止工作由于某种原因(可能是应用程序变得太慢了 - 在我实现我没有用100毫秒,10毫秒不过)。然后,我删除了postDelayed在清理周期,因为它并没有改变已有的错误行为。
the fix above kind-of worked the first time, because it set the pagers current position after the onItemSelected event fired. later, it ceased to work for some reason (probably the app became too slow - in my implementation i didn't use 100ms, but 10ms). i then removed the postDelayed in a cleanup cycle, because it didn't change the already faulty behaviour.
更新2:我不能downvote我自己的职位。我以为,尊贵切腹自杀是唯一的选择。
update 2: i can't downvote my own post. i assume, honorable seppuku is the only option left.
这篇关于Android的ViewPager setCurrentItem onResume后不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!