向上滚动时如何触发事件,javafx

How to fire event when scrolling up,javafx

我正在 java 上开发聊天应用程序,我想使用 facebook 使用的功能在用户向上滚动时检索聊天历史记录。我尝试了 "on Scroll" 操作,只要滚动到达滚动条的顶部或底部就会触发事件。现在我只想在滚动条到达顶部时触发操作事件,就像在 facbook 聊天框中一样。

假设你有一个ScrollPane,你可以观察到它的vvalueProperty

scrollPane.vvalueProperty().addListener(
    (ObservableValue<? extends Number> observable, Number oldValue, Number newValue) -> {
    if(newValue.doubleValue() == 0){
        System.out.println( "AT TOP" );
        // load more items
    }
});

您还可以使用

滚动到底部
scrollPane.setVvalue( scrollPane.getVmax() );

最初。

这是您想要的示例。希望对您有所帮助。

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class ScrollUtil extends Application 
{
    private boolean scrollToBottom = false;
    private boolean scrollToTop = false;

    public static void main(String[] args) 
    {
        launch(args);
    }

    @Override
    public void start(final Stage stage) throws Exception 
    {

        final VBox root = new VBox();
        final ScrollPane scrollPane = new ScrollPane();

        final VBox vBox = new VBox();
        vBox.setAlignment(Pos.BOTTOM_CENTER);

        scrollPane.setContent(vBox);

        scrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);

        Button button = new Button("add");
        Button button3 = new Button("scroll up");

        button3.setOnAction(new EventHandler<ActionEvent>() 
        {
            @Override
            public void handle(ActionEvent actionEvent) 
            {
                scrollToTop = true;
                 scrollPane.setVvalue(scrollPane.getVmin());
                System.out.println("vmin= "+scrollPane.getVmin() + "; vmax = " + scrollPane.getVmax() + "; vvalue" + scrollPane.getVvalue());
            }
        });

        button.setOnAction(new EventHandler<ActionEvent>() 
        {
            @Override
            public void handle(ActionEvent actionEvent) 
            {
                vBox.getChildren().add(new Label("hallo"));
                scrollToBottom = true;
                //System.out.println(scrollPane.getVmin() + "; max = " + scrollPane.getVmax() + "; " + scrollPane.getVvalue());
            }
        });

        scrollPane.setVvalue(scrollPane.getVmax());
        scrollPane.vvalueProperty().addListener(new ChangeListener<Number>() 
        {
          @Override
          public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) 
          {
            if(scrollToBottom)
            {
              scrollPane.setVvalue(scrollPane.getVmax());
              scrollToBottom = false;
            }

            if(newValue.doubleValue() ==0)
            {
                vBox.getChildren().add(new Label("hallo"));
                System.out.println("min value ="+scrollPane.getVmin());
                //scrollPane.setVvalue(scrollPane.getVmin());
                scrollToTop = true;
                System.out.println("now get the chat history");
            }
          }
        });

        Button button2 = new Button("scroll");
        button2.setOnAction(new EventHandler<ActionEvent>() 
        {
            @Override
            public void handle(ActionEvent actionEvent) 
            {
                scrollPane.setVvalue(Double.MAX_VALUE);
            }
        });

        root.getChildren().addAll(scrollPane, button, button2, button3);
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
    }
}