分享

Flex移动开发

 nywrb 2012-10-18
SplitViewNavigator是Flex 4.6的另一个组件对象,主要用于当你想在应用程序中创建一个主视图(master view)或细节视图(detail view)时的版面开发,而主视图一般用作导航的源,细节视图作为主视图上动作的结果展示。根据布局,主视图一般位于屏幕的左边或上边,而细节视图一般位于右边或底部。如果你对于这个屏幕形式不熟悉,可以点这个链接查看。一个很好的例子就是你的一般邮件应用,比方说我的一个iPad邮件账号。

svn01.png

现在我可以简单的使用Flex 4.6的新特性用这类布局来创建应用程序了!SplitViewNavigator扩展了ViewNavigatorBase,所以它没有定位在应用程序的根级而是像ViewNavigator一样定位在子级。注意这个组件只能用在一个空的ApplicationTabbedViewNavigatorApplication里,以下是例子:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
  3.                xmlns:s="library://ns.adobe.com/flex/spark" applicationDPI="160">

  4.     <s:SplitViewNavigator id="svn" width="100%" height="100%">
  5.         <s:ViewNavigator id="leftNav" width="30%" height="100%" firstView="views.LeftView"/>
  6.         <s:ViewNavigator id="rightNav" width="100%" height="100%" firstView="views.RightView"/>
  7.     </s:SplitViewNavigator>
  8. </s:Application>
复制代码
在这里我的左视图和右视图只是简单的View类,它展示了一列可点击的城市,以及一些在细节视图中关于特别城市的繁琐的数据。我会在下面加入代码作参考:

LeftView.mxml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
  3.         xmlns:s="library://ns.adobe.com/flex/spark" title="Left View">

  4.     <fx:Script>
  5.         <![CDATA[
  6.             protected function list_clickHandler(event:MouseEvent):void
  7.             {
  8.                 (this.parentDocument as Sample).rightNav.activeView.data=list.selectedItem;
  9.             }
  10.         ]]>
  11.     </fx:Script>

  12.     <s:List id="list" width="100%" height="100%" click="list_clickHandler(event)">
  13.         <s:dataProvider>
  14.             <s:ArrayCollection>
  15.                 <fx:String>Atlanta, GA</fx:String>
  16.                 <fx:String>Baltimore, MD</fx:String>
  17.                 <fx:String>Boston, MA</fx:String>
  18.                 <fx:String>Chicago, IL</fx:String>
  19.                 <fx:String>Dallas, TX</fx:String>
  20.                 <fx:String>Detroit, MI</fx:String>
  21.                 <fx:String>Honolulu, HI</fx:String>
  22.                 <fx:String>Los Angeles, CA</fx:String>
  23.                 <fx:String>Minneapolis, MA</fx:String>
  24.                 <fx:String>New Orleans, LA</fx:String>
  25.                 <fx:String>New York, NY</fx:String>
  26.                 <fx:String>Richmond, VA</fx:String>
  27.                 <fx:String>San Francisco, CA</fx:String>
  28.                 <fx:String>San Jose, CA</fx:String>
  29.                 <fx:String>St. Louis, MO</fx:String>
  30.                 <fx:String>Washington, DC</fx:String>
  31.             </s:ArrayCollection>
  32.         </s:dataProvider>
  33.     </s:List>
  34. </s:View>
复制代码
RightView.mxml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <s:View xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" title="Detail View">
  3.     <s:layout>
  4.         <s:VerticalLayout paddingTop="15" paddingBottom="15" paddingLeft="15" paddingRight="15" gap="5"
  5.                           horizontalAlign="center" verticalAlign="top"/>
  6.     </s:layout>
  7.     <s:Label text="Click on a location on the left to explore!" visible="{data==null?true:false}"/>
  8.     <s:Label text="Information about {this.data}" visible="{data!=null?true:false}"/>

  9.     <s:TextArea text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur accumsan felis ac tortor aliquam iaculis. Phasellus hendrerit viverra enim, sit amet scelerisque lectus dictum at. Aenean sodales nisi sed leo congue et porttitor ligula vehicula.
  10. Pellentesque turpis massa, suscipit vel fermentum quis, dignissim sed ipsum. Nulla aliquet libero adipiscing risus lobortis eleifend quis at velit. Duis at leo urna.
  11. Praesent facilisis faucibus neque, ut ullamcorper lacus gravida a. Donec vel iaculis sapien."  width="90%" editable="false" visible="{data!=null?true:false}"/>
  12. </s:View>
复制代码
应用程序结果:

svnLand (1).png

