This request comes to us from a faithful reader who was wondering how you can display a full image when a user clicks on a thumbnail in a HorizontalList control in Flex. My solution was to put the thumbnail image and full image URLs in the data provider and then use bindings to the HorizontalList control’s selectedItem property.
Full code after the jump.
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/02/15/creating-a-simple-image-gallery-with-the-flex-horizontallist-control/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
verticalAlign="top"
backgroundColor="white">
<mx:Array id="arr">
<mx:Object label="Flex"
thumbnailImage="assets/fx_appicon-tn.gif"
fullImage="assets/fx_appicon.jpg" />
<mx:Object label="Flash"
thumbnailImage="assets/fl_appicon-tn.gif"
fullImage="assets/fl_appicon.jpg" />
<mx:Object label="Illustrator"
thumbnailImage="assets/ai_appicon-tn.gif"
fullImage="assets/ai_appicon.jpg" />
<mx:Object label="Dreamweaver"
thumbnailImage="assets/dw_appicon-tn.gif"
fullImage="assets/dw_appicon.jpg" />
<mx:Object label="ColdFusion"
thumbnailImage="assets/cf_appicon-tn.gif"
fullImage="assets/cf_appicon.jpg" />
<mx:Object label="Flash Player"
thumbnailImage="assets/fl_player_appicon-tn.gif"
fullImage="assets/fl_player_appicon.jpg" />
<mx:Object label="Fireworks"
thumbnailImage="assets/fw_appicon-tn.gif"
fullImage="assets/fw_appicon.jpg" />
<mx:Object label="Lightroom"
thumbnailImage="assets/lr_appicon-tn.gif"
fullImage="assets/lr_appicon.jpg" />
<mx:Object label="Photoshop"
thumbnailImage="assets/ps_appicon-tn.gif"
fullImage="assets/ps_appicon.jpg" />
</mx:Array>
<mx:Panel title="{horizontalList.selectedItem.label}"
height="100%"
horizontalAlign="center">
<mx:Image id="img"
source="{horizontalList.selectedItem.fullImage}"
maintainAspectRatio="true"
horizontalAlign="center"
width="{horizontalList.width}"
height="100%" />
<mx:ControlBar horizontalAlign="center">
<mx:HorizontalList id="horizontalList"
labelField="label"
iconField="thumbnailImage"
dataProvider="{arr}"
itemRenderer="CustomItemRenderer"
columnCount="4"
columnWidth="125"
rowCount="1"
rowHeight="100"
horizontalScrollPolicy="on" />
</mx:ControlBar>
</mx:Panel>
</mx:Application>
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/02/15/creating-a-simple-image-gallery-with-the-flex-horizontallist-control/ -->
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml"
horizontalAlign="center"
verticalAlign="middle">
<mx:Image source="{data.thumbnailImage}" />
<mx:Label text="{data.label}" />
</mx:VBox>
View source is enabled in the following example.





