开发者

PopView to the location on screen we were at

I have a Blackberry playbook Flex app that has a form. There is a TextArea input that when "focused in", pushes a new view with a full screen text area. When that full screen text area is filled, a button is clicked and the screen is popped to the original form...

The problem: When I pop this 2nd screen, it returns to the first form at the top, not retaining the view position we 开发者_如何学编程were at on the page... is there anyway to return to the part of the form we were editting?


You can push the edit-view with a parameter [1] that contains the last scroll-coordinates and when poping [2] the view you could set these coordinates as return value, so the first form-view can scroll back down [3].

[1] The second argument of pushView takes any object:

navigator.pushView(EditView, {position:"someValue"});

[2] Override createReturnObject to create a return object when poping a view:

public function createReturnObject():Object

[3] in the viewActivate eventhandler the data-property will hold your pushed object.


Taking inspiration from alopix's response I came up with the following solution that worked across the entire app. I have to extend the View class:

MyView.as

This was needed so that each time the view became active (was viewed), its verticalScrollPosition would be stored value in a global variable (shown next). "mainContent" is the ID of the MyGroup that stores everything on the actual MXML view (so we can track it and update its verticalScrollPosition).

public class MyView extends View
{

    public function MyView()
    {
        super();
        this.addEventListener(FlexEvent.VIEW_ACTIVATE, goToCorrectViewpoint);
    }

    private function goToCorrectViewpoint(e:Event):void{
        trace("Setting vertical position to "+FlexGlobals.topLevelApplication.verticalPositionTracker[this.title]);
        if(FlexGlobals.topLevelApplication.verticalPositionTracker.hasOwnProperty(this.title))
            (this["mainContent"] as Group).verticalScrollPosition = FlexGlobals.topLevelApplication.verticalPositionTracker[this.title];
    }
}

The Global variable for storing the verticalScrollPosition of each Page

I had to store all the Y coordinates in a Global variable called verticalPositionTracker. This was just an Object of "Page Title" => verticalScrollPosition (eg. the Y Coordinate) e.g:

  1. Login Page -> 120
  2. Results Page ->200
  3. etc...

    <?xml version="1.0" encoding="utf-8"?>
    <s:MobileApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                 xmlns:s="library://ns.adobe.com/flex/spark" firstView="view.LoginView" backgroundColor="0xF5FAFF">
    
    <fx:Script>
    <![CDATA[
        public var verticalPositionTracker:Object = new Object();
    ]]>
    </fx:Script>
    
    </s:MobileApplication>
    

MyGroup

I then had to create a custom Group (that would contain all the data in the scroller), so that when the user scrolled down a page (changing the verticalScrollPosition), the Global variable would be updated with the new position.

public class AirpointGroup extends VGroup
{

    public var pageID:String;

    public function AirpointGroup()
    {
        super();
    }

    override public function set verticalScrollPosition(value:Number):void{
        if (layout)
        {
            layout.verticalScrollPosition = value;
        }
        trace("The position of "+this.pageID+" has changed to "+value);
        if(value!=0)
            FlexGlobals.topLevelApplication.verticalPositionTracker[pageID] = value;
    }
}

Results.mxml (Working example) I then included this group in a view with the ID of "mainContent" (important as it allows the view to know which group to set the y-coordinate to)

<?xml version="1.0" encoding="utf-8"?>
<components:MyView xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:components="components.*"
    xmlns:s="library://ns.adobe.com/flex/spark" title="Tasks" xmlns:mx="library://ns.adobe.com/flex/mx">

<s:Scroller left="0" right="0" top="0" bottom="0">
    <components:MyGroup pageID="{this.title}" id="mainContent">
                  <!-- ALL CONTENT GOES HERE -->
    </components:MyGroup>
</s:Scroller>


</components:MyView>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