apache-flex 动态更改树节点高度的Flex

yjghlzjz  于 2022-11-01  发布在  Apache
关注(0)|答案(1)|浏览(175)

我想有一个树节点渲染器,其作用如下:

  • 单击它可更改节点高度。

我试了如下方法:创建一个从UIComponent派生的自定义类,然后将其插入树节点渲染器中,但由于某些原因,它没有按我预期的方式工作。
下面是代码:

package
{
    import flash.display.Sprite;
    import flash.events.MouseEvent;

    import mx.controls.Tree;
    import mx.core.UIComponent;

    public class Expander extends UIComponent
    {
        public var expanded : Boolean = false;

        public function Expander()
        {
            super();
            this.redraw();
            this.addEventListener( MouseEvent.CLICK, onClick );
        }

        private function redraw( newWidth : Number = 60, newHeight : Number = 20 ) : void
        {
            this.graphics.clear();
            this.graphics.beginFill( 0x0000FF, 0.7 );
            this.graphics.drawRect( 0, 0, newWidth, newHeight );
            this.graphics.endFill();
        }

        private function onClick( e : MouseEvent ) : void
        {
            e.stopImmediatePropagation();
            colapseExpand();
        }

        private function colapseExpand() : void
        {
            expanded = ! expanded;
            if( expanded )
                redraw( this.width, 100 );
            else
                redraw();

            ( parent as Tree ).invalidateSize();

        }
    }
}

下面是渲染器类代码:

<?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"
                      xmlns:lx="*">

    <s:states>
        <s:State name="normal" />            
        <s:State name="hovered" />
        <s:State name="selected" />
    </s:states>
    <s:HGroup left="0" right="0" top="0" bottom="0" verticalAlign="middle">
        <s:Rect id="indentationSpacer" width="{treeListData.indent}" percentHeight="100" alpha="0">
            <s:fill>
                <s:SolidColor color="0xFFAFFF" />
            </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"/>
        <lx:Expander width="100%">

        </lx:Expander>
    </s:HGroup>
</s:MXTreeItemRenderer>

自定义类被标记为lx:Expander width="100%"

jv2fixgn

jv2fixgn1#

您要做的是使用variableRowHeight="true"创建一个树,然后在项目渲染器中使用状态来“展开"项目渲染器:

<?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"
                      xmlns:lx="*">

    <s:states>
        <s:State name="normal" />            
        <s:State name="hovered" />
        <s:State name="selected" />
    </s:states>
    <s:HGroup height.selected="75">
        <s:Label text="Not expanded" text.selected="EXPANDED!" />
    </s:HGroup>
</s:MXTreeItemRenderer>

当选择项呈示器时,该选项 * 应该 * 会扩展,但我没有测试它。您的树可能需要具有属性“selectable=true”,或者您始终可以在项呈示器中的单击事件中手动更改状态。

相关问题