TileListItemRenderer in Flex - actionscript-3

I followed an example to display a list and the code is as follows...
<mx:TileList id="tileList"
dataProvider="{xmlListColl}"
itemRenderer="TileListItemRenderer"
columnCount="3"
columnWidth="150"
rowCount="2"
rowHeight="100" />
The itemRenderer is:
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml"
x="496" y="10" width="221" height="317" backgroundColor="#98AEEF">
<mx:Image source="{data.#src}"
horizontalCenter="0"
verticalCenter="0" />
<mx:Label text="{data.#lbl}"
fontWeight="bold"
horizontalCenter="0"
bottom="0" />
</mx:Canvas>
Now I wanted help to have these images with drag enabled. All the dragDrop functions are within the main mxml file...I am a bit lost here. Any other information required please do ask me.
Thanks

You need to set dragEnabled to true on your TileList. If you're just rearranging within the TileList, then set its dragMoveEnabled and dropEnabled to true. If you want to drag it to another List, set dropEnabled true on that list, but if you want to copy from one list to another, see "Maintaining type information during a copy" here http://livedocs.adobe.com/flex/3/html/help.html?content=dragdrop_7.html.
If your destination is a container, check out http://livedocs.adobe.com/flex/3/html/help.html?content=dragdrop_7.html .
FWIW, I would have only needed to give you one of those pieces of information if you'd given more info about what you are trying to do. You're the one seeking help, so you should put forth at least as much effort as you're asking others to.

Related

Passing data between MXML files and opening one after Click on another

I am pretty new to flex and was trying to understand how the entire Flex works
Scenario :
I started with creating a single MXML file where there will be a textbox and a button. If the button is clicked a popup will be displayed with the value of the textbox(this worked perfectly). The second thing that came to my mind is how to show the same data in some other page. SO, if i click on the button. It should load another page and display the data that was written in the textbox
My Code till now is this :
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="horizontal">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
public function open(event:MouseEvent):void{
Alert.show("I am opening a page");
Alert.show(txt_inp.text + " is written here");
}
]]>
</mx:Script>
<mx:TextInput id="txt_inp" />
<mx:Button id="openBttn" label="Click Me" click="open(event)" />
</mx:Application>
And Name.xml(The Second page is )
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="setYelp()">
<mx:Script>
<![CDATA[
public var helloYou:HelloYou;
public function yelpHandler():void{
yelp.text = "Ayush"
}
public function setYelp():void {
helloYou.openBttn.addEventListener(MouseEvent.CLICK,yelpHandler);
}
]]>
</mx:Script>
<mx:Label id="yelp" text="yelp">
</mx:Label>
</mx:Application>
I referred to some of the previously asked questions like this and tried following it, but couldn't really understand.
Also, in my flex builder I had two more options of MXML > MXML Module and MXML Component. Should I use them here, if not, when should I use it?
Why are you using multiple page? usually a Flex application is a single application with multiple views and sharing data is way easier in this situation.
If you want to pass information between different pages you can use cookies (see for example Accessing browser cookies from Flex)
Another flex solution is this http://custardbelly.com/blog/blog-posts/2010/11/15/flex-hero-persistant-data-in-mobileapplication/
The last thing that I can think about is using the javascript bridge to access the localstorage (o sqlite) of your browser and store information there.
But again why save information between requests and not simply create a single application that changes its views ?
1st solution
Use ViewStack component to integrate your 1st mxml and 2nd mxml.
This is what #wezzy said "single application with multiple views".
This is the most easily way to share values, if you can integrate to one mxml.
like this
<mx:ViewStack id="viewstack" width="100%" height="100%" selectedIndex="0" >
<!-- Input Page is here -->
<mx:Canvas id="view1" width="100%" height="100%">
<mx:TextInput id="txtName" width="150" maxChars="40" />
<mx:Button label="Go to NextPage" click="viewstack.selectedChild=view2;"/>
</mx:Canvas>
<!-- Result Page -->
<mx:Canvas id="view2" width="100%" height="100%">
<mx:TextInput text="{txtName.text}" width="150" editable="false" />
<mx:Button label="Back to PrevPage" click="viewstack.selectedChild=view1;"/>
</mx:Canvas>
</mx:ViewStack>
2nd solution
Use SharedObject(or DB) to save and load values.
3rd solution
Use singleton variables to share the values between classes.
Please see this answer of mine.
Flex - Problems in accessing static variable on another mxml page

