Search code examples
delphimobilefiremonkeygesturedelphi-10-seattle

Firemonkey how to add longTap gesture to runtime made ListBoxItems


I'm using Delphi 10 Seattle to build a multi device project with firemonkey.

My project has a ListBox, and I fill it runtime with ListBoxItems. I want to add the LongTap gesture to the ListBoxItems.

I have already tried this:

gestureManager := TGestureManager.Create(nil);
listBoxItem.Touch.GestureManager := gestureManager;
listBoxItem.Touch.InteractiveGestures := [TInteractiveGesture.LongTap];
listBoxItem.OnGesture := ListBoxItemGesture;

But the onGesture method doesn't get called. If I add the gestureManager to the Form in the designer and call the same onGesture method it does get called.


Solution

  • Gestures don't work with controls inside ScrollBox and descendants (I don't know, why). You should use ListBox.Touch, ListBox.OnGesture and analyze Selected property:

      ListBox1.Touch.GestureManager := FmyGestureManager;
      ListBox1.Touch.InteractiveGestures := [TInteractiveGesture.LongTap];
      ListBox1.OnGesture := ListBox1Gesture;
    
    
    
    procedure THeaderFooterForm.ListBox1Gesture(Sender: TObject; const EventInfo: TGestureEventInfo; var Handled: Boolean);
    begin
      if (Sender = ListBox1) and Assigned(ListBox1.Selected) then
        begin
          lblMenuToolBar.Text := 'Handled' + ListBox1.Selected.Text;
          Handled := True;
        end;
    end;
    

    Or, more complex method - find item by gesture location:

    procedure THeaderFooterForm.ListBox1Gesture(Sender: TObject; const EventInfo: TGestureEventInfo; var Handled: Boolean);
    var
      c: IControl;
      ListBox: TListBox;
      lbxPoint: TPointF;
      ListBoxItem: TListBoxItem;
    begin
      c := ObjectAtPoint(EventInfo.Location);
      if Assigned(c) then
        if Assigned(c.GetObject) then
          if c.GetObject is TListBox then
            begin
              ListBox := TListBox(c.GetObject);
              lbxPoint := ListBox.AbsoluteToLocal(EventInfo.Location);
    
              ListBoxItem := ListBox.ItemByPoint(lbxPoint.X, lbxPoint.Y);
              if Assigned(ListBoxItem) then
                lblMenuToolBar.Text := 'Handled ' + ListBoxItem.Text;
              Handled := True;
            end;
    end;