【问题标题】:JavaFXPorts: Selected item in ListView is not selectedJavaFXPorts:未选择 ListView 中的选定项
【发布时间】:2016-11-08 08:48:00
【问题描述】:

这里是示例代码:

package com.javafxportslistviewdemo;

import com.gluonhq.charm.down.Platform;
import com.gluonhq.charm.down.Services;
import com.gluonhq.charm.down.plugins.LifecycleEvent;
import com.gluonhq.charm.down.plugins.LifecycleService;
import javafx.application.Application;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.VBox;
import javafx.stage.Screen;

import javafx.stage.Stage;

public class JavaFXPortsListViewDemo extends Application {

    @Override
    public void init() {
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        Screen primaryScreen = Screen.getPrimary();
        Rectangle2D visualBounds = primaryScreen.getVisualBounds();
        double width = visualBounds.getWidth();
        double height = visualBounds.getHeight();

        Label label = new Label("Here is selected item...");

        ListView<String> listView = new ListView<>();
        ObservableList<String> items = FXCollections.observableArrayList(
                "one", "two", "three", "four");
        listView.setItems(items);
        listView.getSelectionModel().selectedItemProperty().addListener((ObservableValue<? extends String> ov, String old_val, String new_val) -> {
            label.setText(new_val);
        });

        VBox root = new VBox();
        root.getChildren().addAll(label, listView);

        Scene scene = new Scene(root, width, height);

        Services.get(LifecycleService.class).ifPresent(ls -> {
            ls.addListener(LifecycleEvent.PAUSE, () -> onPause());
            ls.addListener(LifecycleEvent.RESUME, () -> onResume());
        });

        scene.addEventHandler(KeyEvent.KEY_RELEASED, e -> {
            if (KeyCode.ESCAPE.equals(e.getCode())) {
                if (Platform.isAndroid()) {
                    // bring up the menu or other Android stuff
                    Services.get(LifecycleService.class).ifPresent(LifecycleService::shutdown);
                } else {
                    // bring up the menu or other Desktop stuff
                    Services.get(LifecycleService.class).ifPresent(LifecycleService::shutdown);
                }
            }
        });

        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private void onPause() {
    }

    private void onResume() {
    }
}

开发环境:JavaFXPorts 8.60.8、javafxmobile-plugin 1.1.0、Gluon Plugin 2.4.0、NetBeans 8.1、Windows 10 Pro、64 位

测试环境:Android 设备 Samsung Galaxy A5 2016,Android 6.0.1

重现步骤: 1. 使用 JavaFXPorts 8.60.8、javafxmobile-plugin 1.1.0、Gluon Plugin 2.4.0 构建示例代码; 2.在Android设备(Android 6.0.1)上安装并运行示例; 3. 触摸 ListView 并从 ListView 中选择任何项目 - 未选择项目 -> BUG

为 JavaFXPorts 的问题跟踪器添加错误:JavaFXPorts issue

【问题讨论】:

    标签: javafx javafxports gluon-mobile


    【解决方案1】:

    感谢您报告问题。

    众所周知,在某些三星设备中,触摸事件处理与其他 Android 设备不同。

    虽然这在 JavaFXPorts 中已修复,但您可以使用以下解决方法:为 ListCell 提供一个侦听器,在内部连接选择。

    根据您的样本:

        ListView<String> listView = new ListView<>();
        listView.setCellFactory(p -> new ListCell<String>() {
    
            private String item;
            {
                setOnMouseClicked(e -> listView.getSelectionModel().select(item));
            }
    
            @Override
            protected void updateItem(String item, boolean empty) {
                super.updateItem(item, empty); 
                this.item = item;
                setText(item);
            }
    
        });
        listView.getSelectionModel().selectedItemProperty()
          .addListener((ov, old_val, new_val) -> label.setText(new_val));
    

    【讨论】:

    • 您的解决方法是可以接受的。谢谢。 ...你有什么想说的吗ComboBox behavior
    • 如何将其添加到单元格而不是鼠标侦听器:{ listView.getSelectionModel().select(item); }
    • 样式仍然无法正常工作,选择项目也无法再次工作。
    • 你可以试试这个:{ setOnMouseClicked(e -&gt; listView.getSelectionModel().select(item)); }? (我无法重现)
    • 对我来说,完整的解决方法是:{ setOnMouseClicked(e -&gt; { label.setText(item); listView.getSelectionModel().select(item); }); } ...并且样式和选择正在工作。谢谢。
    猜你喜欢
    • 2014-03-22
    • 2013-07-19
    • 1970-01-01
    • 1970-01-01
    • 2016-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多