spark List with ItemRenderer click function not working - apache-flex

I am having an issue with my ItemRenderer, which I am using for a spark List. My code is the following:
I have this list:
<s:List
id="productSetList"
dataProvider="{ model.productSets }"
change="model.selectSet( productSetList )"
height="100%" width="100%"
borderVisible="false"
itemRenderer="SideBarItemRenderer" top="20" left="15">
</s:List>
and my itemRenderer is:
<s:ItemRenderer
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/halo"
width="160" height="175" autoDrawBackground="false" buttonMode="true" useHandCursor="true"
click="click(event)" cacheAsBitmap="true"
>
<fx:Script>
<![CDATA[
import com.png.vm.model.vos.ProductSet;
protected function click(event:MouseEvent):void
{
trace('arthur');
}
]]>
</fx:Script>
<s:BitmapImage source="{ProductSet(data).image}" width="160" height="175"/>
</s:ItemRenderer>
The thing is, if I scroll the list, and click on an item, it does not trace 'arthur' ! Why is this so ? I must trace that all the time that someone clicks in the list!
EDIT:
If I remove change="model.selectSet( productSetList )" in the list, it works!! but I cannot remove that, some suggestions ? How can I switch that to another function?

Trace only works when you are debugging/using the debugging version of Flash Player. Make sure you are using that. If you want a pop-up message use Alert.show("message")
For more information about trace() check out:
http://livedocs.adobe.com/flex/3/html/help.html?content=logging_08.html
And Alert.show(): http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00001965.html
If you are running debug player. Try originating the click event from an embedded <s:Group> this way whatever you add in here beyond the bitmap will still trigger the click event.
Something like:
<s:Group width="100%" height="100%" click="click(event)" >
<s:BitmapImage source="{ProductSet(data).image}" width="160" height="175"/>
</s:Group>
I've definitely had click events work for me inside of ItemRenderers before

