A few days ago, I encountered a problem when writing ViSR, and the problem is as follows:
1. Define a visual component of HorizontalList type: MyHorizontalList.
2. Properties of MyHorizontalList: dataProvider. It binds an external data. (Suppose there are 6 external data in total)
3. The property of MyHorizontalList: itemRenderer, which uses a custom component: MyItemRenderer
4. A public type functon is defined in MyItemRenderer: refresh()
5. When the program is initialized, it is well known that if you want to assign values to the items in the MyItemRenderer, the form to be used should be used. At the same time, 6 MyItemRenderers should be established, and they are all presented by the HorizontalList renderer itemRenderer.
Everything went smoothly in the above situation, right? :)
The problem arises: When the Flex App is running, I want to dynamically call public funciton: refresh() in MyItemRenderer contained in MyHorizontalList, that is, I need to call all refresh in MyItemRenderer. This call does not require any event (such as itemClick event).
How to explore:
1. Go through the child in MyHorizontalList and find that there are only three.
2. Automatic traversal triggers itemClick event, which can be achieved, but efficiency is a problem (when building a large Flex App, you must pay attention to the issue of efficiency.)
I had no choice but to search and answer the questions, and finally I found the solution to this problem in combination with Adobe Doc.
Here is the idea to look for:
HorizontalList is inherited from ListBase, and ListBase belongs to the class below the package, so the most promising thing is below this
Implementation method:
1. When using the first method in the exploration stage, you will get that there are three children in MyHorizontalList, and we need its last child.
That is: (2)
2. Please be sure to note: the last child is a variable of type ListBaseContentHolder, and ListBaseContentHolder exists in it.
Therefore, we need to manually introduce the following class:
import ;
var myHList : ListBaseContentHolder = ( 2 ) as ListBaseContentHolder;
Through the above code, I get myHList. (Note that its type is: ListBaseContentHolder )
3. There is a public method: listItems in ListBaseContentHolder, and its definition is as follows:
Original text: An Array of Arrays that contains the item renderer instances that render each data provider item.
Translation: An Array consisting of an array containing an instance of the project renderer provided by dataProvider.
It is not difficult to see from the above translation that listItems is the method we want to use, that is, an array containing MyItemRenderer.
4. The following source is relatively simple:
//Get MyRendererArr
var MyRendererArr : Array = [ 0 ] as Array ;
//Transfer MyRendererArr
for ( var i : int = 0; i < MyRendererArr .length; i ++ ) {
//Each MyRendererArr[i] is an instance of MyItemRenderer
var temp : MyItemRenderer= MyRendererArr [ i ] as MyItemRenderer;
//The final result I want
();
}
Isn't it very simple? In fact, the above method applies to any component that inherits from ListBase.
About ListBase:
It is the base class that provides a list of items, i.e. it is the base class of itemRenderer.
1. Define a visual component of HorizontalList type: MyHorizontalList.
2. Properties of MyHorizontalList: dataProvider. It binds an external data. (Suppose there are 6 external data in total)
3. The property of MyHorizontalList: itemRenderer, which uses a custom component: MyItemRenderer
4. A public type functon is defined in MyItemRenderer: refresh()
5. When the program is initialized, it is well known that if you want to assign values to the items in the MyItemRenderer, the form to be used should be used. At the same time, 6 MyItemRenderers should be established, and they are all presented by the HorizontalList renderer itemRenderer.
Everything went smoothly in the above situation, right? :)
The problem arises: When the Flex App is running, I want to dynamically call public funciton: refresh() in MyItemRenderer contained in MyHorizontalList, that is, I need to call all refresh in MyItemRenderer. This call does not require any event (such as itemClick event).
How to explore:
1. Go through the child in MyHorizontalList and find that there are only three.
2. Automatic traversal triggers itemClick event, which can be achieved, but efficiency is a problem (when building a large Flex App, you must pay attention to the issue of efficiency.)
I had no choice but to search and answer the questions, and finally I found the solution to this problem in combination with Adobe Doc.
Here is the idea to look for:
HorizontalList is inherited from ListBase, and ListBase belongs to the class below the package, so the most promising thing is below this
Implementation method:
1. When using the first method in the exploration stage, you will get that there are three children in MyHorizontalList, and we need its last child.
That is: (2)
2. Please be sure to note: the last child is a variable of type ListBaseContentHolder, and ListBaseContentHolder exists in it.
Therefore, we need to manually introduce the following class:
import ;
var myHList : ListBaseContentHolder = ( 2 ) as ListBaseContentHolder;
Through the above code, I get myHList. (Note that its type is: ListBaseContentHolder )
3. There is a public method: listItems in ListBaseContentHolder, and its definition is as follows:
Original text: An Array of Arrays that contains the item renderer instances that render each data provider item.
Translation: An Array consisting of an array containing an instance of the project renderer provided by dataProvider.
It is not difficult to see from the above translation that listItems is the method we want to use, that is, an array containing MyItemRenderer.
4. The following source is relatively simple:
//Get MyRendererArr
var MyRendererArr : Array = [ 0 ] as Array ;
//Transfer MyRendererArr
for ( var i : int = 0; i < MyRendererArr .length; i ++ ) {
//Each MyRendererArr[i] is an instance of MyItemRenderer
var temp : MyItemRenderer= MyRendererArr [ i ] as MyItemRenderer;
//The final result I want
();
}
Isn't it very simple? In fact, the above method applies to any component that inherits from ListBase.
About ListBase:
It is the base class that provides a list of items, i.e. it is the base class of itemRenderer.