In Flex, project Renderer is an important rendering method.
Question 1: How to click an item in an application to obtain the value of this item
As shown below, when you click "video" item, the word "video" is displayed.
Two solutions
Method 1:
Distribute events in the click events of the custom itemrenderer component
Dispatchevent (new event ("itemclick", true ));
Pay attention to the following "true", which indicates that the event bubble is used. This is very important !!
In the application file that calls the custom itemrenderer component, add a listener event
Addeventlistener ("itemclick", onitemclick );
Next, define the onitemclick method.
Private function onitemclick (Event: myevent): void
{
Alert.show(event.tar get as itemrenderer).data.txt );
}
If there is no such thing in the Custom itemrenderergroup, The event.tar get type will not be itemrenderer.
Method 2:
Custom Event myevent
Package events
{
Import flash. Events. event;
Public class myevent extends event
{
Private VaR _ DATA: object;
Public Function myevent (type: String, data: Object = NULL, bubbles: Boolean = false, cancelable: Boolean = false)
{
Super (type, Bubbles, cancelable );
This. _ DATA = data;
}
Public Function get data (): Object
{
Return this. _ data;
}
Public Function set data (data: Object): void
{
This. _ DATA = data;
}
}
}
Distribute events in the Click Event of the custom itemrenderer component
Dispatchevent (New myevent ("itemclick", data.txt, true ));
Add the data.txt data to the event parameters for distribution.
Of course, in this custom itemrenderer component file, you must declare that itemclick is an event.
<FX: Metadata>
[Event (name = "itemclick", type = "events. myevent")]
</FX: Metadata>
In the application file that calls the custom itemrenderer component, add a listener event
Addeventlistener ("itemclick", onitemclick );
Next, define the onitemclick method.
Private function onitemclick (Event: myevent): void
{
Alert. Show (event. Data as string );
}
OK!
Question 2: How to dynamically modify the value of an item
The implementation is as follows.
In this example, arraylist is used as the data source binding method, and the arraylist is composed of objects.
[Bindable]
Private var arr: arraylist = new arraylist ();
Arr. additem ({SRC: 'Assets/images/f_menu_34.png ', TXT: 'News '});
Arr. additem ({SRC: 'Assets/images/f_menu_36.png ', TXT: 'webpage '});
Arr. additem ({SRC: 'Assets/images/f_menu_38.png ', TXT: 'Post bar '});
Arr. additem ({SRC: 'Assets/images/f_menu_40.png ', TXT: ''});
Arr. additem ({SRC: 'Assets/images/f_menu_42.png ', TXT: 'mp3 '});
Arr. additem ({SRC: 'Assets/images/f_menu_44.png ', TXT: 'image '});
Arr. additem ({SRC: 'Assets/images/f_menu_46.png ', TXT: 'video '});
Arr. additem ({SRC: 'Assets/images/f_menu_48.png ', TXT: 'map '});
Some warnings will be issued during compilation.
Generally, objects are not of the ieventdispatcher type and cannot be bound to the SRC and TXT attributes.
That is to say, when we dynamically change the values of SRC and txt, the system will not update them automatically.
Is it swollen?
Two solutions
Method 1:
Use objectproxy to proxy object
Arr. additem (New objectproxy ({SRC: 'Assets/images/f_menu_34.png ', TXT: 'News '}));
Arr. additem (New objectproxy ({SRC: 'Assets/images/f_menu_36.png ', TXT: 'page '}));
Arr. additem (New objectproxy ({SRC: 'Assets/images/f_menu_38.png ', TXT: 'puba '}));
Arr. additem (New objectproxy ({SRC: 'Assets/images/f_menu_40.png ', TXT: ''}));
Arr. additem (New objectproxy ({SRC: 'Assets/images/f_menu_42.png ', TXT: 'mp3 '}));
Arr. additem (New objectproxy ({SRC: 'Assets/images/f_menu_44.png ', TXT: 'image '}));
Arr. additem (New objectproxy ({SRC: 'Assets/images/f_menu_46.png ', TXT: 'video '}));
Arr. additem (New objectproxy ({SRC: 'Assets/images/f_menu_48.png ', TXT: 'map '}));
Modify the item value in the Click Event of the button.
Private function changevalue (Event: mouseevent): void
{
Arr.getitemat(int(textinput.text=0000.txt = textinput2.text;
}
Method 2:
Since the object is not of the ieventdispatcher type, we will construct a class custommenu that inherits the ieventdispatcher type.
Package
{
Import flash. Events. eventdispatcher;
[Bindable]
Public class custommenu extends eventdispatcher
{
Public var SRC: string;
Public var TXT: string;
}
}
Construct a data source
[Bindable]
Private var arr: arraylist = new arraylist ();
VaR CM: custommenu = new custommenu ();
Cm. src = 'Assets/images/f_menu_34.png ';
Cm.txt = 'News ';
Arr. additem (CM );
Cm = new custommenu ();
Cm. src = 'Assets/images/f_menu_36.png ';
Cm.txt = 'website ';
Arr. additem (CM );
Cm = new custommenu ();
Cm. src = 'Assets/images/f_menu_38.png ';
Cm.txt = 'paste bar ';
Arr. additem (CM );
Cm = new custommenu ();
Cm. src = 'Assets/images/f_menu_40.png ';
Cm.txt = 'cognition ';
Arr. additem (CM );
Cm = new custommenu ();
Cm. src = 'Assets/images/f_menu_42.png ';
Cm.txt = 'mp3 ';
Arr. additem (CM );
Cm = new custommenu ();
Cm. src = 'Assets/images/f_menu_44.png ';
Cm.txt = 'image ';
Arr. additem (CM );
Cm = new custommenu ();
Cm. src = 'Assets/images/f_menu_46.png ';
Cm.txt = 'video ';
Arr. additem (CM );
Cm = new custommenu ();
Cm. src = 'Assets/images/f_menu_48.png ';
Cm.txt = 'map ';
Arr. additem (CM );
You can also modify the item value in the button click event.
Private function changevalue (Event: mouseevent): void
{
Arr.getitemat(int(textinput.text=0000.txt = textinput2.text;
}
Get it done, go to bed