Firemonkey 如何将 longTap 手势添加到运行时制作的 ListBoxItems

Firemonkey how to add longTap gesture to runtime made ListBoxItems

我正在使用 Delphi 10 Seattle 使用 firemonkey 构建一个多设备项目。

我的项目有一个 ListBox,我在运行时用 ListBoxItems 填充它。我想将 LongTap 手势添加到 ListBoxItems。

我已经试过了:

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

但是没有调用 onGesture 方法。如果我将 gestureManager 添加到设计器中的表单并调用相同的 onGesture 方法,它确实会被调用。

手势不适用于 ScrollBox 和后代中的控件(我不知道,为什么)。您应该使用 ListBox.TouchListBox.OnGesture 并分析 Selected 属性:

  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;

或者,更复杂的方法 - 通过手势位置查找项目:

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;

正确的解决方案要平庸得多:

for i:=0 to pred(ListBox1.items.count)do
begin
  ListBox1.ItemByIndex(i).Touch.GestureManager:=GestureManager1;
  ListBox1.ItemByIndex(i).Touch.InteractiveGestures :=
  [TInteractiveGesture.LongTap, TInteractiveGesture.DoubleTap];
  ListBox1.ItemByIndex(i).OnGesture := ListBoxitemGesture;
  ListBox1.ItemByIndex(i).HitTest:=true;
end;