Flex Examples
Setting the horizontal scroll position in the HorizontalList control in Flex
The following example shows how you can scroll the Flex HorizontalList control by setting the horizontalScrollPosition property.
Full code after the jump.
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/04/03/scrolling-the-horizontallist-control-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
verticalAlign="middle"
backgroundColor="white">
<mx:Script>
<![CDATA[
import mx.events.SliderEvent;
private function prev():void {
var pos:int = hList.horizontalScrollPosition-1;
var min:int = 0;
var value:int = Math.max(min, pos);
hList.horizontalScrollPosition = value;
}
private function next():void {
var pos:int = hList.horizontalScrollPosition+1;
var max:int = hList.maxHorizontalScrollPosition;
var value:int = Math.min(pos, max);
hList.horizontalScrollPosition = value;
}
private function slider_change(evt:SliderEvent):void {
hList.horizontalScrollPosition = evt.value;
}
]]>
</mx:Script>
<mx:Array id="arr">
<mx:Object lbl="Illustrator" src="ai_appicon-tn.gif" />
<mx:Object lbl="Adobe AIR" src="air_appicon-tn.gif" />
<mx:Object lbl="ColdFusion" src="cf_appicon-tn.gif" />
<mx:Object lbl="Dreamweaver" src="dw_appicon-tn.gif" />
<mx:Object lbl="Flash" src="fl_appicon-tn.gif" />
<mx:Object lbl="Flash Player" src="fl_player_appicon-tn.gif" />
<mx:Object lbl="Fireworks" src="fw_appicon-tn.gif" />
<mx:Object lbl="Flex" src="fx_appicon-tn.gif" />
<mx:Object lbl="Lightroom" src="lr_appicon-tn.gif" />
<mx:Object lbl="Photoshop" src="ps_appicon-tn.gif" />
</mx:Array>
<mx:Panel styleName="opaquePanel">
<mx:HorizontalList id="hList"
dataProvider="{arr}"
itemRenderer="HorizontalListItemRenderer"
verticalScrollPolicy="off"
horizontalScrollPolicy="off"
columnWidth="100"
columnCount="4"
rowHeight="100"
rowCount="1"
borderSkin="{null}" />
<mx:ControlBar>
<mx:Button label="Previous" click="prev();" />
<mx:HSlider id="slider"
minimum="0"
maximum="{hList.maxHorizontalScrollPosition}"
value="{hList.horizontalScrollPosition}"
liveDragging="true"
snapInterval="1"
tickInterval="1"
showDataTip="false"
width="100%"
change="slider_change(event);" />
<mx:Button label="Next" click="next();" />
</mx:ControlBar>
</mx:Panel>
</mx:Application>
View HorizontalListItemRenderer.mxml
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/04/03/scrolling-the-horizontallist-control-in-flex/ -->
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml"
styleName="plain">
<mx:Image source="{data.src}"
horizontalCenter="0"
verticalCenter="0" />
<mx:Label text="{data.lbl}"
fontWeight="bold"
horizontalCenter="0"
bottom="0" />
</mx:Canvas>
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.
18 Responses to Setting the horizontal scroll position in the HorizontalList control in Flex
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


Again, I love this site.. so great that you post this kind of simple yet undergrated thins.
Hi! Thanks for providing that code…its really useful. Waiting for your next post.
Awesome site and great post. Helped me greatly. Anyone have an idea on how to apply effects as you transition between list items?
Hello, Thanks for the great info on your site.
Question on this example: Is this something that is easily set to use the tween class to slide from image to image, instead of just jumping?
Great example, it really helped me extend my gallery. Is there a way to add effects to transition between items on a HorizontalList?
A very cool example! Is it possible to have the sliding effect within the horizontal list instead of using the slider and buttons?
Thanks=)
I agree with Raul! This is one of the greatest reference for new Flex programmers.
As usual, another great sample!
Thanks Peter :)
Hi,
maybe this is the wrong topic to place my post but I couldn’t find anything similar to my prob.
I’m just starting with Flex Builder 3 and after reading a couple of books, I’m trying to build my own RIA. Everything was wonderful and I was happy with the results until I got stuck with a Canvas problem. I got a canvas(A) within another canvas(B) which B can be scaled using a Slider. It worked fine but the thing is, to center the scale of B relative to A it making me mad. I’ve been searching the net for a week and I couldn’t find anything to help me figure it out. My code works but when I try to slide from 200% to 10%, to position from B gets all mess up.
My code: A = _wrap / B = _doc
/* ———————– */
private function alignDoc():void {
if(_doc.width > _wrap.width){
_doc.x = 30;
_wrap.horizontalScrollBar.setStyle(“thumbWidth”, 100);
_wrap.horizontalScrollPosition = _wrap.horizontalScrollBar.width / 2;
}
if(_doc.height > _wrap.height){
_doc.y = 30;
_wrap.verticalScrollPosition = _wrap.verticalScrollBar.height / 2;
}
if(_doc.width
Thank you for sharing! it works :)
Hi Peter,
Thanks a lot for sharing this..
But I need to add the events for the Itemrenderer.
For example, when I click any Item in the HorizontalList, I have to display the “Name of the particular picture”.
Please tell me .. How to do this ???
nathan,
What about something like this:
<mx:Label text="{hList.selectedItem.lbl}" />Peter
Hey… Thanks a lot Peter.. It works fine…
Peter.. One more thing need to know…
Could you please post any example for Event Dispatch from Itemrenderer ???
Where the Itemrenderer for any List based control… Please…
hey frnds,
could we add some move effect to horizontal list?
abhishekchess1@gmail.com
:)
:)
Hi,
I am trying this code with Flex 3, but it is giving error on using the
“Error: Component declaration are not allowed here”. Can you please help me with this??
Thanks
@Payel,
Are you using the code from above, or are you using this in a different project?
Peter
You just saved my life with this post! Thank you so much!!
Add a tween if you like:
var pos:int =hList.horizontalScrollPosition-1;
var min:int = 0;
var value:int = Math.max(min, pos);
TweenLite.to(hList, 0.5,{horizontalScrollPosition:value});
Hi! Thanks for providing that codeā¦its really useful. Waiting for your next post.