动作 - 访问列表/ DataProvider的自定义CellRenderer [英] ActionScript - Access List/DataProvider From Custom CellRenderer

查看:127
本文介绍了动作 - 访问列表/ DataProvider的自定义CellRenderer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在code列示了在使用自定义单元格渲染器(CustomListCell类)主控制器类的List对象。在CustomListCell类创建为将要用于从列表的dataProvider删除自身细胞Button对象。

我怎么能正确地从它的自定义单元格渲染器访问父List对象?

  //控制器类
私有函数createList():无效
 {
 供应商=新的DataProvider(数据);

 名单=新名单();
 list.width = 200;
 list.height = 400;
 list.rowHeight = 50;
 = List.dataProvider被提供;
 list.setStyle(的cellRenderer,CustomListCell);
 }

-----

// CustomListCell类
进口fl.controls.Button;

公共类CustomListCell扩展Sprite实现ICellRenderer
 {
 公共职能CustomListCell()
  {
  VAR按钮:按钮=新按钮();
  button.label =删除单元格;
  button.addEventListener(MouseEvent_MOUSE_DOWN,deleteCellHandler);
        的addChild(按钮);
  }

 私有函数deleteCellHandler(EVT:MouseEvent)方法:无效
  {
  //访问列表/的DataProvider这里
  }

 //需要实现ICellRenderer功能跟随
 }
 


更新

下面是我实现ICellRenderer与Flash V3 List组件工作的自定义呈现。该列表的dataProvider包含2个元素为每个单元:randomColor和randomNumber

 包
{
//进口
进口fl.controls.Button;
进口fl.controls.List;
进口fl.controls.listClasses.ICellRenderer;
进口fl.controls.listClasses.ListData;
进口flash.display.Sprite;
进口flash.events.MouseEvent;
进口API元素flash.text.TextField;
进口flash.text.TextFieldAutoSize;
进口flash.geom.ColorTransform;

//类
公共类TouchListRenderer扩展Sprite实现ICellRenderer
    {
    //属性
    私人VAR cellWidthProperty:数字;
    私人VAR cellHeightProperty:数字;
    私人VAR dataProperty:对象;
    私人VAR listDataProperty:的ListData;
    私人VAR selectedProperty:布尔;

    //单元格显示对象
    私人VAR backgroundCanvas:= MySprite的MySprite的新();
    私人VAR numberTextField:文本字段=新的TextField();
    私人VAR按钮:按钮=新按钮();

    //构造函数
    公共职能TouchListRenderer()
        {
        }

    //尺寸二传手(getter函数故意略)
    公共职能的setSize(宽度:编号,身高:编号):无效
        {
        cellWidthProperty =宽度;
        cellHeightProperty =高度;
        }

    //数据二传手
    公共职能组数据(值:对象):无效
        {
        dataProperty =价值;
        }

    //数据消气
    公共函数获取数据():对象
        {
        返回dataProperty;
        }

    //列表数据二传手
    公共职能设置的ListData(价值:的ListData):无效
        {
        listDataProperty =价值;
        }

    //列表数据消气
    公共职能得到的ListData()的ListData
        {
        返回listDataProperty;
        }

    //选二传手
    公共功能设置选择(价值:布尔):无效
        {
        selectedProperty =价值;

        布局();
        }

    //选择消气
    公共职能都被选择():布尔
        {
        返回selectedProperty;
        }

    //大小和布局
    专用功能布局():无效
        {
        VAR newColor:的ColorTransform =新的ColorTransform();
        newColor.color = dataProperty.randomColor;

        backgroundCanvas.transform.colorTransform = newColor;
        backgroundCanvas.scaleX = cellWidthProperty / backgroundCanvas.width;
        backgroundCanvas.scaleY = cellHeightProperty / backgroundCanvas.height;

        numberTextField.text = dataProperty.randomNumber;
        numberTextField.autoSize = TextFieldAutoSize.LEFT;
        numberTextField.textColor = 0XFFFFFF;
        numberTextField.x = 50;
        numberTextField.y = cellHeightProperty / 2  -  numberTextField.height / 2;
        numberTextField.border = TRUE;
        numberTextField.selectable = FALSE;

        button.label =删除;
        button.x = cellWidthProperty  -  button.width  -  50;
        button.y = cellHeightProperty / 2  -  button.height / 2;
        button.drawNow();
        button.addEventListener(的MouseEvent.MOUSE_DOWN,buttonClickEventHandler);

        的addChild(backgroundCanvas);
        的addChild(numberTextField);
        的addChild(按钮);
        }

    //按钮单击事件处理
    私有函数buttonClickEventHandler(EVT:MouseEvent)方法:无效
        {
        列表(listDataProperty.owner).removeItemAt(listDataProperty.index);
        }

    //风格二传手
    公共职能的setStyle(风格:字符串值:对象):无效
        {
        }

    //鼠标状态二传手
    公共职能setMouseState(州:字符串):无效
        {
        }
    }
}
 


 包
{
进口flash.display.Sprite;

公共MySprite的类扩展Sprite
    {
    公共MySprite的功能()
        {
        graphics.beginFill(为0xFF0000);
        graphics.drawRect(0,0,10,10);
        graphics.endFill();
        }
    }
}
 

