通过ListView获取ListCell [英] Get ListCell via ListView

查看:168
本文介绍了通过ListView获取ListCell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ListView ,带有我自己的 ListCell< MyObject> 实现。通过网络信号,我收到应该更改的ListCell的索引。

I have a ListView with my own ListCell<MyObject> implementation. Via a network signal, I receive an index of my ListCell that should be changed.

超过 listView.getItems()。get(index); 访问模型没有问题,但是我想使用收到的索引对listCell进行布局更改,并使用索引+ 1对ListCell进行布局更改;

Over listView.getItems().get(index); there is no problem to access the model, but I want to make a layout change to the listCell with the received index and a layout change to the ListCell with the index+1;

如何通过ListView访问ListCell?

How can I access the ListCell via the ListView?

我搜索这样的方法

listView.getListCell(index);


推荐答案

不幸的是现在没有API来获取List Cell通过索引或获取ListView的所有子项(listcells)。一种解决方案是,在MyObject类中定义一个新的StringProperty specialIndicator。

Unfortunately right now there is no API to get List Cell by index or to get All children's(listcells) for ListView. One solution can be, define a new StringProperty specialIndicator in your MyObject class.

 class MyObject {

        ....//u r properties
        private StringProperty specialIndicator;

当你从网络信号中获得索引时,设置object的这个specialIndicator属性并对ListView执行forcerefresh

When ever you get index from network signal set this specialIndicator property of object and do forcerefresh of ListView

public void onReceivedNetWorkSignalIndex() {
 listView.getItems().get(indexFromService).setSpecialIndicator("selected");
 listView.getItems().get(indexFromService+1).setSpecialIndicator("selectedplusone");
//force refresh listview (it will trigger cellFactory again so that you can manipulate layout)
 listView.setItems(null);
 listView.setItems(allObjects);

}

因为你已经有自定义对象ListView,我假设你已经有自定义cellFactory(如果不是你必须创建一个),修改你的自定义单元工厂来处理这个特殊的指标

As you already have custom Object ListView , i am assuming you already have custom cellFactory (if not you have to create one ) ,Modify your custom cell factory to handle this special Indicators

         listView.setCellFactory(new Callback<ListView<MyObject>, ListCell<MyObject>>() {
                    @Override
                    public ListCell<MyObject> call(ListView<MyObject> myObjectListView) {
                        ListCell<MyObject> cell = new ListCell<MyObject>(){
                            @Override
                            protected void updateItem(MyObject myObject, boolean b) {
                                super.updateItem(myObject, b);
                                if(myObject != null) {
                                    setText(myObject.getName());
                                    if("selected".equalsIgnoreCase(myObject.getSpecialIndicator())) {
                                        System.out.println("Setting new CSS/graphics for index retun from service." + myObject.getName());
                                    } else if("selectedplusone".equalsIgnoreCase(myObject.getSpecialIndicator())) {
                                        System.out.println("Setting new CSS/Graphics for index+1 returned from service" + myObject.getName());
                                    }
                              myObject.setSpecialIndicator(""); // reset it back to empty
                                }
                            }
                        };

                        return cell;
                    }
                });

以下是整个示例应用程序,您可以查看它(如果上述说明不清楚) )。

Here is the whole sample Application ,you can look into it (in case the above explanation is not clear).

public class ListViewTest extends Application {


    @Override
    public void start(Stage stage) throws Exception {

        VBox root = new VBox();
        final ObservableList<MyObject> allObjects = FXCollections.observableArrayList(new MyObject("object0"), new MyObject("object1"),new MyObject("object2"),new MyObject("object3"),new MyObject("object4"));
       final  ListView<MyObject> listView = new ListView<>(allObjects);
        listView.setCellFactory(new Callback<ListView<MyObject>, ListCell<MyObject>>() {
            @Override
            public ListCell<MyObject> call(ListView<MyObject> myObjectListView) {
                ListCell<MyObject> cell = new ListCell<MyObject>(){
                    @Override
                    protected void updateItem(MyObject myObject, boolean b) {
                        super.updateItem(myObject, b);
                        if(myObject != null) {
                            setText(myObject.getName());
                            if("selected".equalsIgnoreCase(myObject.getSpecialIndicator())) {
                                System.out.println("Setting new CSS/graphics for index retun from service." + myObject.getName());
                                setText("I am selected Index from Service");
                            } else if("selectedplusone".equalsIgnoreCase(myObject.getSpecialIndicator())) {
                                System.out.println("Setting new CSS/Graphics for index+1 returned from service" + myObject.getName());
                                setText("I am selected Index +1  from Service");
                            }
                            myObject.setSpecialIndicator(""); // reset it back to empty
                        }
                    }
                };

                return cell;
            }
        });
        Button serviceIndex2 = new Button("ServiceIndex2");
        serviceIndex2.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                int indexFromService =2;
                listView.getItems().get(indexFromService).setSpecialIndicator("selected");
                listView.getItems().get(indexFromService+1).setSpecialIndicator("selectedplusone");
                listView.setItems(null);
                listView.setItems(allObjects);
            }
        });
        root.getChildren().addAll(listView,serviceIndex2);
        Scene scene = new Scene(root,500,500);
        stage.setScene(scene);
        stage.show();

    }

    public static void main(String[] args) {
        launch(args);
    }

    class MyObject {

        private StringProperty name;
        private StringProperty specialIndicator;

        MyObject(String name) {
            this.name = new SimpleStringProperty(name);
            this.specialIndicator = new SimpleStringProperty();
        }

        public String getName() {
            return name.get();
        }

        public StringProperty nameProperty() {
            return name;
        }

        public void setName(String name) {
            this.name.set(name);
        }

        public String getSpecialIndicator() {
            return specialIndicator.get();
        }

        public StringProperty specialIndicatorProperty() {
            return specialIndicator;
        }

        public void setSpecialIndicator(String specialIndicator) {
            this.specialIndicator.set(specialIndicator);
        }
    }
}

这篇关于通过ListView获取ListCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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