How can I get the ItemRenderer of a Flex Spark List from its DataProvider Object?

試著忘記壹切 提交于 2019-12-23 12:28:58

问题


In Flex I can create an ItemRenderer to represent each item in the Lists DataProvider but how do I access the instance of the ItemRenderer via the DataProviders Object? Something like myList.getItemRenderer(dp.getItemAt(10));


回答1:


public function getItemRenderer(list:List, item:Object):ItemRenderer
{
    var dataGroup:DataGroup = list.dataGroup;
    var n:int = dataGroup.numElements;
    for (var i:int = 0; i < n; i++)
    {
        var renderer:ItemRenderer = dataGroup.getElementAt(i) as ItemRenderer;
        if (renderer && renderer.data == item)
            return renderer;
    }
    return null;
}



回答2:


By using this method, you cannot get the renderer that is outside of the viewable area and there will also be a lot of other issues.




回答3:


If you need to change how the item renderer is behaving, change the data that is causing the behavior.

var items:ArrayCollection = this.dataProvider as ArrayCollection;
    var newItems:ArrayCollection = new ArrayCollection();
    if (items.length > 0) {
        for (var i:int = 0; i < items.length; i++) {
            var item:Object = items[i] as Object;

            if (!item.editMode) {
                item.editMode = true;
            } else {
                item.editMode = false;
            }

            newItems.addItem(item);
        }
    }

    this.dataProvider = null;
    this.dataProvider = newItems;

This is a simple example where the I mark items to be deletable and the renderer changes according since the data has been changed. For larger data sets, I understand this can be a little slow but it gives you the control you are after.

If you need to change one renderer, simply modify the data on the corresponding object in the dataProvider or dataGroup.

var item:Object = this.getItemAt(index);



回答4:


I know this is an old thread, but perhaps it will help someone:

var item:ItemRenderer = list.dataGroup.getElementAt(0) as ItemRenderer;

That will give you the ItemRenderer, if you have a custom IR then just use that class and cast and your set.



来源:https://stackoverflow.com/questions/4026198/how-can-i-get-the-itemrenderer-of-a-flex-spark-list-from-its-dataprovider-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!