The item render gives us the ability to create custom views of our list items. As per the Adobe documentation, there are several different methods of doing this:
In this article we go straight for item number 3 - An In-Line Item Renderer. This method gives us enough flexibility to perform all the actions we need in a single file (item number 4 is also a good solution. I provide the code at the end of the article). You can further extend this method and create and/or use an external component to perform the same task. We'll keep it simple for now. Let's talk for a minute about what we want this list to look like. For starters, let's suppose that we will use this list for selection. Let's say that users will be able to select which fruit they would like to see based on this list. That indicates that we will use checkboxes. Our item renderer, then, will be responsible for displaying a checkbox for each item in the list. The first thing we will do is add an <mx:itemRenderer> tag followed by an <mx:Component> tag. The <itemRenderer> tag tells Flex that we will be overriding the default renderer and the nested <mx:Component> defines how we will be overriding it. Inside of our <mx:Component> tag, we will add our <mx:Checkbox> and any other control we require to appear per list item. In this case we also need a <mx:Label> to display the name of the fruit. Here is what the updated application looks like: Here is the updated code: <?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx=http://www.adobe.com/2006/mxml That is all there is to making a basic item renderer, but there is one thing I should mention. You probably noticed a few warnings in your Flex Builder console. The ones I have look like this: warning: unable to bind to property 'label' on class 'XML' (class is not an IEventDispatcher)We are getting these warnings because we are trying to bind the @label of the XMLList with the mx:Label (in our component). The problem here is that the XMLList does not implement the IEventDispatcher Interface. So, when data in the XMLList changes, the List will not reflect the changes. Flex is warning us because this may be a problem in some applications. There are a few ways to fix this, but a lesson on bindings is first in order. These warnings will not present a problem when the application is compiled and released. For more information on bindings see http://www.adobe.com/devnet/flex/quickstart/using_data_binding/#section3
blog comments powered by Disqus |