2016-11-08 57 views
2

下面是示例代码:JavaFXPorts:ListView中选择的项目没有选择

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-插件1.1.0,胶子插件2.4.0上,NetBeans 8.1的Windows 10 Pro中,64位

环境来进行测试:Android装置三星Galaxy A5 2016,机器人6.0.1

重现步骤:1.生成样本代码:JavaFXPorts 8.60.8,javafxmobile-插件1.1。 0,Gluon Plugin 2.4.0; 2.在Android设备上安装并运行示例(Android 6.0.1); 3. ListView控件触摸,然后选择从ListView的任何项目 - 没有选择项目 - > BUG

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

回答

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)); 
+0

你的解决方法是可以接受的。谢谢。 ...并且你想说些什么[ComboBox行为](http://stackoverflow.com/questions/40373812/javafxports-is-this-correct-behavior-for-javafx-scene-control-combobox-on-andro ) –

+0

...但为聚焦ans选定的项目造型不起作用... –

+0

如何将其添加到单元格而不是鼠标监听器:'{listView.getSelectionModel()。select(item); }' –