Flex 4:mx | tree - 如何禁用项目选择?

时间:2011-02-09 17:41:17

标签: flex flex4 tree itemrenderer

我想创建一个树,它的子节点包含我创建的特定flex组件。我重写了默认的ItemRenderer来实现这一点。

在我的ItemRenderer中,我有:

  1. 两种状态:itemroot_item

  2. 在创建完成后执行的函数,使用数据验证此组件应处于的正确状态并将currentState更改为所需状态。

  3. 我的问题是,一旦用户点击任何元素,它就会自动更改为第一个搞乱的状态。

    如何禁用项目选择?当然,我希望用户能够在树上钻取,而不是选择项目。

    谢谢!

    更新

    项目会改变悬停效果的状态,所以或者我禁用项目选择或以某种方式阻止悬停效果改变状态。

    另一次更新

    这是我的代码:

    主TreeTest.xml:

    <?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" autoLayout="false" minHeight="600">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
        <fx:XML id="treeData">
            <node label="notifications">
            <node label="Winnings" is_root="yes">
                <node label="winner"/>
            </node>
            <node label="Challenges" is_root="yes">
            </node>
            <node label="Achievements" is_root="yes">
            </node>
            <node label="Lucky charms" is_root="yes">
            </node>
            <node label="Friend requests" is_root="yes">
            </node>
            </node>
        </fx:XML>  
    </fx:Declarations>
    
    <fx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import mx.controls.listClasses.ListBase;
            import mx.events.ListEvent;
            protected function tree_itemClickHandler(event:ListEvent):void
            {
                tree.selectedItem = null;
                event.preventDefault();
            }
    
            protected function tree_itemRollOverHandler(event:ListEvent):void {
                event.preventDefault();
            }
        ]]>
    </fx:Script>
    
    <mx:Tree id="tree" itemRollOver="tree_itemRollOverHandler(event)" itemClick="tree_itemClickHandler(event)" dataProvider="{treeData}"  folderOpenIcon="{null}" folderClosedIcon="{null}" defaultLeafIcon="{null}" width="1024" height="768" labelField="@label" itemRenderer="TreeItemRenderer" showRoot="false"  allowMultipleSelection="false" allowDragSelection="false"/>
    
    </s:Application>
    

    正如你在这里看到的,我试图阻止itemRollover和itemClick,但它没有解决我的问题。

    这是TreeItemRenderer.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <s:MXTreeItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" 
                      xmlns:s="library://ns.adobe.com/flex/spark" 
                      xmlns:mx="library://ns.adobe.com/flex/mx" creationComplete="init()">
    
    <s:states>
        <s:State name="root_item" />            
        <s:State name="item" />
    </s:states>
    <fx:Script>
        <![CDATA[
            import com.flexer.Debug;
    
            import mx.binding.utils.ChangeWatcher;
            import mx.controls.Alert;
            import mx.events.StateChangeEvent;
    
            private function _stateChangeEventHandler(e:StateChangeEvent):void {
                Alert.show("state changed to " + e.target.currentState);
            }
    
    
            private function init():void {
                var theXML:XMLList = XMLList(this.data);
                var theState:String =( theXML.attribute("is_root") == "yes"  ? "root_item" : "item");
                this.currentState=theState;
                this.addEventListener(StateChangeEvent.CURRENT_STATE_CHANGE,this._stateChangeEventHandler);
    //          Alert.show(theXML.attribute("is_root"));
    //          Alert.show(theXML.attribute("label") + (theXML.attribute("is_root") == "yes" ? "true" : "false"));
            }
    
        ]]>
    </fx:Script>
    <s:HGroup left="0" right="0" top="0" bottom="0" verticalAlign="middle" includeIn="root_item">
        <s:Rect id="indentationSpacer" width="{treeListData.indent}" percentHeight="100" alpha="0">
            <s:fill>
                <s:SolidColor color="0xFFFFFF" />
            </s:fill>
        </s:Rect>
        <s:Group id="disclosureGroup">
            <s:BitmapImage source="{treeListData.disclosureIcon}" visible="{treeListData.hasChildren}" />
        </s:Group>
        <s:BitmapImage source="{treeListData.icon}" />
        <s:Label id="labelField" text="{treeListData.label}" paddingTop="2"/>
    </s:HGroup>
    </s:MXTreeItemRenderer>
    

1 个答案:

答案 0 :(得分:2)

您可以为treeClick事件

添加事件处理程序
itemClick="tree_itemClickHandler(event)"

然后在事件处理程序中,您可以取消itemClick事件

protected function tree_itemClickHandler(event:ListEvent):void
{
    tree.selectedItem = null;
    event.preventDefault();
}

<强>更新

项呈示器有3种默认状态:正常,悬停和选中。您需要使用带有3个默认状态的basedOn来引用您的自定义状态。理想情况下,您需要数据中的状态,以便您可以避免在init()中执行的所有工作。它还允许您将basedOn绑定到数据的状态:

<s:states>
    <s:State name="normal" basedOn="{data.@state}"/>
    <s:State name="hovered" basedOn="{data.@state}"/>
    <s:State name="selected" basedOn="{data.@state}"/>
    <s:State name="root_item" />            
    <s:State name="item" />
</s:states>

然后,无论父列表发送的默认状态如何,显示都将基于数据中的内容。