Creating a simple image gallery with the Flex HorizontalList control
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 name="HorizontalList_selectedItem_test" 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 name="CustomItemRenderer" 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.
Peter deHaan
Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.
-
Add Widgets (Content Sidebar)
This is your Content Sidebar. Edit this content that appears here in the widgets panel by adding or removing widgets in the Content Sidebar area.
76 Responses to Creating a simple image gallery with the Flex HorizontalList control
Leave a Reply Cancel reply
-
Categories
- Accordion
- AccordionHeader
- ActionScript
- AddChild
- AdvancedDataGrid
- Alert
- alpha
- Animate
- AnimateProperties
- Application
- Application (Spark)
- ArrayCollection
- BarChart
- baseColor
- beta
- beta1
- beta2
- Bitmap
- Bitmap/BitmapData
- BitmapData
- BitmapFill
- BitmapFill (Spark)
- BitmapGraphic
- BitmapImage
- BitmapImage (Spark)
- BitmapImageResizeMode
- Border (Spark)
- BorderContainer (Spark)
- Box
- BuildInfo
- Button
- Button (Spark)
- ButtonBar
- ButtonBar (Spark)
- ByteArray
- Camera
- Charting
- CheckBox
- CheckBox (Spark)
- ClassFactory
- CollectionEvent
- Color
- ColorPicker
- ColorUtil
- ComboBox
- ComboBoxArrowSkin
- Compiler
- Component
- Component (Spark)
- Configuration
- Container
- ContextMenu
- ContextMenuEvent
- ContextMenuItem
- CSSCondition
- CSSSelector
- CSSStyleDeclaration
- CurrencyFormatter
- CursorManager
- Data Binding
- DataGrid
- DataGrid (Spark)
- DataGridColumn
- Date
- DateBase
- DateChooser
- DateField
- DateFormatter
- Debugging
- DefaultComplexItemRenderer
- DefaultTileListEffect
- DropDownList
- DropDownList (Spark)
- DropDownListButtonSkin
- DropDownListSkin
- DropShadowFilter
- E4X
- Effects
- Ellipse
- EmailValidator
- Embed
- Event
- Fade
- FileFilter
- FileReference
- fill
- Filters
- Flash
- Flash Integration
- FlashVars
- Flex 3 SDK
- Flex Builder
- Flex Builder 3
- Flex SDK
- Flex4
- FLVPlayback
- FocusManager
- FontLookup
- Fonts
- Form
- Form (Spark)
- FormHeading (Spark)
- FormItem
- FormItem (Spark)
- Forms
- FTETextField (Spark)
- FullScreen
- FullScreenEvent
- FxAnimateColor
- FxButtonBar
- FxCheckBox
- FXG
- FxHScrollBar
- FxHSlider
- FxList
- FxNumericStepper
- FxRadioButton
- FxRotate3D
- FxScroller
- FxTextArea
- FxTextInput
- FxToggleButton
- FxVScrollBar
- FxVSlider
- getStyleDeclaration()
- GradientEntry
- Graphic (Spark)
- HBox
- HDividedBox
- HGroup (Spark)
- HorizontalLayout
- HorizontalList
- HSBColor (Spark)
- HScrollBar (Spark)
- HSlider
- HSlider (Spark)
- HTML template
- ID3Info
- Image
- Image (Spark)
- ImageSnapshot
- itemRenderer
- JointStyle
- Label
- Label (Spark)
- Legend
- LegendItem
- LigatureLevel
- Line
- LinearGradientStroke
- LineScaleMode
- LinkBar
- LinkButton
- List
- List (Spark)
- Menu
- MenuBar
- Metadata
- MetadataEvent
- Model
- Mouse
- MouseCursor
- MouseEvent
- Move
- Namespace
- NavigatorContent (Spark)
- needsSWF
- NetConnection
- NetStream
- Nightly Builds
- NumberBaseRoundType
- NumberFormatter
- NumberValidator
- NumericCompare
- NumericStepper
- NumericStepper (Spark)
- ObjectProxy
- ObjectUtil
- paddingLeft
- paddingRight
- Panel
- Panel (Spark)
- Parallel
- Path
- PieChart
- PieSeries
- PieSeriesItem
- PopUpAnchor (Spark)
- PopUpButton
- PopUpManager
- ProgrammaticSkin
- ProgressBar
- PropertyChangeEvent
- QName
- RadialGradient
- RadioButton
- RadioButton (Spark)
- RadioButtonGroup
- RadioButtonGroup (Spark)
- Rect
- RegExp
- Regular Expressions
- Repeater
- RichEditableText
- RichText
- RichText (Spark)
- RichTextEditor
- Rotate
- Rotate3D (Spark)
- Scroller (Spark)
- Sequence
- setStyle()
- SimpleText
- SimpleText (Spark)
- skinClass
- Slider
- SliderEvent
- SolidColor
- SolidColorStroke
- Sort
- SortField
- Sound
- SoundEffect
- Spinner (Spark)
- SpriteVisualElement (Spark)
- StageDisplayState
- States
- StringUtil
- StringValidator
- StyleManager
- Styles
- SWFLoader
- SWFObject
- System
- SystemManager
- TabBar
- TabBar (Spark)
- TabNavigator
- TabStopFormat
- Text
- Text Layout Framework (TLF)
- TextArea
- TextArea (Spark)
- TextBox
- TextConverter
- TextEvent
- TextFlow
- TextFlowUtil
- TextFormat
- TextGraphic
- TextInput
- TextInput (Spark)
- TextLayoutFormat
- TextView
- Themes
- TileLayout
- TileList
- TileOrientation
- Timer
- TitleWindow
- TitleWindow (Spark)
- TLF
- ToggleButton (Spark)
- ToggleButtonBar
- ToolTip
- Transition
- Tree
- TruncationOptions
- UIComponent
- UIFTETextField
- Updater
- URLLoader
- URLRequest
- URLUtil
- URLVariables
- ValidationResultEvent
- Validator
- Validators
- VBox
- VDividedBox
- Vector
- VerticalLayout
- VerticalLayout (Spark)
- VGroup (Spark)
- Video
- VideoDisplay
- VideoElement
- VideoElement (Spark)
- VideoEvent
- VideoPlayer (Spark)
- VideoPlayerScrubBar
- ViewStack
- VScrollBar (Spark)
- VSlider
- VSlider (Spark)
- XML
- XMLList
- XMLListCollection
- ZipCodeValidator
- ZipCodeValidatorDomainType
- Zoom
-
Articles
- December 2010
- November 2010
- October 2010
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
-
Meta


