为什么 SelectedIndex 在 Flex 4 中不能每隔一段时间工作一次?
在下面的工作示例中,只要文本框发生更改,列表的选定索引就会重置为 0。
然而,由于某种奇怪的原因,每隔一次击键,所选项目就会消失,然后在随后的击键时重新出现。
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
<fx:Script>
<![CDATA[
import mx.collections.ArrayList;
import mx.core.UIComponent;
import mx.events.FlexEvent;
import spark.effects.Scale;
import spark.events.TextOperationEvent;
[Bindable]
public var items : ArrayList;
protected function textinput1_changeHandler(event:TextOperationEvent):void
{
items = new ArrayList(input.text.split(" "));
list.selectedIndex = 0;
}
]]>
</fx:Script>
<s:TextInput x="165" y="124" change="textinput1_changeHandler(event)" id="input" text="a few words"/>
<s:List x="165" y="184" width="433" height="291" dataProvider="{items}" id="list"></s:List>
</s:Application>
In the following working example the list's selected index is supposed to reset to 0 whenever the text box changes.
However, for some odd reason every other keystroke the selected item disappears and then reappears at the subsequent keystroke.
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
<fx:Script>
<![CDATA[
import mx.collections.ArrayList;
import mx.core.UIComponent;
import mx.events.FlexEvent;
import spark.effects.Scale;
import spark.events.TextOperationEvent;
[Bindable]
public var items : ArrayList;
protected function textinput1_changeHandler(event:TextOperationEvent):void
{
items = new ArrayList(input.text.split(" "));
list.selectedIndex = 0;
}
]]>
</fx:Script>
<s:TextInput x="165" y="124" change="textinput1_changeHandler(event)" id="input" text="a few words"/>
<s:List x="165" y="184" width="433" height="291" dataProvider="{items}" id="list"></s:List>
</s:Application>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先你应该检查“String.split”函数。它有几个错误,我不记得了。按“”或“blah”(末尾有空格)的顺序尝试。
您还应该等到列表实际更新。更改可绑定属性只会触发一些事件,而不会实际更改列表(AFAIK)。只需谷歌列表的事件。您也可以尝试覆盖列表的“dataProvider”设置器。
First you should check "String.split" function. It has several bugs, I don't remember them. Try it on sequence like " " or "blah " (space at the end).
Also you should wait until the List is actually updated. Changing the bindable property only fires some event, not actually changing the list (AFAIK). Just google List's events. Also you may try to override List's "dataProvider" setter.
问题是,当您设置选定的索引时,您的列表尚未呈现。
更改您的
textinput1_changeHandler
方法将解决此问题:The problem is, that your list is not rendered yet, when you set the selected index.
Changing your
textinput1_changeHandler
method will solve this issue:首先将数据提供者的刷新添加到您的函数中,以便它获取更改:
Add into your function a refresh of the data provider first so it picks up the changes:
来回的原因是该事件只是随着index的变化而创建,查看listbase的setselectedindex;
将 selectedindex 更改为 0 之前的修复方法是先将其更改为 -1,然后更改为 0。
The reason for the back and forth is that the event is only create with the index changes, check out listbase the setselectedindex;
the fix before you change selectedindex to 0 is to change it first to -1 and then to 0.