How to get the coordinates of the selected item as a list in Xamarin.Forms?

I want to get the coordinates (rectangle borders: x, y, width and height) of the selected item in the list view relative to the screen (suppose the listview fills the entire screen), so I can create an object at this location and animate it to display some details selected item in my Xamarin.Forms application.

listview in xaml:

<ListView ItemTapped="ItemTapped"
    AbsoluteLayout.LayoutFlags="All"
    AbsoluteLayout.LayoutBounds="0.5, 0.5, 1.0, 1.0">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell Height="50">              
                <AbsoluteLayout>
                    <Label Text="{Binding Info}"
                        AbsoluteLayout.LayoutFlags="All"
                        AbsoluteLayout.LayoutBounds="0.1, 0.5, 0.7, 0.5"/>
                </AbsoluteLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

C # code for ItemTapped event:

void ItemTapped(object sender, EventArgs args)
{
    var listView = (ListView)sender; // the listview
    var selectedItem = args.Item; // the selected item

    // need to get selected item coordinates for the animation
    var selectedItemBounds = ...

    ...
}

In the end, I want to create something like this in Xamarin.Forms with listview (the number of objects in the list changes):

sample listview animation

+4
source share

Source: https://habr.com/ru/post/1694829/


All Articles