This bug report seems to describe the design decisions around encapsulating the scrollbar object and events.
This post shows how you can fake a ScrollEvent by keying off of the layoutY property of an individual ListCell. ListCells can be recycled, so I put a handler on each of the ListCells and interrogate the data value to determine which one is the last item.
The use case for this is to load the ListView and fetch data once the last item is encountered.
This is a real hack, but it's using public APIs.
public class LVSApp extends Application { private final static Integer LAST_ITEM = 199; @Override public void start(Stage primaryStage) throws Exception { VBox vbox = new VBox(); ListView<Integer> lv = new ListView<>(); lv.setCellFactory((i) -> new ListCell<Integer>() { private ChangeListenerlistener = (obs, ov, nv) -> { if( getText() != null && getText().equals(String.valueOf(LAST_ITEM)) && ((getListView().getHeight()-this.getHeight() -nv.doubleValue()) > 0 )) { System.out.println("last item in view " + " scrolled into view (fetch more data?)"); } }; { layoutYProperty().addListener(listener); } @Override protected void finalize() throws Throwable { super.finalize(); layoutYProperty().removeListener(listener); } @Override protected void updateItem(Integer item, boolean empty) { if( item != null && !empty ) { setText( String.valueOf(item) ); } else { setText( "" ); } } } ); for( int i=0; i<200; i++ ) { lv.getItems().add(new Integer(i)); } vbox.getChildren().add( lv ); Scene scene = new Scene(vbox); primaryStage.setScene( scene ); primaryStage.show(); } public static void main(String[] args) { launch(args); } }
No comments:
Post a Comment