How to get touch position to corresponding item in .NET MAUI ListView (SfListView)?
You can get the position relative to the ListViewItem by getting its Y position and calculating the position by subtracting that Y-position value from the position you received in the ItemTapped,ItemDoubleTapped or ItemLongPressed event.
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;
}
Output
Download the complete sample from GitHub.
Conclusion
I hope you enjoyed learning how to touch the position to the corresponding item in the .NET MAUI ListView.
You can refer to our .NET MAUI ListView feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started with configuration specifications.
For current customers, check out our components from the License and Downloads page. If you are new to Syncfusion®, try our 30-day free trial to check out our other controls.
Please let us know in the comments section if you have any queries or require clarification. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!