2 Replies Latest reply on Oct 28, 2011 11:49 AM by Shongrunden

    Setting selectedIndex on view stack after removal

    mr_malee Level 1

      I'm having an issue where when I remove an element from a ViewStack its not selecting the previous item in the list. It keeps snapping back to index 0.

       

      here's some simple code:

       

      <?xml version="1.0" encoding="utf-8"?>
      <s:WindowedApplication 
      
      
                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="onCreationComplete();"
      >
                <fx:Script>
                          <![CDATA[
                                    import spark.components.Label;
                                    import spark.components.NavigatorContent;
        
                                    private function onCreationComplete():void {
        
                                              for (var i:int = 0; i < 10; i++) {
        
                                                        addNew();
                                              }
                                    }
      
      
                                    private function removeCurrent():void {
        
                                              var child:NavigatorContent = layouts.selectedChild as NavigatorContent;
        
                                              if (child) {
        
                                                        layouts.removeChild(child);
                                              }
                                    } 
        
                                    private function addNew():void {
        
                                              var title:String = "item " + layouts.length;
        
                                              var content:NavigatorContent = new NavigatorContent();
                                              content.label = title;
        
                                              var label:Label = new Label();
                                              label.text = title;
        
                                              content.addElement(label);
        
                                              layouts.addElement(content);
                                    }
                          ]]>
                </fx:Script>
      
      
                <s:layout>
              <s:VerticalLayout/>
          </s:layout>
          
          <!-- Drop down list to show the view stack, uses double data binding -->
                <s:DropDownList id="dropDown" width="200" labelField="label" dataProvider="{layouts}" selectedIndex="@{layouts.selectedIndex}" />
        
                <!-- TabBar to show the view stack, uses single data binding -->
                <s:TabBar id="tabBar" width="100%" dataProvider="{layouts}" />
      
      
                <!-- Content view stack -->
                <mx:ViewStack id="layouts">
                </mx:ViewStack>
        
                <!-- Buttons to test -->
                <s:Button label="remove" click="removeCurrent();"/>
                <s:Button label="add" click="addNew();" />
      
      
      </s:WindowedApplication>
      
      

       

      when you remove the current item, the stack selects item 0. I cannot seem to override this. I want the view stack to select the previous item in the list