解决方案

唉!答案是在我面前的全部时间!下一次提醒我检查文档:

 列表(listData.owner)
 

<一个href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/fl/controls/listClasses/ListData.html#owner"相对=nofollow> fl.controls.listClasses.ListData.owner

the code below sets up a List object in the main controller class that uses a custom cell renderer (CustomListCell class). the CustomListCell class creates a Button object for the cell that will be used to delete itself from the List's DataProvider.

how can i properly access the parent List object from its custom cell renderer?

//Controller Class
private function createList():void
 {
 provider = new DataProvider(data);

 list = new List();
 list.width = 200;
 list.height = 400;
 list.rowHeight = 50;
 list.dataProvider = provider;
 list.setStyle("cellRenderer", CustomListCell);
 }

-----

//CustomListCell Class
import fl.controls.Button;

public class CustomListCell extends Sprite implements ICellRenderer
 {     
 public function CustomListCell()
  {
  var button:Button = new Button();
  button.label = "Delete Cell";
  button.addEventListener(MouseEvent_MOUSE_DOWN, deleteCellHandler);
        addChild(button);
  }

 private function deleteCellHandler(evt:MouseEvent):void
  {
  //Access List/DataProvider Here
  }

 //required implemented ICellRenderer functions follow
 }


UPDATE

the following is my working custom renderer that implements ICellRenderer with Flash v3 List component. the List's dataProvider consists of 2 elements for each cell: randomColor and randomNumber.

package
{
//Imports
import fl.controls.Button;
import fl.controls.List;
import fl.controls.listClasses.ICellRenderer; 
import fl.controls.listClasses.ListData;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.geom.ColorTransform;

//Class
public class TouchListRenderer extends Sprite implements ICellRenderer
    {
    //Properties
    private var cellWidthProperty:Number;
    private var cellHeightProperty:Number;
    private var dataProperty:Object;
    private var listDataProperty:ListData;  
    private var selectedProperty:Boolean;

    //Cell Display Objects
    private var backgroundCanvas:MySprite = new MySprite();
    private var numberTextField:TextField = new TextField();
    private var button:Button = new Button();

    //Constructor
    public function TouchListRenderer()
        {
        }

    //Size Setter (Getter Functions Intentionally Omitted)
    public function setSize(width:Number, height:Number):void
        {
        cellWidthProperty = width;
        cellHeightProperty = height;
        }

    //Data Setter
    public function set data(value:Object):void
        {
        dataProperty = value;
        }

    //Data Getter
    public function get data():Object
        { 
        return dataProperty; 
        }

    //List Data Setter
    public function set listData(value:ListData):void
        { 
        listDataProperty = value;
        }

    //List Data Getter
    public function get listData():ListData
        { 
        return listDataProperty; 
        }

    //Selected Setter
    public function set selected(value:Boolean):void
        { 
        selectedProperty = value;

        layout();
        }

    //Selected Getter
    public function get selected():Boolean
        { 
        return selectedProperty;
        }

    //Size And Layout
    private function layout():void
        {
        var newColor:ColorTransform = new ColorTransform();
        newColor.color = dataProperty.randomColor;

        backgroundCanvas.transform.colorTransform = newColor;
        backgroundCanvas.scaleX = cellWidthProperty / backgroundCanvas.width;
        backgroundCanvas.scaleY = cellHeightProperty / backgroundCanvas.height;

        numberTextField.text = dataProperty.randomNumber;
        numberTextField.autoSize = TextFieldAutoSize.LEFT;
        numberTextField.textColor = 0xFFFFFF;
        numberTextField.x = 50;
        numberTextField.y = cellHeightProperty / 2 - numberTextField.height / 2;
        numberTextField.border = true;
        numberTextField.selectable = false;

        button.label = "Delete";
        button.x = cellWidthProperty - button.width - 50;
        button.y = cellHeightProperty / 2 - button.height / 2;
        button.drawNow();
        button.addEventListener(MouseEvent.MOUSE_DOWN, buttonClickEventHandler);

        addChild(backgroundCanvas);
        addChild(numberTextField);
        addChild(button);
        }

    //Button Click Event Handler
    private function buttonClickEventHandler(evt:MouseEvent):void
        {
        List(listDataProperty.owner).removeItemAt(listDataProperty.index);
        }

    //Style Setter
    public function setStyle(style:String, value:Object):void
        {
        }

    //Mouse State Setter
    public function setMouseState(state:String):void
        {
        }
    } 
} 


package
{
import flash.display.Sprite;

public class MySprite extends Sprite
    {
    public function MySprite()
        {
        graphics.beginFill(0xFF0000);
        graphics.drawRect(0, 0, 10, 10);
        graphics.endFill();
        }
    }
}

解决方案

ugh! the answer was in front of me the whole time! next time remind me to check the docs:

List(listData.owner)

fl.controls.listClasses.ListData.owner

这篇关于动作 - 访问列表/ DataProvider的自定义CellRenderer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