Extending QNX AlternatingCellRenderer


/ Published in: ActionScript 3
Save to your folder(s)



Copy this code and paste it in your HTML
  1. public class PictureCellRenderer extends AlternatingCellRenderer {
  2.  
  3. /**
  4.   * Skin parts used to render the data
  5.   */
  6. protected var img:Image;
  7. protected var lbl:Label;
  8. protected var bg:Sprite;
  9. protected var format:TextFormat;
  10.  
  11. public function PictureCellRenderer() {
  12. super();
  13. // hide the built in label
  14. label.visible = false;
  15. createUI();
  16. }
  17. /**
  18.   * setSize() is called everytime the tiles size are changed
  19.   * this is where we add our method layout() to reposition/redraw
  20.   * various parts of the cell renderer.
  21.   */
  22. override public function setSize(w:Number, h:Number):void {
  23. super.setSize(w, h);
  24. //we want to draw the skin parts only when the
  25. //actual width/height are set
  26. //this method is called first for the default sizes and then for
  27. //for the user prefered sizes
  28. if (stage) {
  29. layout();
  30. }
  31. }
  32. /**
  33.   * Updates the text and image everytime a new data is set
  34.   * for this renderer instance.
  35.   */
  36. override public function set data(value:Object):void {
  37. super.data = value;
  38. //Update the image source and text if there is valid data.
  39. if (value) {
  40. img.setImage(value.img);
  41. lbl.text = value.label;
  42. }
  43. }
  44. /**
  45.   * Create all the cell renderer components just once
  46.   */
  47. private function createUI():void {
  48. img = new Image();
  49. bg = new Sprite();
  50. lbl = new Label();
  51.  
  52. //...
  53. //code to position the img, Sprite, and label
  54. //...
  55.  
  56. addChild(img);
  57. addChild(bg);
  58. addChild(lbl);
  59. }
  60. /**
  61.   * Draws the rectangle used as background
  62.   * for the label
  63.   */
  64. private function drawBg():void {
  65. //code to redraw the gray rounded rectangular
  66. }
  67. /**
  68.   * Reposition/redraw the renderer parts
  69.   * everytime the tile size is changed
  70.   */
  71. private function layout():void {
  72. lbl.y = height - 20;
  73. lbl.width = width - 10;
  74. drawBg();
  75. onComplete(new Event(Event.COMPLETE));
  76. }
  77. /**
  78.   * Resize the image once the bits were loaded
  79.   */
  80. private function onComplete(e:Event):void {
  81. img.setSize(width - 20, height - 20);
  82. }
  83. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.