在JavaFX的同一场景中多次重用同一ImageView [英] Reusing same ImageView multiple times in the same scene on JavaFX

查看:199
本文介绍了在JavaFX的同一场景中多次重用同一ImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个Twitter样式的ListView,并且我无法在同一列表中多次重复使用同一ImageView.加载多个副本似乎很浪费,并且由于UI虚拟化而导致滚动速度变慢.有什么解决方法吗?

I'm trying to build a Twitter-style ListView, and I couldn't reuse the same ImageView multiple times in the same list. Loading multiple copies seem to be wasteful and causes scrolling to slow down due to UI Virtualization. Are there any workarounds?

public class TwitterCell extends ListCell<Object> {

private static HashMap<String, ImageView> images = new HashMap<String, ImageView>();

@Override
protected void updateItem(Object tweet, boolean empty) {
  super.updateItem(tweet, empty);
  Tweet t = (Tweet) tweet;
  if (t != null) {
    String message = t.getMessage();
    setText(message);
    String imageUrl = t.getImageUrl();
    if (!images.containsKey(imageUrl)) {
      images.put(imageUrl, new ImageView(imageUrl));
    }
    setGraphic(images.get(imageUrl));
  }
}

推荐答案

在JavaFX中,场景图不能包含相同的Node两次,并且无法克隆节点(据我所知).

A scenegraph cannot contain the same Node twice in JavaFX, and there is no way of cloning nodes (as far as I know).

一种解决方法可能是使地图成为HashMap存储Image而不是ImageView,然后将最后一行更改为

A workaround would perhaps be to make your map a HashMap store Image instead of ImageView, and change the last row to

setGraphic(new ImageView(images.get(imageUrl)));

这样,您至少将缓存实际Image的负载,这实际上应该是繁重的工作.

This way you'll at least cache the loading of the actual Image, which should really be the heavy lifting part.

这篇关于在JavaFX的同一场景中多次重用同一ImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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