flashbuilder list component - how to add an image URL

Im working with flashbuilder 4.6 and have a simple app underway that uses a listcomponent pulling data from an XML service (external URL) that is setup with data/services functionality within flashbuilder.
right now I have a relatively simple list that just displays the 'name' of the xml items:
<s:List id="list1" x="0" y="108" width="320" height="355" change="list1_changeHandler(event)"
creationComplete="list1_creationCompleteHandler(event)" labelField="Itemname">
<s:AsyncListView list="{getDataResult.lastResult}"/>
</s:List>
... I have another xml node / data element that is a full URL (http:// ... .jpg) to a thumbnail image, I'd like to have that image aligned to the left of the 'name' element.
I tried using someting like the following, but no luck. the information I've found online hasnt really helped much with understanding itemrenderers (which is what I think I need to use).
<s:List id="list1" x="0" y="108" width="320" height="355" change="list1_changeHandler(event)"
creationComplete="list1_creationCompleteHandler(event)" labelField="Itemname">
<s:AsyncListView list="{getDataResult.lastResult}"/>
<mx:itemRenderer>
<mx:Component>
<mx:Label text="Itemname"/>
<mx:Image source="ItemImageURL"/>
</mx:Component>
</mx:itemRenderer>
</s:List>
is there some way to do this WITHOUT itemrenderers? or is that the only way to go? was my attempt close at all to the proper way to implement a thumbnail image in the list component? Thanks for any help someone can provide here.
The code you provided mixes Spark and MX Components in such a way that I would expect compiler errors. For starters, you have these issues:
The mx:itemRenderer is the syntax used to define a property on a component. So, it is used to define the value for the itemRender property on the s:List component. This will cause a compiler error because the namespaces are different. You should specify s:itemRenderer instead.
The mx:Component is used to define a new in-line component. However, your namespace is wrong. With Flex 4.6 you should specify the fx namespace, not the mx namespace.
There are two components listed as children to the mx:Component tag, where their should only be 1. The solution in the MX world to this would be to wrap it the label and image in a container. In the Spark world, the itemRenderer must extend the ItemRenderer class.
For the text and source you are specifying string literals; not variables in your data element.
You have the right idea; but the wrong execution. Read this information on creating itemRenderers.
I would rework your code like this:
<s:List id="list1" x="0" y="108" width="320" height="355" change="list1_changeHandler(event)"
creationComplete="list1_creationCompleteHandler(event)" >
<s:AsyncListView list="{getDataResult.lastResult}"/>
<s:itemRenderer>
<fx:Component>
<s:ItemRenderer dataChange="onDataChange()">
<fx:Script>
<![CDATA[
protected function onDataChange():void{
labelDisplay.text =data.Itemname
imageDisplay.source = data.ItemImageURL
}
]]>
</fx:Script>
<s:HGroup>
<s:Label id="labelDisplay" />
<s:Image id="imageDisplay" />
</s:HGroup>
</s:ItemRenderer>
</fx:Component>
</s:itemRenderer>
</s:List>
This is untested code I wrote the browser; but it should demonstrate the correct approach. The namespaces are correct. There is a single component defined as the itemRenderer and it extends the ItemRenderer class. The displayed items should update when the itemRenderer data changes, such as when you scroll in the list. And the two items are positioned so that they do not display on top of each other.

How to set DefaultComplexItemRenderer background alpha to 0

I have a simple list with a tileLayout of images. From the examples I've looked at it looks like i'm supposed to use the DefaultComplexItemRenderer. It works, and my array of images loads fine, but each item has a solid background. I need the background behind each image to have an alpha of 0. How Can I set that?
In the past i've just made a custom itemRenderer, and overrided the drawBackground function on itemRenderers, but DefaultComplexRenderer doesn't have a drawBackground function to override.
Is there another simple solution in the flex code? OR it would be awesome if someone could show me how to make a custom DefaultComplexRenderer.
NOTE:
This is a Flex MOBILE project. I know some itemRenderer's aren't friendly with mobile. Adobe says to "always do item renderers in AS3 rather than mxml" so, keep that in mind.
Thanks!
Here's my code:
<fx:Declarations>
<s:ArrayList id="arrList">
<s:BitmapImage source="assets/images/one.png" scaleMode="letterbox" smooth="true" width="100%" height="100%"/>
<s:BitmapImage source="assets/images/two.png" scaleMode="letterbox" smooth="true" width="100%" height="100%"/>
<s:BitmapImage source="assets/images/three.png" scaleMode="letterbox" smooth="true" width="100%" height="100%"/>
<s:BitmapImage source="assets/images/four.png" scaleMode="letterbox" smooth="true" width="100%" height="100%"/>
</s:ArrayList>
</fx:Declarations>
<s:List id="extrasList_list" width="100%" height="100%"
dataProvider="{arrList}"
itemRenderer="spark.skins.spark.DefaultComplexItemRenderer"
horizontalCenter="0"
verticalCenter="0">
<s:layout>
<s:TileLayout requestedColumnCount="-1"
requestedRowCount="-1"
horizontalGap="10"
verticalGap="10"
orientation="rows"
columnAlign="justifyUsingWidth"
/>
</s:layout>
</s:List>
Things I've tried:
this in my main css file...
s|DefaultComplexItemRenderer {
contentBackgroundAlpha:0;
}
but i get this warning, and it doesn't work...
-Cannot resolve namespace qualified type 'DefaultComplexItemRenderer' in CSS selector 'DefaultComplexItemRenderer'
[Edit]
The contentBackgroundAlpha and contentBackgroundColor styles for a Flex List are somewhat misleading! You generally want to style the item renderers. In the context of a list, these styles only affect a tiny bit of "chrome" that can appear around the list. See below for details on styling the renderers.
If you just want to show an image, the IconItemRenderer is the way to go. It extends LabelItemRenderer so it's optimized for mobile and also has two text fields as well.
Flex List components recycle item renderers to be efficient, and only create as many renderers as needed to display what is currently visible. To do this, the list populates the renderer's data property. So you want to configure your item renderer using this data property.
An efficient way to do that is to override the setter function for data. Create a new Actionscript class that extends IconItemRenderer add this to it:
override public function set data(value:Object):void
{
super.data = value;
// IconItemRenderer already has a BitmapImage component, it's property name is iconDisplay
// your ArrayList should therefore only contain Strings representing the image sources
// note how I've changed your ArrayList in the declarations tag below
iconDisplay.source = data.imageSource;
}
You'll likely want to configure that iconDisplay BitmapImage to look how you want it. The method above may get called frequently, so you can put code that only needs to happen once somewhere else... by overriding a Flex component lifecycle method like createChildren():
override protected function createChildren():void
{
super.createChildren();
iconDisplay.scaleMode="letterbox";
iconDisplay.smooth=true;
}
Now tell the List to use your renderer w/syntax like this:
<s:List itemRenderer="com.yourdomain.or.whatever.MyIconItemRendererClass" />
Styling the renderer:
<s:List alternatingItemColors="[0xFFFFFF, 0xFFFFFF]" selectionColor="#FF0000" />
Another way is to override the mobile item renderer's drawBackground() and/or drawBorder() protected methods and draw your own stuff w/the graphics api (or nothing at all).
Supplying the data:
Instead of giving the list an array of BitmapImage components, you give it an array of objects that contain your data. It's better to use strongly typed objects, but this works too:
<fx:Declarations>
<s:ArrayList id="arrList">
<fx:Object imageSource="assets/images/one.png" />
<fx:Object imageSource="assets/images/two.png" />
<fx:Object imageSource="assets/images/three.png" />
</s:ArrayList>
</fx:Declarations>
I like writing renderers in Actionscript... But there is also an MXML example in the link to IconItemRenderer docs at the top. It also shows how you to set the values of the two text areas in this renderer (with labelField and messageField). You can also specify a function that returns the label/message/icon values (with labelFunction, messageFunction, and iconFunction).
Looks like you should be able to use setStyle('contentBackgroundAlpha', 0); on your DefaultComplexItemRenderer.
Not 100% this is what you're looking for, not really familiar with this class. Any reason you're using this over extending LabelItemRenderer?
edit
I believe you may want to extend IconItemRenderer
Check out this tutorial
http://www.youtube.com/watch?v=EOpsDZaQrOI
Thanks for trying to help guys, but I found the quickest/simple solution to be be just as simple as copying the DefaultComplexItemRender from the SDK into a custom one, and then changing this one line of code
autoDrawBackground="false"
It's simple and it worked.

resizing list components upon dataProvider change

I have a component that contains a list that implements a custom renderer. I use this component as a tooltip, keeping one in memory and just altering the databindings as necessary.
Something like this where TTComponent is a class that extends Canvas:
<s:TTComponent>
<s:BorderContainer>
<s:List id='lstItems' dataProvider="{data.Items}" width="50%" borderVisible="false" contentBackgroundColor="#222222">
<s:layout>
<s:VerticalLayout useVirtualLayout="false" requestedMinRowCount="1"/>
</s:layout>
<s:itemRenderer>
<fx:Component>
<s:ItemRenderer>
<s:HGroup>
<s:Label text="{data.Value}" paddingLeft="6" />
<s:Label text="{data.Name}" />
</s:HGroup>
</s:ItemRenderer>
</fx:Component>
</s:itemRenderer>
</s:List>
</s:BorderContainer>
</s:TTComponent>
Now, the driver code looks like this for the mouse over:
if (tt == null) {
tt = new TTComponent();
}
tt.data = data;
PopUpManager.addPopUp(tt, this);
And, of course, the mouse out:
PopUpManager.removePopUp(tt);
Now, what happens is when data containing many items is set to tt.data, the list resizes itself larger and displays fine. However, after this, if data containing few items is set to tt.data, the component momentarily displays larger than it needs to be, then resizes itself smaller.
What I'd like to do is have the component resize itself before it displays, so I don't see the resize on screen. Any idea on how to accomplish this?
First, I would compel you to consider creating the component once and just showing / hiding it / moving it when needed rather than using pop up manager. That's how I would typically do something similar to what you're attempting. It will perform better and probably fix this issue on its own. Just a thought.
Secondly, after you rebind new data to it, try these :
Tell it needs to resize :
tt.invalidateDisplayList();
Also, you might need to tell it you changed the data by refreshing the data if an ArrayCollection:
data.refresh();
Let me know if that doesn't do the trick.

Accessing itemRenderer instance flash buiilder 4

My setup is pretty basic. I have an s:List with a custom itemRenderer and a dataprovider. What I would like to do is access the generated instances of the item renderer but I have no idea how.
Here is the code for the list:
<s:List id="layersList"
borderVisible="false"
allowMultipleSelection="true"
contentBackgroundAlpha="0"
itemRenderer="renderers.LayerRenderer"
dataProvider="{AssetsCollection}">
<s:layout>
<s:VerticalLayout gap="1" />
</s:layout>
<s:list>
What I would like is to access the generated renderers like:
layersList.renderers[selectedIndex] or layersList.selectedItems[0].renderer. In order to access some of its internal objects. Like in the event I would want to listen events dispatched in the renderer instance from the List's parent.
Can anyone help?
the conceptual model of Lists/ItemRenderers is that they are a representation of the items in the dataProvider. One reason for keeping this in mind is that Lists recycle their ItemRenderers in order to reduce memory footprint. This means you may have 100 items in your dataProvider, but only a small subset of those will have ItemRenderers associated with them, and some of those may not even be visible on screen or even valid any longer. There are a few ways you could approach having your ItemRenderers in your List reflect the state of the List's parent without having to directly manipulate the renderers. For instance, you could do something like this:
<s:List id="layersList"
borderVisible="false"
allowMultipleSelection="true"
dataProvider="{AssetsCollection}"
contentBackgroundAlpha="0">
<s:layout>
<s:VerticalLayout gap="1" />
</s:layout>
<s:itemRenderer>
<fx:Component>
<myrenderers:TestRenderer myState="{outerDocument.someState}"/>
</fx:Component>
</s:itemRenderer>
</s:List>
Where TestRenderer has a bindable public property called myState. And the List's parent has a bindable property called "someState". Then inside your renderer you can set some conditional logic based on the value of myState. Hope that helps.