在Java fx

时间:2018-11-27 09:59:06

标签: java javafx javafx-8

最近,我编写了一个场景,使用自定义listview在javafx中显示与recyclerview相同的场景。我的自定义列表单元格

public class QuestionCell extends ListCell<TestGrammarModel.Question> {
    private HBox content;
    private HBox choose;
    private Text question;
    private RadioButton optionA;
    private RadioButton optionB;
    private RadioButton optionC;
    private RadioButton optionD;
    private Text ans;
    private Text key;
    private Text number;
    private VBox vBox;
    private ToggleGroup group;
    private boolean isSummitted;
    public QuestionCell(boolean isSummitted){
        super();
        question = new Text();
        optionA = new RadioButton();
        optionB = new RadioButton();
        optionC = new RadioButton();
        optionD = new RadioButton();

        number = new Text();
        content = new HBox(number, question);
        choose= new HBox(  optionA, optionB ,
                optionC ,optionD);
        HBox.setMargin(optionA, new Insets(60, 70, 30, 70));
        HBox.setMargin(optionB, new Insets(60, 70, 30, 70));
        HBox.setMargin(optionC, new Insets(60, 70, 30, 70));
        HBox.setMargin(optionD, new Insets(60, 70, 30, 70));
        group = new ToggleGroup();
        optionB.setToggleGroup(group);
        optionA.setToggleGroup(group);
        optionC.setToggleGroup(group);
        optionD.setToggleGroup(group);
        choose.setAlignment(Pos.CENTER);
        vBox = new VBox(content, choose);
        vBox.setSpacing(10);
        this.isSummitted = isSummitted;
    }

    public ToggleGroup getGroup() {
        return group;
    }

    @Override
    protected void updateItem(TestGrammarModel.Question item, boolean empty) {
        super.updateItem(item, empty);
        if (item !=null && !empty ){
            //set text
            question.setText(item.getQuestion());
            optionA.setText("A:  "+item.getOptionA());
            optionB.setText("B:  "+item.getOptionB());
            optionC.setText("C:  "+item.getOptionC());
            optionD.setText("D:  "+item.getOptionD());
            //set font
            optionA.setFont(Font.font(18));
            optionB.setFont(Font.font(18));
            optionC.setFont(Font.font(18));
            optionD.setFont(Font.font(18));
            question.setFont(Font.font(18));
            number.setFont(Font.font(18));
            //set user date
            optionA.setUserData(item.getOptionA());
            optionB.setUserData(item.getOptionB());
            optionC.setUserData(item.getOptionC());
            optionD.setUserData(item.getOptionD());
            number.setText(String.valueOf(item.getNumber()) + ": ");
            setGraphic(vBox);

          //when user did not click on Submit button 
        if (!isSummitted){
                group.selectedToggleProperty().addListener((obs, oldSel, newSel) -> {
                    if (group.getSelectedToggle() != null) {
                        if (group.getSelectedToggle().getUserData() != null ){

                            if (optionA.isSelected())
                                answer.get(selectedIdx)[getIndex()]  = "A";
                            else  if (optionB.isSelected())
                                answer.get(selectedIdx)[getIndex()]  = "B";
                            else  if (optionC.isSelected())
                                answer.get(selectedIdx)[getIndex()]  = "C";
                            else
                                answer.get(selectedIdx)[getIndex()]  = "D";
                            System.out.println(answer.get(selectedIdx)[getIndex()]);
                        }
                        System.out.println(group.getSelectedToggle().getUserData().toString());
                        //get topic's index is selected

                    }
                    if (answer.get(selectedIdx)[getIndex()].equals(item.getKey())){
                        answer.get(selectedIdx)[answer.get(selectedIdx).length -2] =
                                Integer.toString(Integer.parseInt(answer.get(selectedIdx)[answer.get(selectedIdx).length -2 ])+1);
                        answer.get(selectedIdx)[answer.get(selectedIdx).length -1] =
                                Integer.toString(Integer.parseInt(answer.get(selectedIdx)[answer.get(selectedIdx).length-1]) -1) ;
                    }
                });
            }
         }else { setGraphic(null);
                 }
              }

我在这里setCellFactory:

 listView.setCellFactory(param -> new QuestionCell(false));

当我单击自定义列表视图的第一个单元格或第二个单元格时,滚动到列表视图的末尾时将自动选择列表视图的末尾单元格。 我尝试调试并弄清楚,每当我滚动listview时,都会重新创建和更新一些listcell。我在stackoveflow中进行了搜索,并得到了Weird recycle of cells in ListView - JavaFX,但那无济于事。我做错了什么? 这是我的问题。当我在问题1上选择A并向下滚动时,将根据问题1自动选择问题7 [https://s31.aconvert.com/convert/p3r68-cdx67/zj9aw-aiag5.gif ][1]

0 个答案:

没有答案