为什么 SelectedIndex 在 Flex 4 中不能每隔一段时间工作一次?

发布于 2024-09-30 00:29:17 字数 1203 浏览 6 评论 0原文

在下面的工作示例中,只要文本框发生更改,列表的选定索引就会重置为 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 技术交流群。

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

发布评论

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

评论(4

扛起拖把扫天下 2024-10-07 00:29:17

首先你应该检查“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.

这样的小城市 2024-10-07 00:29:17

问题是,当您设置选定的索引时,您的列表尚未呈现。

更改您的 textinput1_changeHandler 方法将解决此问题:

protected function textinput1_changeHandler(event:TextOperationEvent):void
{
    items = new ArrayList(input.text.split(" "));
    callLater(function():void{list.selectedIndex = 0;});
}

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:

protected function textinput1_changeHandler(event:TextOperationEvent):void
{
    items = new ArrayList(input.text.split(" "));
    callLater(function():void{list.selectedIndex = 0;});
}
绅刃 2024-10-07 00:29:17

首先将数据提供者的刷新添加到您的函数中,以便它获取更改:

protected function textinput1_changeHandler(event:TextOperationEvent):void
{
     items = new ArrayList(input.text.split(" "));
     (list.dataProvider as ArrayCollection).refresh();
     list.selectedIndex = 0;
}

Add into your function a refresh of the data provider first so it picks up the changes:

protected function textinput1_changeHandler(event:TextOperationEvent):void
{
     items = new ArrayList(input.text.split(" "));
     (list.dataProvider as ArrayCollection).refresh();
     list.selectedIndex = 0;
}
孤凫 2024-10-07 00:29:17

来回的原因是该事件只是随着index的变化而创建,查看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();
}

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.

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