大家。我完成了以下任务:我需要为listview实现滑动。我尝试使用此question(this答案)中的示例。但它对我不起作用。我尝试将ScrollEvent
用于此目的。这是我的代码:
public class SwipeEventHandler implements EventHandler<MouseEvent> {
private final ListView<Long> list;
private double x;
private double y;
public SwipeEventHandler(final ListView<Long> list) {
this.list = list;
this.x = 0;
this.y = 0;
}
@Override
public void handle(final MouseEvent e) {
final EventType<? extends MouseEvent> type = e.getEventType();
if (type == MouseEvent.MOUSE_PRESSED) {
set(e);
} else if (type == MouseEvent.MOUSE_DRAGGED) {
final double deltaX = getX() - getX(e);
final double deltaY = getY() - getY(e);
set(e);
final EventTarget source = e.getTarget(); // cell
final EventTarget target = (EventTarget) e.getSource(); // listview
final ScrollEvent event = new ScrollEvent(
source,
target,
ScrollEvent.SCROLL,
0,
0,
0,
0,
e.isShiftDown(),
e.isControlDown(),
e.isAltDown(),
e.isMetaDown(),
false,
false,
deltaX,
deltaY,
0,
0,
ScrollEvent.HorizontalTextScrollUnits.NONE,
deltaX,
ScrollEvent.VerticalTextScrollUnits.NONE,
deltaY,
1,
null);
Event.fireEvent(target, event);
} else {
setX(0);
setY(0);
}
}
private double getY(final MouseEvent e) {
return e.getSceneY();
}
private double getX(final MouseEvent e) {
return e.getSceneX();
}
private void set(final MouseEvent e) {
System.out.printf("%s-%s %s-%s %s\r\n", getX(), e.getSceneX(),
getY(), e.getSceneY(), e.getEventType().getName());
setX(e.getSceneX());
setY(e.getSceneY());
}
// getters and setters
但是当我“拖动”列表视图时没有任何反应。谁能解释一下我做错了什么?
更新
当我说“没有任何反应”时,我的意思是我的代码被调用,但没有执行列表滚动。我试图将源,目标,坐标改为另一个值,我找不到这个参数的正确组合。