Flex/AS3 multiple state itemrenderers question
I have created a module with 3 states as an itemrenderer. The states are called Movies, Songs and TvShows. The base state is empty. The itemRenderer consists of Hbox, Vboxes and labels.
And I have created a List component.
What I want to do is to populate data in my List component and make it visible using my ItemRenderer. Depending on the data that is pulled out from the database I want to show the itemRenderer's correct开发者_StackOverflow社区 state. Hence if the record pulled out from the database is a song, I want to display the Song state, if it is a movie, I want to show the Movie state and so on.
So depending on the data that's pulled out, I would like to change the current state of itemrenderer. How would i do that? Can anybody show me an example how i would make this code?
Thanks
override the set data
method of your CustomItemRenderer and assign the appropriate state based on the data.
override public function set data(value:Object):void
{
super.data = value;
if(data.type == "song")
this.currentState = SONG_STATE;
else if(data.type == "movie")
this.currentState = MOVIE_STATE;
else if //and so on...
}
thanks it worked well. The Only thing i notice was that:
this.currentState = MOVIE_STATE;
should be
this.currentState = "MOVIE_STATE";
thanks
DJ
with the above option you need to adjust all your data by looping trough your data arraycollection and setting the type to another state name.
Perhaps a better solution is looping trough all items of the list and change the state;
private function changeItemrendererState():void
{
var item:mycustomitemrenderer;
for(var i:uint = 0; i < itemlist.numElements; i++)
{
item = itemlist.getElementAt(i) as mycustomitemrenderer;
item.currentState = "mynewstate";
}
}
<s:Scroller focusEnabled="false" hasFocusableChildren="true">
<s:DataGroup dataProvider="{mydata}" itemRenderer="skins.mycustomitemrenderer" width="100%" height="100%" id="itemlist">
<s:layout>
<s:TileLayout horizontalGap="2" verticalGap="2" columnAlign="justifyUsingWidth"/>
</s:layout>
</s:DataGroup>
</s:Scroller>
This will not work if you use useVirtualLayout="true" it then would only affect the visible items.
精彩评论