Flex:为视图堆栈创建多列导航栏

时间:2010-10-05 15:20:36

标签: flex navigation dataprovider viewstack multiple-columns

我创建了一个Viewstack并使用Tile组件并重复LinkBut​​tons我能够使用viewstack作为数据提供者进行多列导航。我的问题是这可以做得更好吗?我的代码如下,我想知道我是否采用了这种方法。

<?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">
    <s:layout>
        <s:BasicLayout />
    </s:layout>

    <fx:Script>
        <![CDATA[
            import mx.controls.Alert;

            private var _listItem:Object;
            private var n:int=0;
            public function get listItem():Object
            {
                return this._listItem;
            }

            public function set listItem(listItem:Object):void
            {
                try{n++;
                    this.changeSelection(this._listItem);               
                }catch(e:Error){}
                if(n==1 || n > this.viewStack.length){
                    this._listItem = listItem;
                    this.changeSelection(listItem);
                }
            }

            private function setSelection(obj:Object):void{
                this.viewStack.selectedIndex = this.viewStack.getChildIndex(this.viewStack.getChildByName(obj.target.getRepeaterItem().name));
                this.listItem = obj.target; 
            }
            private function checkSelection(obj:Object):void{               
                if(obj.target.getRepeaterItem() == this.viewStack.selectedChild){
                    if(this.listItem != obj.target){
                        this.listItem = obj.target; 
                    }
                }               
            }
            private function changeSelection(obj:Object):void{              
                if(obj.getRepeaterItem() == this.viewStack.selectedChild){
                    obj.setStyle("color","#000000");    
                }else{
                    obj.setStyle("color","#999999");
                }               
            }
        ]]>
    </fx:Script>

    <mx:Tile id="tiles" horizontalGap="20" verticalGap="0" y="210" direction="vertical">        
        <mx:Repeater id="masterList" dataProvider="{viewStack}">
            <mx:LinkButton 
                id="btn" 
                label="{masterList.currentItem.label}" 
                click="this.setSelection(event)"
                color="#999999"
                creationComplete="checkSelection(event);" />
        </mx:Repeater>
    </mx:Tile>

    <mx:ViewStack id="viewStack"  height="200" width="300" backgroundColor="#000000" >      
        <mx:VBox id="vb1" backgroundColor="#FF0000" label="Screen One"/>        
        <mx:VBox id="vb2" backgroundColor="#00FF00" label="Screen Two"/>        
        <mx:VBox id="vb3" backgroundColor="#0000FF" label="Screen Three"/>          
        <mx:VBox id="vb4" backgroundColor="#00FFFF" label="Screen Four"/>  
    </mx:ViewStack>

</s:Application>

1 个答案:

答案 0 :(得分:0)

在我看来,你已经拥有了不同的导航链接,并且这些链接会根据选择的链接改变颜色。假设是这种情况,它听起来很像基于标签的导航模型。我的方法是使用spark TabBar并将选项卡外观看起来像链接。这样你就可以摆脱大部分代码,让标签皮肤处理根据当前状态改变颜色。此外,您不需要任何更改视图堆栈的代码,因为TabBar会为您处理。希望有所帮助。