Nice!! I love mnemonics.
Very good.
I’m new with adobe flex and I think that your blog is the best I have seen. You put a lot of things.
I could make the same, but loading a video with some controls (play,stop, dull screen)?
Guiso,
I don’t have any examples on a simple video gallery using a HorizontalList control, but it should be very similar to the example above. Simply replace the Image control with a VideoDisplay control. Then whenever you click on the thumbnail image in the HorizontalList control the FLV would be loaded instead of a JPEG.
As for the play/pause/stop controls for the video, check out some of the VideoDisplay specific posts on this blog at http://blog.flexexamples.com/category/videodisplay/.
Hope that helps,
Peter
Thanks for doing this example. It encourages me to forget about my approach of embedding the thumbnails in the application — unnecessary overhead, and mine is probably not a good use of binding. Though my usage is a bit different, maybe I’ll see if I can modify it and go for something more like what you’ve done. Since I’m not using labels for my thumbs, I don’t need the custom item renderer but, again, thanks for show an example that includes one.
Would it be possible to dress this up a bit? For example, when the image is clicked, have it gradually scale to the final size, and move to that location while it is enlarging?
Thanks!
Hi, il’d like to tell that an italian site (sigh, i’m italian) did a COPY and PASTE of your code (the modified only the number and name of mx:objects). Reading today i rembered that you posted some days ago so… i hate the fact that they don’t say that the source is from you.
http://www.augitaly.com/flexgala/index.php?cmd=newsreader&id=220.
Neogene,
Thanks for the heads up. I don’t speak any Italian, but a few of their articles look VERY similar to mine (again, they only changed some variable names and translated English into Italian).
Hhhmmm,
Peter
Don’ use “look”: better to say ARE EQUALS.
Italian style :-(
Hey justice is done :-) Now they linked to your site and write that the article is “inspired” from you post :-)
Neogene,
Yeah, it was just a simple misunderstanding. I emailed them and they were very apologetic and understanding. The posts are all released under Creative Commons License (see the link in the footer for exact details), so basically everybody is free to use/remix the examples, as long as they cite the source (this blog), and release the code under a similar Creative Commons License.
Thanks again,
Peter
Ive Been Looking for a Web Gallery For a While Now, and I Love Yours.
Im New To Flex.
Question tho.
is there a way to make the horizontal bar smaller in height and have the images not be scrolling up and down?
-Chris
Thanks for the article. I really like what you have done. My first two questions that come to mind are:
1. Will this handle sizing images that have different heights and widths than others?
2. How would you set this up to handle a folder of pictures that you would like to read each time it is loaded? For instance, you want a gallery that will search a folder for images as it loads and build the image viewer based off the images in the folder. Is this possible?
Thanks,
Jason
Jason,
My first two answers that come to mind are:
1. Probably. I never tried, it may need some tweaking.
2. Possible in something like AIR since an AIR app would have file system permissions. If you were doing a Flex on the web, you would need a server-side script (ColdFusion, PHP, JSP, ASP, Ruby, etc) to get a directory listing, and return a list of all the images. Although in most cases I think you would want to just create a static XML file containing image names/paths and load that. It would probably be a bit more efficient than doing a file system check each time the Flex app is loaded. You could probably even set up a scheduled task on the server that would get a directory listing once every hour and update the XML file accordingly.
Peter
Very nice Article.
I am new to Flex, am looking for the Canvas to canvas drag and drop images. and save the canvas images to the database and retrieve it. how can i?
Can u please give me solution.
Swamy
Very nice Article.
I am new to Flex, am looking for the Canvas to canvas drag and drop images. and save the canvas images to the database and retrieve it. I finished the canvas to canvas dragging, only i need the solution for saving the current canvas images to the database and then retrieve to the same canvas. how can i?
Can u please give me solution.
Swamy
Very nice.
I am new to Flex, how about for slide show with thumbnail, and if the image is being displayed there is a highlight in the thumbnail image and every image that are going to display their thumbnail is at the center of horizontal list.
Please help me.
Thanks in advance.
I am using an itemRenderer and in that renderer i am calling the init() method of mine on creationComplete even of the renderer component.
The List dataprovider is an ArrayCollection from LCDS rtmp and i notice that the init() function does not get called everytime a new item is added.
So I guess my question is, how can i EVERYTIME call a function in my itemRenderer component?
Hey - thanks for sharing! Great post for newbies like me just getting up and running from Flash to Flex.
All’s working great but I’m now trying to add a on mouse over functionality in order to display the image title on hovering over a thumbnail (rather than after clicking to select).
Slightly changing your original code:
displays the label attribute on *selectedItem* but I’d want something like:
- but can’t seem to find anything remotely like horizontalList.highlightedItem in Flex3 docs for the horizontalList class…
I can’t be the first person who’s needed to do this. Can anyone help?
!!!aarghh, comment box stripped out my code!!!
Let’s try again. For a Text component, I’m looking to change:
text="{horizontalList.selectedItem.label}to something along the lines of:
text="{horizontalList.highlightedItem.label}(but ‘highlightedItem’ isn’t part of the flex api)
baroquedub,
What about using the
itemRollOverevent?<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" verticalAlign="middle" backgroundColor="white"> <mx:Script> <![CDATA[ import mx.events.ListEvent; private function horizontalList_itemRollOver(evt:ListEvent):void { lbl.text = evt.itemRenderer.data.label; } ]]> </mx:Script> <mx:Array id="arr"> <mx:Object label="One" /> <mx:Object label="Two" /> <mx:Object label="Three" /> <mx:Object label="Four" /> <mx:Object label="Five" /> <mx:Object label="Six" /> <mx:Object label="Seven" /> <mx:Object label="Eight" /> <mx:Object label="Nine" /> <mx:Object label="Ten" /> </mx:Array> <mx:HorizontalList id="horizontalList" dataProvider="{arr}" rowHeight="100" rowCount="1" columnWidth="100" columnCount="5" itemRollOver="horizontalList_itemRollOver(event);" /> <mx:Label id="lbl" /> </mx:Application>Peter
Great article - I’m new to Flex and this has really helped my understanding of the software.
I have one question though -
Is it possible to have the first image displayed when the gallery is loaded? Currently, the gallery is blank when first loaded, but I would like to have the first image shown by default, instead of the user clicking on a thumbnail.
Thanks in advance
Micanio
Micanio,
Sure thing, just set the
selectedIndexproperty to 0 (to have the first item pre-selected) and that should be all you need:<mx:HorizontalList id="horizontalList" labelField="label" iconField="thumbnailImage" dataProvider="{arr}" selectedIndex="0" itemRenderer="CustomItemRenderer" columnCount="4" columnWidth="125" rowCount="1" rowHeight="100" horizontalScrollPolicy="on" />Peter
Thats perfect - thanks very much!!!!
the articals are too exiting for freshers
you site is just like a magnet attracting a lot
Thanks for these articles Peterd. Like alot of people here I’m new to Flex too, and you’ve provided great little examples that help teach.
Pete (II)
Hi Peterd
Can I apply the itemRollOver event (below) on the images not just on the labels
What about using the itemRollOver event?
I forgot to escape my characters, the code I am refering to is the one posted Apr 15th, 2008 at 12:46 pm
I figured it out
import mx.events.ListEvent; private function tileList_itemRollOver(evt:ListEvent):void { Thumb.source = evt.itemRenderer.data.thumbnailImage; }Thanks
nice work Peter!
Can u tell me how to add a button on the both sides(in the beginning and in the end also) so that I can get rid of these horizontal scroller and it will look pretty .
I trid to put button inside the Horizontal list as
mx:HorizontalList xmlns:mx=”http://www.adobe.com/2006/mxml” width=”442″ height=”86″>
But it does’nt show buttons at run time.
I m very new to Flex so I don’t know why its not showing up on run time.
Can you suggest me how to do that!
Thanks & Regards!
Amar!
amarshukla123@gmail.com
code was-
actually i m making a custom component.Problem is as written above.
Thanks & Regards!
Amar!
amarshukla123@gmail.com
Is there a way to set lineScrollsize of the scrollbar in HorizontalList ?
In this example, when you click at the scrollbar ’s arrow, one item is scrolled at a time. I’m looking for a way to make more than one item to scroll at a time. Any example will be appreciated . Thanks!
Id like to change the size of the thumbnails and have 2 rows, but then i messed up the area for the large image. any ideas
thanks
beautiful work
fixed the thumbail problem, just took time playing with the code.
how would i go about adding transisions to the large images. fade in etc
thanks
MarcMM,
The easiest way is probably to set a
completeEffecton the image/thumbnail:<mx:Image id="img" source="{horizontalList.selectedItem.fullImage}" maintainAspectRatio="true" horizontalAlign="center" width="{horizontalList.width}" height="100%" completeEffect="Fade" />Peter
Thanks for this ,
how would i have 2 rows? ive been playing with it but it stretches and the main image becomes smaller, but i need more images and no scroll
thanks