滚动后列表视图项目顺序已更改

时间:2017-05-10 20:40:08

标签: listview javafx

我有这个循环,我填充我的listview:

private final ArrayList<Image> stationLogos = new ArrayList<>();
private List<Item> m_station_items;
ListView<String> list = new ListView<>();
ObservableList<String> items = FXCollections.observableArrayList();
stationLogos.add(null);

for (Item item : m_station_items)
    {
        station_name = item.getTitle();
        items.add(station_name);
        BufferedImage buffered = (BufferedImage) m_app.retrieveBitmap(item.getLogoPath());

        Image tempImg = SwingFXUtils.toFXImage(buffered, null );
        stationLogos.add(tempImg);

        list.setCellFactory(param -> new ListCell<String>() {
        private final ImageView imageView = new ImageView();

        @Override
        public void updateItem(String name, boolean empty) 
        {
            super.updateItem(name, empty);
            if (name != null) { 
                if(logoIterations < stationLogos.size())
                    {
                    imageView.setImage(stationLogos.get(logoIterations));
                    setText(name);
                    setGraphic(imageView);
                    logoIterations++;
                    }
                }
            }
        });
    }

list.setItems(items);

它似乎应该在发布之后但是当我向下滚动并备份项目顺序的变化时。任何想法可能是错的?以下是该应用的一个小示例视频:https://youtu.be/LbMuASBgJIQ

1 个答案:

答案 0 :(得分:1)

您应该将列表视图设为ListView<Item>,然后您可以轻松地将其配置为:

ListView<Item> list = new ListView<>();
list.setItems(m_station_items);
list.setCellFactory(lv -> new ListCell<>() {
    private final ImageView imageView = new ImageView();
    @Override
    protected void updateItem(Item item, boolean empty) {
        super.updateItem(item, empty);
        if (empty) {
            setText(null);
            setGraphic(null);
        } else {
            setText(item.getTitle());
            // may need to construct image differently, depending on what is in logoPath
            imageView.setImage(new Image(item.getLogoPath().toURI().toString()));
            setGraphic(imageView);
        }
    }
});