你也可以在SplitViewNavigator容器中设置布局,比方说你想将其垂直分离,你可以设置一个VerticalLayout并调整高度比例同时宽度设为100%,如:
  1. <s:SplitViewNavigator id="svn" width="100%" height="100%">
  2.     <s:layout>
  3.          <s:VerticalLayout/>
  4.     </s:layout>
  5.     <s:ViewNavigator id="leftNav" width="100%" height="30%" firstView="views.LeftView"/>
  6.     <s:ViewNavigator id="rightNav" width="100%" height="70%" firstView="views.RightView"/>
  7. </s:SplitViewNavigator>
复制代码
这是更新后的布局:

svnVert.png

分离视图的方法对于横向模式比较适用,而在纵向模式中你可能想要不同的显示方式因为屏幕宽度更小了。还有一些内置特性可以帮你做到。在SplitViewNavigator中有一个 autoHideFirstViewNavigator 属性,在应用程序接收到纵向改变事件时可以自动隐藏在左边视图中。
  1. <s:SplitViewNavigator id="svn" width="100%" height="100%" autoHideFirstViewNavigator="true">
  2.     <s:ViewNavigator id="leftNav" width="30%" height="100%" firstView="views.LeftView"/>
  3.     <s:ViewNavigator id="rightNav" width="100%" height="100%" firstView="views.RightView"/>
  4. </s:SplitViewNavigator>
复制代码
这时运行我们会看到:

svnAuto.png

其实这样也不是很有用,因为在我们的列表中并没有导航到不同项目的路径!所以我们需要再深入一些,在纵向模式的时候,我们可以使用SplitViewNavigatorshowViewNavigatorInPopUp函数来弹出一个在Callout中的列表。我们从ViewNavigator右边的actionBar即navigationContent的一个按钮中调用这个函数,比如以下代码:
  1. <s:SplitViewNavigator id="svn" width="100%" height="100%" autoHideFirstViewNavigator="true">
  2.         <s:ViewNavigator id="leftNav" width="30%" height="100%" firstView="views.LeftView"/>
  3.         <s:ViewNavigator id="rightNav" width="100%" height="100%" firstView="views.RightView" >
  4.             <s:navigationContent>
  5.                 <s:Button id="listButton" label="List" click="svn.showFirstViewNavigatorInPopUp(listButton)"/>
  6.             </s:navigationContent>
  7.         </s:ViewNavigator>
  8. </s:SplitViewNavigator>
复制代码
注意:更清楚的说,在上面我们用的actionBar已经隐定义为Spark ViewNavigator容器的一部分而且将父级设置为listButton的弹出窗口,这样Callout或是弹出的箭头都源于那个按钮。这就是在纵向模式时以上代码运行的结果了:

svnPortrait.png

现在,我们可能希望在回到横向模式时这个按钮不要显示出来,所以我们需要在横向模式中添加状态操作使其不可。决定什么时候更新状态的其中一种方式就是添加resize事件句柄,根据检验出来的宽高值就能更新状态了。我们的代码现在看起来如下:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
  3.                xmlns:s="library://ns.adobe.com/flex/spark" applicationDPI="160" resize="application1_resizeHandler(event)">
  4.     <fx:Script>
  5.         <![CDATA[
  6.             import mx.events.ResizeEvent;

  7.             protected function application1_resizeHandler(event:ResizeEvent):void
  8.             {
  9.                 if (width>height)
  10.                     this.currentState="landscape";
  11.                 else this.currentState="portrait";
  12.             }
  13.         ]]>
  14.     </fx:Script>

  15.     <s:states>
  16.         <s:State name="portrait"/>
  17.         <s:State name="landscape"/>
  18.     </s:states>

  19.     <s:SplitViewNavigator id="svn" width="100%" height="100%" autoHideFirstViewNavigator="true">
  20.         <s:ViewNavigator id="leftNav" width="30%" height="100%" firstView="views.LeftView"/>
  21.         <s:ViewNavigator id="rightNav" width="100%" height="100%" firstView="views.RightView" >
  22.             <s:navigationContent>
  23.                 <s:Button id="listButton" label="List" click="svn.showFirstViewNavigatorInPopUp(listButton)" visible.landscape="false"/>
  24.             </s:navigationContent>
  25.         </s:ViewNavigator>
  26.     </s:SplitViewNavigator>
  27. </s:Application>
复制代码
当我们在横向模式中运行程序时,我们会看到List按钮没有在actionBar里面显示:

svnLand.png

你可以点击这里下载程序的源代码和项目文件!

还有,如果你还没看到这里,Christophe和我创建了一个庞大的基于SplitViewNavigator的消费追踪面板的应用程序,XpenseIt,它也包含了许多其他Flex 4.6特性,点击这里可以下载。以下是应用程序的一个截图:

svn2.png

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多