Setting an effect when the items in a data provider change on a List control in Flex
2008-10-26 17:30
656 查看
Setting an effect when the items in a data provider change on a List control in Flex
The following example shows how you can add a data effect when the items in a data provider change in a Flex List control by setting theitemsChangeEffecteffect style.
Full code after the jump.
View MXML
<?xml version="1.0" encoding="utf-8"?> <!-- http://blog.flexexamples.com/2008/07/29/setting-an-effect-when-the-items-in-a-data-provider-change-on-a-list-control-in-flex/ --> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" verticalAlign="middle" backgroundColor="white"> <mx:Script> <![CDATA[ private function addItemToDataProvider():void { var obj:Object = {label:new Date().toTimeString()}; var idx:int = Math.max(list.selectedIndex, 0); list.scrollToIndex(idx); arrColl.addItemAt(obj, idx); } private function removeItemFromDataProvider():void { if (arrColl.length == 0) { return; } var idx:int = Math.max(list.selectedIndex, 0); list.scrollToIndex(idx); arrColl.removeItemAt(idx); } ]]> </mx:Script> <mx:ArrayCollection id="arrColl" /> <mx:DefaultListEffect id="defaultListEffect" /> <mx:ApplicationControlBar dock="true"> <mx:Button label="Add item" click="addItemToDataProvider();" /> <mx:Button label="Delete item" click="removeItemFromDataProvider();" /> </mx:ApplicationControlBar> <mx:List id="list" dataProvider="{arrColl}" itemsChangeEffect="{defaultListEffect}" variableRowHeight="true" width="200" /> </mx:Application>
View source is enabled in the following example.
You can also set the
itemsChangeEffecteffect in an external .CSS file or <mx:Style /> block, as seen in the following snippet:
View MXML
<?xml version="1.0" encoding="utf-8"?> <!-- http://blog.flexexamples.com/2008/07/29/setting-an-effect-when-the-items-in-a-data-provider-change-on-a-list-control-in-flex/ --> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" verticalAlign="middle" backgroundColor="white"> <mx:Script> <![CDATA[ private function addItemToDataProvider():void { var obj:Object = {label:new Date().toTimeString()}; var idx:int = Math.max(list.selectedIndex, 0); list.scrollToIndex(idx); arrColl.addItemAt(obj, idx); } private function removeItemFromDataProvider():void { if (arrColl.length == 0) { return; } var idx:int = Math.max(list.selectedIndex, 0); list.scrollToIndex(idx); arrColl.removeItemAt(idx); } ]]> </mx:Script> <mx:Style> List { itemsChangeEffect: ClassReference("mx.effects.DefaultTileListEffect"); } </mx:Style> <mx:ArrayCollection id="arrColl" /> <mx:ApplicationControlBar dock="true"> <mx:Button label="Add item" click="addItemToDataProvider();" /> <mx:Button label="Delete item" click="removeItemFromDataProvider();" /> </mx:ApplicationControlBar> <mx:List id="list" dataProvider="{arrColl}" variableRowHeight="true" width="200" /> </mx:Application>
Or, you can set the
itemsChangeEffecteffect using ActionScript, as seen in the following example:
View MXML
<?xml version="1.0" encoding="utf-8"?> <!-- http://blog.flexexamples.com/2008/07/29/setting-an-effect-when-the-items-in-a-data-provider-change-on-a-list-control-in-flex/ --> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" verticalAlign="middle" backgroundColor="white"> <mx:Script> <![CDATA[ import mx.effects.DefaultListEffect; private function init():void { list.setStyle("itemsChangeEffect", DefaultListEffect); } private function addItemToDataProvider():void { var obj:Object = {label:new Date().toTimeString()}; var idx:int = Math.max(list.selectedIndex, 0); list.scrollToIndex(idx); arrColl.addItemAt(obj, idx); } private function removeItemFromDataProvider():void { if (arrColl.length == 0) { return; } var idx:int = Math.max(list.selectedIndex, 0); list.scrollToIndex(idx); arrColl.removeItemAt(idx); } ]]> </mx:Script> <mx:ArrayCollection id="arrColl" /> <mx:ApplicationControlBar dock="true"> <mx:Button label="Add item" click="addItemToDataProvider();" /> <mx:Button label="Delete item" click="removeItemFromDataProvider();" /> </mx:ApplicationControlBar> <mx:List id="list" dataProvider="{arrColl}" variableRowHeight="true" width="200" initialize="init();" /> </mx:Application>
相关文章推荐
- Using an XML data provider with the Spark List control in Flex 4
- An explicit value for the identity column in table can only be specified when a column list is used and IDENTITY_INSERT is ON
- Toggling item roll over highlighting on a List control in Flex
- [Ruby on Rails Issue] When Setting Sqlite version on the Gemfile, Show error "An error occurred while installing sqlite3 ",
- Creating a radio button item renderer on a Spark List control in Flex 4
- An implementation of the skip list data structure written in C++
- Changing the default skins on a Button control in Flex
- Animating a Flex PieChart control’s rotation when a user clicks on an item
- An explicit value for the identity column in table 'users' can only be specified when a column list
- Stop Wrapping Exceptions in Exceptions: Use the .Data Collection on an Exception Instead
- Alternating tile background colors for items in a TileList control in Flex
- Loading Data when the User Scrolls to the End of a List in Windows Phone 7
- Zhuanzai: change Asp.net Themes dynamicly (Setting An ASP.NET Theme in the PreInit Event Handler)
- Setting the background color and background alpha on a Flex PopUpButton control’s pop up menu
- Listening for a click on the MX MenuBar control in Flex
- MS bug "The connection pool" in Oracle 10g and the data sort according to specified filed on DataGrid control.
- SyntaxError: JSON.parse: bad control character in string literal at line 1 column 16 of the JSON data
- The method setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener)in the type RadioGroupisnot
- 解决方法:An error occurred on the server when processing the URL. Please contact the system administrator
- Find the 5 repeated items in an array