Flex:图像在tilelist中切换位置

发布于 2024-08-09 10:11:37 字数 878 浏览 10 评论 0原文

我在 TileList 内的 itemRenderers 中遇到了一个奇怪的问题。

这是一个没有 itemRenderer 的工作示例:152.org/flex/

这是带有 itemRenderer 的损坏版本:152.org/brokenExample/
(我没有代表将这两个都设为链接)

两个示例都启用了“查看源代码”。

要查看问题,请使用损坏的示例,选择一个相册并向下滚动一行。向后滚动,图像将会切换。如果你在工作示例上尝试一下,那就没问题了。

这似乎是一个众所周知的错误,但我找不到解决方案。

更新
我再次开始玩这个例子并发现了其他东西。事实证明您不必覆盖数据设置器。您可以在 itemRenderer 中创建一个新方法,每当图块想要刷新时就设置该方法。因此,诀窍是不要依赖initialize 或creationComplete 方法。

这就是我在应用程序中的 itemRenderer 的内容。

<itemRenderers:ImageTile img="{data}"/>

这是我在 itemRenderer 中的代码。

public function set img(value:String) : void {
  trace("setting source: " + value);
  this.source = value;
  this.name = value.toString().split("/").pop().split(".").shift();
}

我更新了我的示例以反映此更改。

I'm running into an odd issue with itemRenderers inside a TileList.

Here is a working example without an itemRenderer: 152.org/flex/

Here is the broken version with an itemRenderer: 152.org/brokenExample/

(I don't have the rep to make both of these a link)

Both examples have "View Source" enabled.

To see the problem use the broken example, select an album and scroll down one row. Scroll back up and the images will be switched. If you try this on the working example it's fine.

This seems to be a widely known bug, but I can't find a solution for it.

UPDATE
I started playing with this example again and found out something else. Turns out you don't have to override the data setter. You can create a new method in the itemRenderer that is set whenever the tile wants to refresh. So the trick is to not rely on the initialize or creationComplete methods.

This is what I have for the itemRenderer in the Application.

<itemRenderers:ImageTile img="{data}"/>

This is the code I have in the itemRenderer.

public function set img(value:String) : void {
  trace("setting source: " + value);
  this.source = value;
  this.name = value.toString().split("/").pop().split(".").shift();
}

I updated my example to reflex this change.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

無心 2024-08-16 10:11:37

我手边没有你的应用程序,所以我无法进行端到端测试,但我已经查看了你的源代码。您可能需要覆盖 itemRenderer 中的data setter:

<?xml version="1.0" encoding="utf-8"?>
<mx:Image xmlns:mx="http://www.adobe.com/2006/mxml" initialize="init()">

 <mx:Script>
  <![CDATA[

   override public function set data(value:Object):void
   {
    super.data = value;
    this.source = data;
    this.name = data.toString().split("/").pop().split(".").shift();
   }

   private function init() : void {
    // Removed from your source and transplanted above
   }

  ]]>
 </mx:Script>

</mx:Image>

Flex 将尝试重用列表中的项目渲染器(这意味着您可能期望的生命周期事件——初始化、创建完成等——不会总是触发),因此如果您想确保渲染器在数据项更改时得到更新(就像滚动事件发生时一样),最佳实践是覆盖渲染器的 data 属性。这很可能会解决问题。

I don't have your app handy, so I can't test end-to-end, but I've looked at your source. You probably need to override the data setter in your itemRenderer:

<?xml version="1.0" encoding="utf-8"?>
<mx:Image xmlns:mx="http://www.adobe.com/2006/mxml" initialize="init()">

 <mx:Script>
  <![CDATA[

   override public function set data(value:Object):void
   {
    super.data = value;
    this.source = data;
    this.name = data.toString().split("/").pop().split(".").shift();
   }

   private function init() : void {
    // Removed from your source and transplanted above
   }

  ]]>
 </mx:Script>

</mx:Image>

Flex will attempt to re-use item renderers in lists (which means the lifecycle events you might be expecting -- initialize, creationComplete, etc. -- won't always fire), so if you want to be sure your renderer gets updated when the data item changes (as it will when scroll events happen), the best practice is to override the renderer's data property. That'll most likely fix the problem.

梦境 2024-08-16 10:11:37

也许尝试在创建完成时失效?

根据我对 DataGrids 的回忆(其工作原理与图块列表有些相似),当一个项目成为焦点时,它会重新创建。

<mx:itemRenderer>
  <mx:Image id="myImage" creationComplete="myImage.invalidate()" />
</mx:itemRenderer>

还没有尝试过这段代码,但我认为这是你想要开始寻找的地方。我查看了您的 itemRenderer 组件。尝试creationComplete而不是初始化来调用你的函数

Maybe try to invalidate on creationComplete?

From what I recall with DataGrids (which work somewhat similarly to a tilelist), when an item comes into focus its recreated.

<mx:itemRenderer>
  <mx:Image id="myImage" creationComplete="myImage.invalidate()" />
</mx:itemRenderer>

Haven't tried this code but I think this is where you want to start looking. I took a look at your itemRenderer component. Try creationComplete instead of initialize to call your function

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文