2016-11-08 4 views
2

Voici un exemple de code:JavaFXPorts: élément sélectionné dans ListView n'est pas sélectionnée

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() { 
    } 
} 

Environnement pour le développement: JavaFXPorts 8.60.8, javafxmobile-plugin 1.1.0, gluons Plugin 2.4.0, NetBeans 8.1, Windows 10 Pro, 64 bits

Environnement pour le test: appareil Android Samsung Galaxy A5 2016, Android 6.0.1

étapes pour reproduire: 1. Construire un exemple de code avec: JavaFXPorts 8.60.8, javafxmobile-plugin 1.1. 0, Gluon Plugin 2.4.0; 2. Installez et exécutez l'échantillon sur l'appareil Android (Android 6.0.1); 3. Appuyez sur ListView et Sélectionnez un élément de ListView - article n'a pas été sélectionné -> BUG

Ajouté bug pour problème de suivi JavaFXPorts: JavaFXPorts issue

Répondre

1

Merci pour avoir signalé ce problème.

Il est déjà connu que dans certains appareils Samsung, la gestion des événements tactiles ne fonctionne pas comme dans le reste des appareils Android.

Bien que cela soit résolu dans JavaFXPorts, vous pouvez utiliser la solution de contournement suivante: fournissez un écouteur au ListCell qui connecte la sélection en interne.

en fonction de votre échantillon:

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

Votre solution est acceptable. Merci. ... et voulez-vous quelque chose à dire [comportement ComboBox] (http://stackoverflow.com/questions/40373812/javafxports-is-this-correct-behavior-for-javafx-scene-control-combobox-on-andro –

+0

... mais le style pour l'élément sélectionné et focalisé ne fonctionne pas ... –

+0

Qu'en est-il de l'ajouter à la cellule au lieu du mouse listener: '{listView.getSelectionModel(). Select (item); } ' –