Detect where ListViewItem has been clicked/pressed

╄→尐↘猪︶ㄣ 提交于 2019-12-02 07:55:54

问题


In Delphi XE8 using a Firemonkey TListView.

I have a ListView with about 5 items in it. Each item has an image within them.

How would one detect when the image is clicked/pressed?.

I've been looking at the procedure:

OnItemClickEx

But I do not understand how to use it. Wasn't sure if this is what I need to use or not.

Any help would be great.

Thanks,


回答1:


Set Listview item image object properties....

procedure TForm1.OnFormCreate(Sender:TObject)
begin
  ListView1.ItemAppearanceObjects.ItemObjects.Image.Align :=  TListItemAlign.Leading;
  ListView1.ItemAppearanceObjects.ItemObjects.Image.VertAlign := TListItemAlign.Center;
  ListView1.ItemAppearanceObjects.ItemObjects.Image.PlaceOffset.X := 370;
end;

Then in the ItemClickEx procedure I did the following:

procedure TForm1.ListView1ItemClickEx(const Sender: TObject;
ItemIndex: Integer; const LocalClickPos: TPointF;
const ItemObject: TListItemObject);
begin
 if (LocalClickPos.X > ListView1.ItemAppearanceObjects.ItemObjects.Image.PlaceOffset.X) and
 (LocalClickPos.X < (ListView1.ItemAppearanceObjects.ItemObjects.Image.PlaceOffset.X + ListView1.ItemAppearanceObjects.ItemObjects.Image.Width)) and
 (LocalClickPos.Y > ListView1.ItemAppearanceObjects.ItemObjects.Image.PlaceOffset.Y) and
 (LocalClickPos.Y < (ListView1.ItemAppearanceObjects.ItemObjects.Image.PlaceOffset.Y + ListView1.ItemAppearanceObjects.ItemObjects.Image.Height)) then
  begin
    ShowMessage('Image Pressed!;)
  end;
end;



回答2:


on the ListView1ItemClickEx event add

 if ItemObject is TListItemImage then
    ShowMessage('Image Pressed!)



回答3:


You are allready looking at the right procedure. You have to check the class of the parameter ItemObject there:

if (ItemObject<>nil) and (ItemObject.InheritsFrom(TListItemImage)) then
    //...

This is the minimal example which works with RAD Studio 10.1 Berlin:

Create a new multi device application and place a TlistView on the main form. Add the following event handler:

procedure TForm1.FormCreate(Sender: TObject);
begin
  ListView1.ItemAppearance.ItemAppearance := 'ImageListItemBottomDetail';
  ListView1.Items.Add;
end;

procedure TForm1.ListView1ItemClickEx(const Sender: TObject; ItemIndex:  Integer;
  const LocalClickPos: TPointF; const ItemObject: TListItemDrawable);
begin
  if (ItemObject<>nil) and (ItemObject.InheritsFrom(TListItemImage)) then
    ShowMessage('image clicked');
end;



回答4:


procedure TfmReference.ListView1UpdateObjects(const Sender: TObject;
 const AItem: TListViewItem);
begin
 AItem.Objects.FindObjectT<TListItemImage>('Image4').Bitmap := ImageList1.Source.Items[0].MultiResBitmap[0].Bitmap;
end;



回答5:


procedure TForm1.ListView1ItemClickEx(const Sender: TObject; ItemIndex:  Integer;
  const LocalClickPos: TPointF; const ItemObject: TListItemDrawable);
begin
ShowMessage('clicked: ' + ItemObject.Name); 
// ItemObject.Name is the name of the oject that is clicked in the ListView row.

//If just looking for type of object:
if ItemObject is TListItemAccessory then ShowMessage('Acessory clicked');

if ItemObject is TListItemImage then ShowMessage('Image clicked: ' + ItemObject.Name);

if ItemObject is TListItemText then ShowMessage('text clicked');
end;


来源:https://stackoverflow.com/questions/39099708/detect-where-listviewitem-has-been-clicked-pressed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!