简体   繁体   中英

Does Xamarin.Forms support tap and hold gesture?

I am new to Xamarin.forms, my client wants a feature like Gmail where in user can tap and hold on one of list item and get an option to multi-select items.

The app will have a list of items with different options available like delete, view, upload etc. SO basically it has 5 options and as per windows mobile limitation the app cannot have more than 4 menu options(ToolbarItem). And hence the need for the tap and hold gesture. Once the user tap and holds one of the item, the ToolbarItem sholud change and replace with only delete option. By doing this we can reduce ToolbarItem to four.

Any references will be of great help!! :-)

Also would like to know if tap and hold is possible then how does different platform(iOS,windows,android) will render it? Will it be handled by Xamarin.forms or there is something in the code which has to be taken care of for different OS platforms?

Have you considered using Context Options instead of replacing the options in the toolbar?

If you can use Context Options instead of the toolbar, you don't need 3rd party component, as Xamarin.Forms allows you to define such options for each listView item easily:

To instanciate your ListView

var listView = new ListView();
listView.ItemTemplate = new DataTemplate(typeof(MyListItemCell));

And the data template should look like this:

public class MyListItemCell : ViewCell
{
    // To register the LongTap/Tap-and-hold gestures once the item model has been assigned
    protected override void OnBindingContextChanged()
    {
        base.OnBindingContextChanged();
        RegisterGestures();
    }

    private void RegisterGestures()
    {
        var deleteOption = new MenuItem()
        {
            Text = "Delete",
            Icon = "deleteIcon.png", //Android uses this, for example
            CommandParameter = ((ListItemModel) BindingContext).Id
        };
        deleteOption.Clicked += deleteOption_Clicked;
        ContextActions.Add(deleteOption);

        //Repeat for the other 4 options

    }
    void deleteOption_Clicked(object sender, EventArgs e)
    {
         //To retrieve the parameters (if is more than one, you should use an object, which could be the same ItemModel 
        int idToDelete = (int)((MenuItem)sender).CommandParameter; 
        //your delete actions
    }
    //Write the eventHandlers for the other 4 options
}

Unfortunately that event isn't in there by default. However there is a cheap 3rd party component that handles it for you: http://www.mrgestures.com/#events with LongPressing or LongPressed.

Or you have the option of implementing it natively on each platform.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM