SFListview Tapped Event and Tapped Command not returning correct value for Position
Hi,
We are using the MAUI SFListView and need to find out exactly what position within item it was tapped for our App.
According to your documentation (MVVM Page) it says the ItemTappedEvent.Position property should be the 'position in the item' - however it's instead returning the overall display screen coordinate which is not helpful.
I can't find any information on how to resolve this back to the item itself and determine the 'Position within the item' (which is what we need).
Is this a bug in the control or incorrect documentation on your site?
How do we find out the position from this event?
thanks
Niall
Hi niall,
We have changed the way the position is retrieved in the ItemTapped event to reflect the relative screen position instead of the item position, similar to how it worked in Xamarin. We apologize for not updating the documentation to reflect this change. We will take immediate action to update it accordingly.
If you want to get the position relative to the ListView item, you can do so by getting the Y position of the ListView item and calculating the position by subtracting the Y value from the position you receive in the ItemTapped event.
Please refer the following code snippet:
|
private void listView_ItemTapped(object sender, Syncfusion.Maui.ListView.ItemTappedEventArgs e) { var listViewItemYPosition = GetListViewItemPosition(GetListViewItem(e.DataItem)); var positionYBasedOnItem = e.Position.Y - listViewItemYPosition; } private object GetListViewItem(object obj) { var item = obj as SimpleModel; var Layout = listView.ItemsLayout as LinearLayout; var rowItems = Layout!.GetType().GetRuntimeFields().First(p => p.Name == "items").GetValue(Layout) as IList;
foreach (ListViewItemInfo iteminfo in rowItems) { if (iteminfo.Element != null && iteminfo.DataItem == item) { itemView = iteminfo.Element as View; return itemView; } }
return itemView; }
private double GetListViewItemPosition(object obj) { var platformView = (obj as ListViewItem)!.Handler!.PlatformView; #if WINDOWS var native = platformView as Microsoft.UI.Xaml.UIElement; var point = native!.TransformToVisual(null).TransformPoint(new Windows.Foundation.Point(0, 0)); return point.Y; #elif ANDROID var native = platformView as Android.Views.View; var point = new int[2]; native!.GetLocationOnScreen(point); return point[1] / DeviceDisplay.MainDisplayInfo.Density; #elif IOS var native = platformView as UIKit.UIView; var point = native!.ConvertPointToView(new CoreGraphics.CGPoint(0, 0), null); return point.Y; #endif return 0; } |
We have also attached a working sample for your reference. Please take a look at the sample, and feel free to let us know if you have any further questions.
Regards,
Jayashree
Attachment: ListViewMaui_3e8f0c74.zip
- 1 Reply
- 2 Participants
-
NI niall
- Oct 10, 2024 09:34 AM UTC
- Oct 11, 2024 10:30 AM UTC