Android Spinner 视图:setSelection(intposition) 和 setSelection(intposition, boolean animate) 之间的区别?

发布于 2024-11-08 01:14:38 字数 521 浏览 4 评论 0原文

我阅读了文档,但不太明白...参数“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

掩耳倾听 2024-11-15 01:14:38

嗯,它应该按照名字所暗示的那样做。从当前位置到所需位置的动画。当您仅使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文