如何在ListView中下载带有自定义单元格的fxml



我想在ListView中创建一个自定义单元格。抱歉我英语不好!我想在ListView中显示图片、名称和状态。为此,我使用了一个不同的包含HboxFxml

  public class Controller {    
   CollectionContactForListCollection contactForList = new   CollectionContactForListCollection();
@FXML
private ListView<Contact> listContact ;
@FXML
HBox hbox;
@FXML
ImageView avatar;
@FXML
Label labelName;
@FXML
Label lblStatus;
@FXML
Label lblSense;
@FXML
private void initialize(){ 
    contactForList.fieldData();
    // listContact.setItems((ObservableList)       contactForList.getContactList()); 
    listContact.setCellFactory(new Callback<ListView<Contact>, ListCell<Contact>>() {
        @Override
        public ListCell<Contact> call(ListView<Contact> param) {              
            ListCell<Contact> listCell = new ListCell<Contact>() {                    
                @Override
                protected void updateItem(Contact item, boolean empty) {
                    super.updateItem(item, empty);
                    if (empty || item == null) {
                        setText(null);
                        setGraphic(null);
                    } else {
                        //This method does not work download                          
                       FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/view/boxInContact.fxml"));
                       fxmlLoader.setController(this);                      
                        labelName.setText(item.getName());
                        lblSense.setText(item.getSense());
                        lblStatus.setText(item.getStatus());
                        avatar.setImage(item.getImage());                            
                    }
                }
            };
            return listCell;
        }
    });
    listContact.setItems((ObservableList) contactForList.getContactList());
}

一般来说,您应该为每个FXML文件使用不同的控制器类。使用您的代码,所有单元格都使用相同的控制器实例,因此每个控件只有一个引用,即使有许多labelName等(每个单元格一个)。

因此,为为列表单元格定义的FXML定义一个控制器,并定义更新控件所需的方法:

public class ContactCellController {
    @FXML
    private Label labelName ;
    @FXML
    private Label labelStatus ;
    @FXML
    private Label labelSense ;
    @FXML
    private ImageView avatar ;
    public void setName(String name) {
        labelName.setText(name);
    }
    public void setStatus(String status) {
        labelStatus.setText(status);
    }
    public void setSense(String sense) {
        labelSense.setText(sense);
    }
    public void setAvatarImage(Image image) {
        avatar.setImage(image);
    }
}

更新FXML文件以使用fx:controller="my.package.ContactCellController"的控制器属性,然后您的单元实现可以看起来像

listContact.setCellFactory(lv -> new ListCell<Contact>() {
    private Node graphic ;
    private ContactCellController controller ;
    {
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("/view/boxInContact.fxml"));
            graphic = loader.load();
            controller = loader.getController();
        } catch (IOException exc) {
            throw new RuntimeException(exc);
        }
    }
    @Override
    protected void updateItem(Contact contact, boolean empty) {
        super.updateItem(contact, empty);
        if (empty) {
            setGraphic(null);
        } else {
            controller.setName(contact.getName());
            controller.setStatus(contact.getStatus());
            controller.setSense(contact.getSense());
            controller.setAvatarImage(contact.getImage());
            setGraphic(graphic);
        }
    }
});

最新更新