Android Spinner 视图:setSelection(intposition) 和 setSelection(intposition, boolean animate) 之间的区别?
我阅读了文档,但不太明白...参数“animate”是做什么用的?
我唯一注意到的一件事:
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
if (savedPosition != -1)
{
((Spinner) parent).setSelection(savedPosition );
savedPosition = -1;
return;
}
//...
}
...与 setSelection
配合良好,但与 setSelection(savedPosition, true);
配合使用会陷入无限循环/StackOverflowError;
我真的很好奇为什么会这样是这样吗?
I read the documentation, but I don't quite understand it... What is the parameter "animate" for?
Only thing I noticed:
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
if (savedPosition != -1)
{
((Spinner) parent).setSelection(savedPosition );
savedPosition = -1;
return;
}
//...
}
...works fine with setSelection
, but runs into endless loop / StackOverflowError with setSelection(savedPosition, true);
I'm really curious why this would be so?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯,它应该按照名字所暗示的那样做。从当前位置到所需位置的动画。当您仅使用 setSelection 时,当您试图确保用户看到更改(某种更改通知)时,更改更合适,而且看起来更流畅。至少这就是我使用它的原因。至于动画我从来没有遇到过任何问题。
为什么你使用savedPosition而不是位置来进行选择?也许这就是导致循环的原因。我的建议是尝试用位置来做,看看是否会失败。你的代码有点奇怪..
是的,正如评论建议你出于某种原因再次调用该方法,因为我相信动画 setSelection 在实际设置选择时会执行此操作。
Well it should be doing what the name suggests. animating from the current position to the desired position. when you use just setSelection the change is more apropriate when you are trying to make sure the user sees the change (sort of a notification of a change) plus it looks smoother. at least that's why i use it. as for the animating i never had any issues with it.
why are you using the savedPosition instead of position for the selection? maybe that's what is causing the loop. my suggestion is to try to do it with position and see if it fails. your code is somewhat odd..
ye as the comment suggested you are calling the method again for some reason because i believe the animated setSelection does that when the selection is actually set.