I have used Pointer.Listener and Mouse.Listener with ImageLayer and it worked. But when I use Pointer.Listener to a GroupLayer, It doesnt listens the clicks or touches. How can I do this?

I am using the following code.

myGroupLayer.addListener(new Pointer.Listener() {
  @Override
  public void onPointerEnd(Event event) {
    System.out.println("click registered");
    myMethodToRun();
  }
  @Override
  public void onPointerDrag(Event event) { }
  @Override
  public void onPointerStart(Event event) { }
});
link|improve this question

If you're unable to attach a listener to a GroupLayer, an alternative approach would be to set a listener on the pointer and then trigger the event on condition of a collision between pointer event location and the GroupLayer. An example of concept can be found here: PlaynDev.java – klenwell May 18 at 3:00
feedback

1 Answer

up vote 0 down vote accepted

A GroupLayer will never itself be "hit" by a mouse click. One of the children of the GroupLayer could be hit, but that won't trigger a listener registered on the group layer, only on the child itself.

If you have a GroupLayer that contains multiple children, and you want the event to be dispatched to the GroupLayer's listener when any of those children are clicked, then you have to use a custom hit tester on the GroupLayer:

myGroupLayer.setHitTester(new Layer.HitTester() {
  public Layer hitTest(Layer l, Point p) {
    Layer hitLayer = myGroupLayer.hitTestDefault(p);
    return (hitLayer != null) ? myGroupLayer : null;
  }
});

This will cause your GroupLayer to hit test all of its children, and if any of the children are hit, it returns itself as the hit layer. Then listeners registered on your group layer will be run as if the group layer itself were clicked.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.