gpt4 book ai didi

listview - JavaFX ListView 带有用于上下滚动的触摸事件

转载 作者:行者123 更新时间:2023-12-04 02:22:39 25 4
gpt4 key购买 nike

我想用手势实现一个可滚动的 ListView,就像在手机和平​​板电脑中一样,用手指上下滚动我的列表。但是我当前的列表会在我点击列表后立即选择一个项目。我怎样才能做到这一点?我在 Oracle 教程中找不到任何示例。

private ObservableList<Document> items = FXCollections.observableArrayList();
@FXML ListView<Document> listView;

{
...

listView.setItems(items);
listView.getStylesheets().add("style/listview.css");
listView.setStyle("-fx-background-insets: 0 ;"); // remove 1px border of listview container

listView.setCellFactory(new Callback<ListView<Document>, ListCell<Document>>() {
@Override
public ListCell<Document> call(ListView<Document> listView) {
return new DocumentArrayAdapter();
}
});

...
}

public void loadListView(List<Document> ldoc){
if (!ldoc.isEmpty()){
items.addAll(ldoc);
}
}

最佳答案

这是我做的

public class CustomListCell extends ListCell<Document>{

private double lastYposition = 0;

public CustomListCell(){

setOnMousePressed(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
lastYposition = event.getSceneY();
}
});

setOnMouseDragged(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
double newYposition = event.getSceneY();
double diff = newYposition - lastYposition;
lastYposition = newYposition;
CustomScrollEvent cse = new CustomScrollEvent();
cse.fireVerticalScroll((int)diff, DocumentArrayAdapter.this, (EventTarget) event.getSource());
}
});


package myproject.utils;

import javafx.event.Event;
import javafx.event.EventTarget;
import javafx.scene.input.ScrollEvent;

public class CustomScrollEvent {

public void fireVerticalScroll(int deltaY, Object source, EventTarget target){

ScrollEvent newScrollEvent = null;
newScrollEvent = new ScrollEvent(source,
target,
ScrollEvent.SCROLL,
0,
0,
0,
0,
false,
false,
false,
false,
false,
false,
0,
deltaY,
0,
0,
ScrollEvent.HorizontalTextScrollUnits.CHARACTERS,
0,
ScrollEvent.VerticalTextScrollUnits.NONE,
deltaY,
0,
null);

Event.fireEvent(target, newScrollEvent);
}
}

虽然我已经在我自己的 ListCell 中实现了监听器,但我想它也可以直接在 ListView 上实现监听器,使用 listView.setOnMousePressed 和 listView.setOnMouseDragged

关于listview - JavaFX ListView 带有用于上下滚动的触摸事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26537548/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com