在下面的工作示例中,只要文本框发生更改,列表的选定索引就会重置为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>
答案 0 :(得分:0)
首先你应该检查“String.split”功能。它有几个bug,我不记得了。按照“”或“blah”(最后的空格)等顺序尝试。
此外,您应该等到列表实际更新。更改可绑定属性仅触发某些事件,而不是实际更改列表(AFAIK)。只是google List的活动。您也可以尝试覆盖List的“dataProvider”设置器。
答案 1 :(得分:0)
问题是,当您设置所选索引时,您的列表尚未呈现。
更改textinput1_changeHandler
方法将解决此问题:
protected function textinput1_changeHandler(event:TextOperationEvent):void
{
items = new ArrayList(input.text.split(" "));
callLater(function():void{list.selectedIndex = 0;});
}
答案 2 :(得分:0)
首先在您的函数中添加数据提供程序的刷新,以便它获取更改:
protected function textinput1_changeHandler(event:TextOperationEvent):void
{
items = new ArrayList(input.text.split(" "));
(list.dataProvider as ArrayCollection).refresh();
list.selectedIndex = 0;
}
答案 3 :(得分:0)
来回的原因是事件只是在索引更改时创建,请查看listbase中的setselectedindex;
将selectedindex更改为0之前的修复是先将其更改为-1,然后再更改为0.
/**
* @private
* Used internally to specify whether the selectedIndex changed programmatically or due to
* user interaction.
*
* @param dispatchChangeEvent if true, the component will dispatch a "change" event if the
* value has changed. Otherwise, it will dispatch a "valueCommit" event.
*/
mx_internal function setSelectedIndex(value:int, dispatchChangeEvent:Boolean = false):void
{
if (value == selectedIndex)
return;
if (dispatchChangeEvent)
dispatchChangeAfterSelection = dispatchChangeEvent;
_proposedSelectedIndex = value;
invalidateProperties();
}