Firemonkey ListView 的點擊事件一直讓人摸不着頭緒(各平台觸發規則不太相同),因為它提供了點擊相關的事件就有如下:
- OnChange:改變項目觸發。
- OnClick:點擊觸發。
- OnItemClick:點擊項目觸發
- Windows 平台:按下立即觸發,放開后接着觸發 OnItemClickEx。
- Android 平台:按下立即觸發,不用放開接着 OnItemClickEx(按鈕 Button 觸發順序與 Widnows 相同,要放開才會觸發 OnItemClickEx)。
- 下列以這二個事件為例:
- OnItemClickEx:項目內單項觸發。
- OnButtonClick:按鈕事件。
下例將 Item.Apperance 設定為 Custom。
可獲取每一個單項的事件觸發:
參考代碼:
procedure TForm1.ListView1ItemClickEx(const Sender: TObject; ItemIndex: Integer; const [Ref] LocalClickPos: TPointF; const ItemObject: TListItemDrawable); begin if ItemObject is TListItemText then Label1.Text := 'OnItemClickEx_Text_' + ItemIndex.ToString else if ItemObject is TListItemImage then Label1.Text := 'OnItemClickEx_Image_' + ItemIndex.ToString else if ItemObject is TListItemAccessory then Label1.Text := 'OnItemClickEx_Accessory_' + ItemIndex.ToString; end; procedure TForm1.ListView1ButtonClick(const Sender: TObject; const AItem: TListItem; const AObject: TListItemSimpleControl); begin if AObject is TListItemGlyphButton then Label1.Text := 'OnButtonClick_GlyphButton_' + AItem.Index.ToString else if AObject is TListItemTextButton then Label1.Text := 'OnButtonClick_TextButton_' + AItem.Index.ToString; end;
有一些問題存在:
- 點擊 Accessory 后是觸發 Text 而非 Accessory,這部份我有改動到源碼,使它能正常辨別是點擊到那一個(改動源碼並不建議,有興趣自行研究)。