1

如何检测SpinnerList控件中间的项目的点击?

即典型情况 - 当用户滚动 SpinnerList 时,直到所需项目位于中间,然后单击它?

当我只是添加一个click事件处理程序时,我也会错误地检测到这种情况,当用户只是单击某个项目以使其显示在中间时,如下截图所示:

在此处输入图像描述

下面是我的测试用例代码。

请问有什么建议吗?我也尝试使用自定义SpinnerListItemRenderer并在那里添加点击处理程序,但它并没有改变上述问题。

主要.mxml:

<?xml version="1.0" encoding="utf-8"?>
<s:ViewNavigatorApplication 
    xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" 
    firstView="views.MainHomeView">
</s:ViewNavigatorApplication>

意见/MainHomeView.mxml:

<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark" 
        title="How to detect a click?">

    <fx:Script>
        <![CDATA[
            import mx.collections.ArrayList;

            private function handleClick(event:Event):void {
                trace('selectedItem: ' + _list.selectedItem);
            }
        ]]>
    </fx:Script>

    <s:SpinnerListContainer>
        <s:SpinnerList id="_list" 
                       click="handleClick(event)"
                       typicalItem="45" 
                       dataProvider="{new ArrayList([1,5,6,10,15,30])}"
                       wrapElements="false"/>
    </s:SpinnerListContainer>
</s:View> 
4

1 回答 1

1

自己解决了这个问题itemRenderer-

红黑.as:

package {
    import flash.events.MouseEvent;
    import spark.components.List;
    import spark.components.SpinnerList;
    import spark.components.SpinnerListItemRenderer;

    public class RedBlack extends SpinnerListItemRenderer {
        public function RedBlack() {
            super();
            cacheAsBitmap = true;
            minHeight = 50;
            setStyle('textAlign', 'center');
            addEventListener(MouseEvent.CLICK, handleClick, false, 0, true);
        }

        override public function set data(value:Object):void {
            super.data = value;
            setStyle('color', int(value) % 2 ? 'red' : 'black');
        }

        private function handleClick(e:MouseEvent):void {
            var list:SpinnerList = owner as SpinnerList;
            var value:int = int(data);

            if (list && value == list.selectedItem) {
                trace('clicked in the middle: ' + value);
            }
        }   
    }
}
于 2012-12-04T07:53:04.640 回答