行渲染器的高度取决于行索引

时间:2014-03-14 14:41:07

标签: flex flex3

我在IDropInListItemRenderer内使用List。行的高度取决于数据及其在List视图中的位置。

如果行索引发生变化,我该如何重新测量?

updateDisplayList每次都不会被调用,而且到那时为止已经太迟了,因为List已经测量过了。

修改

我试图实现的效果类似于iOS,其中一个部分的标题贴在顶部 iOS UITableView with sections

以下是无法正确测量的基本渲染器:

import mx.controls.Label;
import mx.controls.listClasses.BaseListData;
import mx.controls.listClasses.IDropInListItemRenderer;
import mx.controls.listClasses.IListItemRenderer;
import mx.core.UIComponent;
import mx.events.FlexEvent;

public class MyRowRenderer extends UIComponent implements IListItemRenderer, IDropInListItemRenderer {
    private static const HEADER_HEIGHT:int = 20;
    private static const LABEL_HEIGHT:int = 20;

    private var _data:Object;
    private var _label:Label;
    private var _header:Label;
    private var _listData:BaseListData;

    public function MyRowRenderer() {
        super();
    }

    [Bindable("dataChange")]
    public function get data():Object {
        return _data;
    }

    public function set data(value:Object):void {
        this._data = value;
        invalidateProperties();
        dispatchEvent(new FlexEvent(FlexEvent.DATA_CHANGE));
    }

    [Bindable("dataChange")]
    public function get listData():BaseListData {
        return _listData;
    }

    public function set listData(value:BaseListData):void {
        _listData = value;
    }

    override protected function createChildren():void {
        super.createChildren();

        _header = new Label();
        addChild(_header);

        _label = new Label();
        addChild(_label);
    }

    override protected function measure():void {
        super.measure();

        if (_label == null || _header == null) {
            return;
        }

        var h:Number = LABEL_HEIGHT;

        if (_listData.rowIndex == 0) {
            h += HEADER_HEIGHT;
        }

        explicitHeight = measuredHeight = height = h;
    }

    override protected function commitProperties():void {
        super.commitProperties();

        _label.text = _data.label;
        _header.text = _data.header;
    }

    override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
        super.updateDisplayList(unscaledWidth, unscaledHeight);

        _header.visible = _header.includeInLayout = (_listData.rowIndex == 0);

        if (_header.visible) {
            _header.move(0, 0);
            _header.setActualSize(unscaledWidth, HEADER_HEIGHT);

            _label.move(0, HEADER_HEIGHT);
            _label.setActualSize(unscaledWidth, LABEL_HEIGHT);
        } else {
            _label.move(0, 0);
            _label.setActualSize(unscaledWidth, LABEL_HEIGHT);
        }
    }
}

}

1 个答案:

答案 0 :(得分:0)

我最终创建了List的子类并重写了shiftRow函数来调度自定义RowIndexChangeEvent。在行渲染器的set listData中,它为RowIndexChangeEvent的所有者添加了一个事件监听器,并使其属性,大小和显示列表无效。