2

everyone. I got the following task: I need to implement swipe for listview. I've tried to use example from this question (this answer). But it doesn't work for me. I try to use ScrollEvent for this purposes. Here my code:

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

But nothing happens when I 'Drag' over listview. Could anyone explain me what i'm doing wrong?

UPDATE

When I said "nothing happens" I mean that my code called, but no list scroll performed. I've tried to change source, target, coordinates to another values, bit I didn't found right combination of this params.

ZhenyaM
  • 676
  • 1
  • 5
  • 15
  • What does this "nothing happens" mean? Is your code even called? – M. Prokhorov Nov 28 '17 at 11:04
  • @M.Prokhorov, add update section for your question. Thanks for reply – ZhenyaM Nov 28 '17 at 11:43
  • I'm guessing that nothing happens is because your reference answer related to vertical scrolling - something which ListView supports natively. I don't think your list view supports swipes natively. – M. Prokhorov Nov 28 '17 at 11:49
  • Yes, you are right. But i need swipes by mouse drag. So, I need some solution which translate "mouse move" to scroll move or some another... any solution=) – ZhenyaM Nov 28 '17 at 12:01
  • What do you want to happen when the user swipes on the list? Have you tried registering a handler for scroll events with the list view? It's not really clear why you are translating this to a scroll event anyway; why not just use the drag event to initiate the behavior you want? – James_D Nov 28 '17 at 13:00
  • 1
    I think what you intend to do is a list where each item is swipable and shows some actions. This means what you actually want is a list with custom cellfactory ([here](https://stackoverflow.com/a/15691104/7470253) is a pretty good start), where each item should support a [SwipeEvent](https://docs.oracle.com/javase/8/javafx/api/javafx/scene/input/SwipeEvent.html) (or possibly ScrollEvents produced as a result of following the swipe). – M. Prokhorov Nov 28 '17 at 13:02
  • I think I give bad explanation what I want. I want work with list such as with list ob the phobe, but by mouse for desktop. I couldn't found any implementation of swipe directly (my listeners for `onSwipeUp()` etc. was ignored). So, I've tried to convert `MouseEvent.MOUSE_DRAGGED` event to scroll. Of course, handler, described before, added to my `ListView`. – ZhenyaM Nov 28 '17 at 13:46
  • @James_D, what do you mean by "just use drag event"? If drag of cells than how it can be in listview? If you mean `ListView` than I can't understand how it can work at whole. Simply, my question is how to drag list cells by mouse click and drag, not by mouse wheel. – ZhenyaM Nov 28 '17 at 13:50
  • I just wondered why you were firing a scroll event. Presumably you want something to happen when the user does this, right? (You still haven't told us what.) So why not just implement the code for whatever it is you want to happen in your mouse drag listener, instead of firing a scroll event (and then presumably you are going to register a listener for the scroll event???). Anyway, the strategy outlined by @M.Prokhorov is the correct one; register listeners with each cell, using a cell factory. – James_D Nov 28 '17 at 13:54
  • @James_D, that's assuming he does want the swipe. I'm still not clear of behavior he wants. Maybe it's just list scrolling via touch events, like on mobile (but with JavaFx for whatever reason). – M. Prokhorov Nov 28 '17 at 14:18
  • @M.Prokhorov Yes, I subsequently wondered whether he was referring to "panning", rather than "swiping". Panning is supported directly in scroll panes, but not in virtualized controls that manage their own scrolling, AFAIK. – James_D Nov 28 '17 at 14:21

0 Answers0