My apologies, I have finally solved it. The problem was that inside the function,model.selectSet, I was calling List.change; I was messing the list up! My function was as follows:
public function selectSet(list:List):void {
list.layout.verticalScrollPosition=100;
// another stuffs
}
So, I just removed the line : list.layout.verticalScrollPosition=100; and now it`s working fine.
Thanks for all the help!

Related

Switch between Flex Tabbed ViewNavigators

I am working on a Flex TabbedViewNavigatorApplication with three tabs (ViewNavigator elements). I would like to switch from one ViewNavigator to another based upon a user action (via ActionScript code).
I know that switching between Views uses pushView and popView, but I'm working with ViewNavigators, and my searching revealed nothing useful.
I'm trying to switch from Tab2 to Tab1 when an event occurs. In this case, Tab2 contains a list, and when the user makes a selection, I want to jump back to Tab1.
<s:TabbedViewNavigatorApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
creationComplete="onAppReady(event)">
<s:ViewNavigator label="Tab1" width="100%" height="100%" firstView="views.TabOneView"/>
<s:ViewNavigator label="Tab2" width="100%" height="100%" firstView="views.TabTwoView"/>
<s:ViewNavigator label="Tab3" width="100%" height="100%" firstView="views.TabThreeView"/>
</s:TabbedViewNavigatorApplication>
Thanks for your help!
I use the following line of ActionScript to switch from one ViewNavigator to another based on a user action:
TabbedViewNavigator(navigator.parentNavigator).selectedIndex = 1;
It worked like a charm and seems simpler than bubbling events.
This class is strangely undocumented. I have not tried this myself, but from searching online, this is what I found which corroborates with what the rest of the network does.
What you need to do is bubble an event to the TabbedViewNavigatorApplication and from there change the selectedIndex property to whichever tab you need to change to. For example:
<s:TabbedViewNavigatorApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
creationComplete="onCreationComplete()">
<fx:Script>
<![CDATA[
private function onCreationComplete():void
{
this.addEventListener('someEvent', someHandler);
}
private function someHandler(e:Event):void
{
this.selectedIndex = 0; // or whatever index you want.
}
]]>
</fx:Script>
<s:ViewNavigator label="Tab1" width="100%" height="100%" firstView="views.TabOneView"/>
<s:ViewNavigator label="Tab2" width="100%" height="100%" firstView="views.TabTwoView"/>
<s:ViewNavigator label="Tab3" width="100%" height="100%" firstView="views.TabThreeView"/>
</s:TabbedViewNavigatorApplication>
You just need to dispatch a bubbling event from within your children. You could event create a custom event that holds data about which tab to switch to.

Dragging the AIR application window around the screen

I have an AIR application. It should be moved around the screen with the mouse. In order to achieve this I use the event:
this.stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown, true,-2);
It should be activated with the lowest priority compared to inserted elements for example those that should be scrolled, clicked, etc.
I tried the solution shown below with the event priority set to -1 because there might happen 2 different events and my moving application event should be the last one to be serviced or shouldn't be serviced at all.
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
width="200"
height="200"
applicationComplete="init()">
<fx:Script>
<![CDATA[
import mx.core.Window;
import mx.events.ScrollEvent;
private function init():void {
this.stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown, true,-2);
}
private function onMouseDown(event:MouseEvent):void {
trace("clicked on stage "+event.currentTarget.toString());
if(event.currentTarget == stage){
trace("catched stage target");
this.nativeWindow.startMove();
event.stopImmediatePropagation();
}
}
function scrolledCanvasHandler(event:ScrollEvent){
trace("clicked on canvas "+event.currentTarget.toString());
event.stopPropagation();
}
]]>
</fx:Script>
<mx:Canvas x="29" y="34" width="80%" height="80%" backgroundColor="#343434" scroll="scrolledCanvasHandler(event)">
<mx:Label x="25" y="77" text="moving window, moving window"
fontSize="18" color="#FFFFFF" fontWeight="bold"/>
</mx:Canvas>
</s:WindowedApplication>
As you will notice the
event.stopPropagation();
doesn't work.
Perhaps my solution isn't the best suited to achieve this. Are there better solutions?
Chris
that's what i did in an app of mine:
<s:HGroup id="appTitleBar"
width="100%" height="35"
styleName="titleBar"
mouseDown="nativeWindow.startMove();"
doubleClickEnabled="true"
doubleClick="nativeWindow.minimize();"
contentBackgroundColor="#313131"/>
click (+drag) on this HGroup will drag the window. duobleclick will minimize it.
edit
don't make your whole app draggable this will only confuse the user.
and btw priority should be positive not negative - but also don't mess with this. not expected behavior for anyone.

Flex IFrame Component not rendering outside viewable area

Beginner Question:
I am using the IFrame Component for Flex in Flex 4. The code below works when it is put at the top of the scrolling area. However, if I put it where it below the viewable area it will not render. I am a complete beginner to Flex. The interesting thing is when I resize the window while the HBox is in view, the Iframe will load. But scrolling to it will not. Below is my code. I have made sure everything is visible=true but it seems like I need to add a listener or somehow trick it to think that the window has been resized to get it to render. Anyone with an idea how to fix this? Thanks!
<mx:HBox visible="true" backgroundColor="#cccccc" id="facebookhbox" width="100%" height="100" horizontalAlign="center" verticalAlign="middle" verticalGap="0" verticalScrollPolicy="off">
<mx:Canvas id="facebookcanvas" visible="true" x="0" y="0" width="650" height="80">
<flexiframe:IFrame visible="true" y="0" x="0" id="facebookIFrame" source="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.examplelink.com&layout=standard&show_faces=true&width=450&action=like&colorscheme=light&height=80" width="450" height="80"/>
</mx:Canvas>
</mx:HBox>
No experience with the Flex IFrame here just FYI but in generic terms you can invalidate the size of the component or invalidate the display list then call validate now in order to force the LayoutManager to recalculate the sizes of the element and it's children (or simply draw in the case of invalidating the display list):
The difficult part I see here is figuring out when exactly you want that to happen (that is capturing some sort of event from the scrollbar). For test purposes you can just create a button and in it's click handler do the following:
facebookIFrame.invalidateSize();
facebookIFrame.invalidateDisplayList();
facebookIFrame.validateNow();
If this works out then you just need to find the appropriate event from the Scroller or Canvas that tells you when scrolling is occuring (at worst you could capture mouse down mark a flag then capture mouseMove and if the flag is set then run the code to invalidate/re-validate). Basically how the layout/drawing works is that the UIComponents have flags that let it know when it needs to recalculate the size of something or redraw it, this way everything isn't always redrawn only those components that require it. In this case it seems the FacebookIFrame isn't getting invalidated at some point when it should. Let me know if I can help anymore or if this doesn't work out.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
height="100%"
width="100%"
xmlns:code="http://code.google.com/p/flex-iframe/"
mouseDown="application1_mouseDownHandler(event)"
mouseUp="application1_mouseUpHandler(event)"
mouseMove="application1_mouseMoveHandler(event)">
<mx:Script>
<![CDATA[
var isMouseDown:Boolean;
protected function button1_clickHandler(event:MouseEvent):void
{
// TODO Auto-generated method stub
facebookIFrame.invalidateSize();
facebookIFrame.invalidateDisplayList();
facebookIFrame.validateNow();
}
protected function application1_mouseDownHandler(event:MouseEvent):void
{
isMouseDown = true;
}
protected function application1_mouseUpHandler(event:MouseEvent):void
{
isMouseDown = false;
}
protected function application1_mouseMoveHandler(event:MouseEvent):void
{
if(isMouseDown)
{
facebookIFrame.invalidateSize();
facebookIFrame.invalidateDisplayList();
facebookIFrame.validateNow();
}
}
]]>
</mx:Script>
<mx:Spacer height="1000"/>
<mx:Button label="Test Invalidation"
click="button1_clickHandler(event)"/>
<mx:HBox visible="true" backgroundColor="#cccccc" id="facebookhbox" width="100%" height="80" horizontalAlign="center" verticalAlign="middle" verticalGap="0" verticalScrollPolicy="off">
<mx:Canvas id="facebookcanvas" visible="true" x="0" y="0" width="650" height="80">
<code:IFrame visible="true" y="0" x="0" id="facebookIFrame" source="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.examplelink.com&layout=standard&show_faces=true&width=450&action=like&colorscheme=light&height=80" width="450" height="80"/>
</mx:Canvas>
</mx:HBox>
</mx:Application>
Updated to include a full application showing the example working, this is by no means a good way of doing things, this is due to a bug in the IFrame code that should be fixed (though I'm not sure how or where the bug exists something should be causing it to invalidate appropriately where it is not).
Shaun

Component doesn't get garbage collected

I just noticed a strange behaviour while looking at my application in the Flash Profiler. When I click a button in my TitleWindow then the TitleWindow doesn't get garbage collected after it is removed. I have no idea why that is happening.
I've created a small example application, so you can try it out yourself:
Main.mxml
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" pageTitle="Memory Leak (Spark)">
<fx:Script>
<![CDATA[
protected function openWindowBtn_clickHandler():void
{
removeAllElements();
addElement(new ExampleView());
}
]]>
</fx:Script>
<s:controlBarContent>
<s:Button label="Open Window" id="openWindowBtn" click="openWindowBtn_clickHandler()"/>
</s:controlBarContent>
</s:Application>
ExampleView.mxml
<?xml version="1.0" encoding="utf-8"?>
<s:TitleWindow xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" width="100%" height="100%" title="Example View" close="closeHandler()">
<fx:Script>
<![CDATA[
import mx.core.IVisualElementContainer;
protected function closeHandler():void
{
var visualElementParent:IVisualElementContainer = parent as IVisualElementContainer;
if (visualElementParent)
visualElementParent.removeElement(this);
else
parent.removeChild(this);
}
]]>
</fx:Script>
<s:layout>
<s:VerticalLayout verticalAlign="middle" horizontalAlign="center"/>
</s:layout>
<s:Button id="doSomethingBtn" label="Click me!"/>
</s:TitleWindow>
When you click "Open Window" and close the ExampleView without clicking the "Click me!" button in it then the GC kicks in and removes the ExampleView. However, when you click on "Click me!" and close the ExampleView afterwards, the ExampleView stays in memory forever.
I wasn't able to find the references in the Profiler which cause this behaviour. I hope someone knows a solution to this, otherwise Flex is creating a lot of memory leaks.
I may be wrong, but iirc EventListeners added in MXML are always created with a strong reference, which would prevent the Button from being GC'ed.
Have you tried adding the EventListener manually with setting it to being a weak reference? If you look at the list of EventListeners in the Debugger you should see something like a WeakMethodClosure if it was added with a weak reference.
One thing you're probably forgetting is that garbage collection isn't collecting unreferenced objects at the moment they loose the last reference. Usually the GC will collect the loose instances only after you create some object, but even than it's not obvious if it will do it in that moment. You can read more about it here:
About garbage collection
Or take a look at this presentation: Garbage Collection - Alex Harui
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" pageTitle="Memory Leak (Spark)">
<fx:Script>
<![CDATA[
protected function openWindowBtn_clickHandler():void
{
removeAllElements();
addElement(new ExampleView());
}
protected function button1_clickHandler(event:MouseEvent):void
{
var o:Object = new Object();
System.gc();
}
]]>
</fx:Script>
<s:controlBarContent>
<s:Button label="Open Window" id="openWindowBtn" click="openWindowBtn_clickHandler()"/>
<s:Button label="Force GC" click="button1_clickHandler(event)"/>
</s:controlBarContent>
</s:Application>
Take a look at this. If you press the "Force GC" button a couple of times, it will collect the ExampleWindow. In a real world application that does something this happens without the need to call the System.gc() (in fact, it's not a good practice to call it), but after a while, so the things don't just disapear when you're done with them, they disapear when you're done, and Flash Player decides it needs to clean up.
Looks like the ExampleView is not getting Garbage collected because somehow some EventListener is added when "Click Me" is clicked.
The best way to avoid this is to
1. add Event Listener manually in createComplete event
2. Remove the EventListener in closeHandler
3. Remove the button from the container and set it to null
Now the ExampleView will be Garbage collcected

Flex: Method doesn't work when being called on parentDocument

I wonder is anyone can look at this code and tell me why calling the removeSelectedChild works when called from the same document, but returns the following error when called from the child document/component.
"ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller."
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" >
<mx:Accordion id="myAccordion"
width="100%" height="100%"
selectedIndex="0">
<mx:Script>
<![CDATA[
public function removeSelectedChild():void {
trace("before");
try {
myAccordion.selectedChild.parent.removeChild(myAccordion.selectedChild)
} catch(err:ReferenceError) {
trace("catch")
}
trace("after");
}
]]>
</mx:Script>
<mx:headerRenderer>
<mx:Component>
<mx:Button click="{ parentDocument.removeSelectedChild() }" />
</mx:Component>
</mx:headerRenderer>
<mx:HBox>
<mx:Button click="{ removeSelectedChild() }" />
</mx:HBox>
</mx:Accordion>
</mx:Application>
Clicking on the button in the child produces the expected result, whilst clicking on the header removes the child as expected but also throws an error despite the fact they both call exactly the same method.
Sorry that the example is a little contrived, this problem arose in a quite complicated view, which was using all kinds of custom components. This was the only way I could display it in a way that will be quick for you to compile and easy to focus on the real issue without background noise.
I'm pulling my hair out on this one and would really appreciate it if anyone could help.
UPDATE: I have now discovered that the exception is fired after the method has finished executing. See the trace statements above. "after" is traced before the exception is thrown.
Cheers,
Chris
As you noted, the error is not being thrown by your function. The default action when clicking on the header of an accordion is to expand or contract that element. Since you're removing the element, when Flex tries to expand or contract it, you get an error.
You can fix it by stopping the event from doing its default action:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" >
<mx:Accordion id="myAccordion"
width="100%" height="100%"
selectedIndex="0">
<mx:Script>
<![CDATA[
public function removeSelectedChild(event:MouseEvent):void {
myAccordion.removeChild(myAccordion.selectedChild);
event.stopImmediatePropagation();
}
]]>
</mx:Script>
<mx:headerRenderer>
<mx:Component>
<mx:Button click="{ parentDocument.removeSelectedChild(event) }" />
</mx:Component>
</mx:headerRenderer>
<mx:HBox>
<mx:Button click="{ removeSelectedChild(event) }" />
</mx:HBox>
</mx:Accordion>
</mx:Application>
Not a solution, but perhaps a workaround: could you try rewriting the method like this:
myAccordion.selectedChild.parent.removeChild(myAccordion.selectedChild)
if that doesn't work there must be a bug somewhere in Flex.
Use "outerDocument" instead of "parentDocument". defines a new mini-scope.

Resources