Sorting List without propagating event for resetting scrollbar

205 views Asked by At

I have a spark List and using ArrayCollection as dataprovider for list. Initially when the list is populated sorting is done , but i need to add items to list quite frequently as they update on server. After i add item i have to re-sort the list. After applying sort and calling refresh() scrollbar resets itself to top.

I am looking for a way so that i can add items to list and keep sorting the list without resetting the scrollbar to top position.

I have read similar question earlier but in that question most users have said to store the VerticalScrollPostion in a variable and apply that VerticalScrollPosition back after the sorting is done. The problem with this is there is a jerk in scrollbar , as it goes to top after refresh and then scrolls back to position as applied. So this solution is not possible as items are added very frequently as it will make the scrollbar going up and down very frequently and annoying the users. Also does't look nice n professional

I need a way so that scrollbar stays in it place even after sorting is done. Also i want to keep using useVirtualLayout=true for the List.

I was also trying to extent ArrayCollection but could't find a way to solve my problem.

Below is simple example(extracted from my main application) to illustrate the problem. Just Scroll to bottom of list and then press the Sort button(which will sort the list) and the scrollbar will jump to top position. I want scrollbar to stay at its position even after pressing Sort button:

<?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" minWidth="955" minHeight="600" creationComplete="application1_creationCompleteHandler(event)">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <fx:Script>
        <![CDATA[               
            import mx.collections.ArrayCollection;
            import mx.collections.SortField;

            import spark.collections.Sort;
            [Bindable] protected var ar_c:ArrayCollection;
            protected var dgArray:Array=new Array();
            protected function application1_creationCompleteHandler(event:FlexEvent):void
            {

                for(var i:int=0;i<20;i++)
                dgArray.splice(-1,0,{label:"h"+i});
                ar_c= new ArrayCollection(dgArray);

            }


            protected function sort_list():void{
                var sort:spark.collections.Sort=new Sort();             
                sort.fields=[new SortField("label",false,true)];
                ar_c.sort=sort;
                ar_c.refresh();

            }
        ]]>
    </fx:Script>
    <s:List x="42" y="41" id="mylist" width="199" height="253" dataProvider="{ar_c}"  useVirtualLayout="true"></s:List>
    <s:Button x="295" y="40" label="Sort" click="sort_list();"/>
</s:Application>
1

There are 1 answers

0
Black Dynamite On BEST ANSWER

I think a sort is applied once and then everything added to the ArrayCollection later on is sorted on. You only have to add the sort once, and then call refresh once to apply the sort.