hi,
i have tested your code also with Flex 3 Builder and SDK flex_sdk_3.4.0.6955
in this version i get also the binding warning
warning: unable to bind to property ‘label’ on class ‘Object’ (class is not an IEventDispatcher)
warning: unable to bind to property ‘thumbnailImage’ on class ‘Object’ (class is not an IEventDispatcher)
do you have an idear what is going wrong here ???
I notice that everytime you scroll the component keeps loading in images. I understand that this is probably due to itemrenderer recycling, but surely there is a way to cache the images.
@Neil,
Check out Ely Greenfield’s SuperImage component; “Some thoughts on Flex vs. HTML (or… ‘how I made my Flex List Images stop flickering.’)”.
Peter
I was having problems with this too when following the example code in the CustomItemRenderer. However, after I downloaded the source code, I noticed the “name” property was missing in the source code. I removed the “name” property from my version and the compiled swf works beautifully. Please correct the example code to match the downloadable source code for others who are surely experiencing problems as well.
Thank you for an otherwise good example here.
I didn’t try to copy this code letter for letter, rather I just used it as a guide as I’m new to flex, so it worked for me. However, what I really wanted to do was load images dynamically from a foreign rather than bundle them in with the app.
Here is how I did it
First I created a bindable ArrayCollection called images and set it as the data provider for my HorizontalList which looked like this:
Then I made a function to add an image to the list which looked like this:
This way is nice if youre lazy because you dont need to describe a custom ItemRenderer, and, as I said before, you dont need to bundle the images you want with the app
I have copied this code by the letter and only swapped out my own images. I have three images and my issue is that my first two images don’t “select” properly, until I select the third image. After selecting the third I can select either one or two correctly, but they don’t work in any other order.
@vicki,
Doesn’t sound like a known issue. If you think it is a bug, can you please file a bug report at http://bugs.adobe.com/flex/?
Thanks,
Peter
Hi – I had the same result as vicki. Copied the code, changed the array to include my own images/labels (just 4). Only the 4th image can be selected initially. After selecting the 4th i can select any of the other images. In other words, it appears to be necessary to select the last (4th) image in order to ‘reset’ the module so another image can be selected.
Mike, any luck yet? I’m a newbie and don’t feel confident sending it as a bug :) Not quite sure what to report.
how to display images from a database?
can you help me??
thx a lot.. ^^
Very usefull ! thank u!
I need one more thing to do with this picture gallery :D , and I don’t know how to do it..please help!
When u first laod this flex , I want that the first picture to be loaded as big size! Flex logo big size to be loaded at first step. It is kind of emty at first sight!
Thank U very much!
@Viorel,
Try setting the
selectedIndexproperty to 0 in the HorizontalList.Peter
Thank U very much!
It is working!
Nice tutorial :-)
I however have a question. How to proceed to autoload the first picture? I don’t find exemples. I suppose I have to use actionscript but I need a how-to.
It’s probably important I don’t use an array but a xml for pictures datas ;-)
Thank for anyone who can help.
@Xephres,
Set the
selectedIndexproperty on the HorizontalList control to 0 (zero).Peter
Wow, so easy?
Nice, it works ^ ^
Thanks :-)
Hi,
I am java developer and i need to develop a web page with image viewer content like your flex plug-in. However, i do not know how to applied it to my web page by JSP. Can anyone have experiences on this problems.
Thanks so much.
I have a question about the quality of the pictures. I don’t know if this is the right place to ask. No matter what picture size I use (4MB or 150KB) the result is the same: poor quality picture. Can I set a parameter for picture quality!
Thank U very much for this great tutorial!
@Viorel,
There may be a
smoothingproperty somewhere you could set. Although the best approach may be to have images which are sized to fit the Flex application. For examples, the thumbnails in the above example look pretty good because they are small and not distorted, but the large images look somewhat poor because they are being stretched and resized to fit in the application (because I couldn’t be bothered to resize them in Photoshop).Peter
Hi Peter,
How do i create a gallery of “Buttons”???
I have tried to use your idea to create a list of buttons in place of images. But it didnt work out. Plz let me know if there is anything els that i would need to do to create a gallery of buttons.
Thanks :)
@Ronnie,
Create a custom item renderer which is an mx:Button or whatever.
Peter
Thx Peter,
But thats exactly what i ve tried. Before i placed the buttons inside the list i could drag and drop them onto a canvas. But now i suppose i am not able to do that, since i have them as objects.
Just take out the name property inside the CustomItemRenderer.mxml component and it should work great!