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



我正在使用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;

最新更新