kittu
kittu

Reputation: 265

How to find long pressed selected item from list view for long press event using MR.Gestures in Xamarin form

I m using MR.Gestures for long press event on list view. I m able to trigger long press event but not able to get listview selected long press item.

 <mr:ListView  x:Name="listView"
    ItemsSource="{Binding downloadedLessonsData}"
    LongPressedCommand="{Binding LongPressedCommand}"
    LongPressingCommandParameter="{Binding SelectedItem}"
    >

In viewmodel code, OnLongPressed method is getting fired on long press of listview item:

  protected virtual void OnLongPressed(LongPressEventArgs e)
    {
     MR.Gestures.ListView list=   (MR.Gestures.ListView) (e.Sender);
     //list.LongPressedCommandParameter; LongPressedCommandParameter is 
     //comming null, unable to get selectedItem
     Debug.WriteLine("OnLongPress");
     }

But I m unable to get selected long pressed item in OnLongPressed method. Please suggest how to get selected long pressed list item in OnLongPressed method. I m new to xamarin forms. Thanks in advance.

Upvotes: 0

Views: 3143

Answers (2)

Michael Rumpler
Michael Rumpler

Reputation: 335

You can either use listView.SelectedItem (as the selected item will be set when you touch a cell) or you handle LongPress on each Cell. Then you get the pressed Cell in the sender.

In your code the signature of the OnLongPressed method is wrong, but if it gets executed, then this is just a copy&paste error.

Upvotes: 0

Renjith
Renjith

Reputation: 682

Your casting for selected item MR.Gestures.ListView list=(MR.Gestures.ListView) (e.Sender);

is wrong. you have to cast it to model if each selected item is a collection.

protected virtual void OnLongPressed(LongPressEventArgs e) { var selectedItem=(Model name be be given)e.Sender; ' //now you will get the selected model in selectedItem' }

you have to cast it to datatype if each selected item is a single value(eg: string).

protected virtual void OnLongPressed(LongPressEventArgs e) { var selectedItem=(string)e.Sender; ' //now you will get the selected string in selectedItem' }

Upvotes: 1

Related Questions