How to implement long press delete functionality as like in gmail mobile app.

Hi Team,

I am trying to implement long press delete functionality, same like as in gmail app. tried to figure out for inputs but I didn't get exactly.
Could you please share your references if anyone worked on this.

Big thanks in advance,

Regards,
Naveen.

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai

    Try to add Effect to control to achieve LongPress function.

    1.Create the shared Effect in shared code.

    public class LongPressedEffect : RoutingEffect
    {
        public LongPressedEffect() : base("App19F_2.LongPressedEffect")
        {
    
        }
        public static readonly BindableProperty CommandProperty = BindableProperty.CreateAttached("Command", typeof(ICommand), typeof(LongPressedEffect), (object)null);
        public static ICommand GetCommand(BindableObject view)
        {
            return (ICommand)view.GetValue(CommandProperty);
        }
        public static void SetCommand(BindableObject view, ICommand value)
        {
            view.SetValue(CommandProperty, value);
        }
    
        public static readonly BindableProperty CommandParameterProperty = BindableProperty.CreateAttached("CommandParameter", typeof(object), typeof(LongPressedEffect), (object)null);
        public static object GetCommandParameter(BindableObject view)
        {
            return view.GetValue(CommandParameterProperty);
        }
        public static void SetCommandParameter(BindableObject view, object value)
        {
            view.SetValue(CommandParameterProperty, value);
        }
    }
    

    2.Create a subclass of the PlatformEffect class to invoke the method.

    [assembly: ResolutionGroupName("App19F_2")]
    [assembly: ExportEffect(typeof(AndroidLongPressedEffect), "LongPressedEffect")]
    namespace App19F_2.Droid
    {
        public class AndroidLongPressedEffect : PlatformEffect
        {
            private bool _attached;
    
            public static void Initialize() { }
    
            public AndroidLongPressedEffect()
            {
            }
    
            protected override void OnAttached()
            {
                if (!_attached)
                {
                    if (Control != null)
                    {
                        Control.LongClickable = true;
                        Control.LongClick += Control_LongClick;
                    }
                    else
                    {
                        Container.LongClickable = true;
                        Container.LongClick += Control_LongClick;
                    }
                    _attached = true;
                }
            }
    
            private void Control_LongClick(object sender, Android.Views.View.LongClickEventArgs e)
            {
                Console.WriteLine("Invoking long click command");
    
                var command = LongPressedEffect.GetCommand(Element);
                command?.Execute(LongPressedEffect.GetCommandParameter(Element));
            }
    
            protected override void OnDetached()
            {
                if (_attached)
                {
                    if (Control != null)
                    {
                        Control.LongClickable = true;
                        Control.LongClick -= Control_LongClick;
                    }
                    else
                    {
                        Container.LongClickable = true;
                        Container.LongClick -= Control_LongClick;
                    }
                    _attached = false;
                }
            }
        }
    }
    

    3.page.xaml

    <Label x:Name="label" Text="Long Press Me!" local:LongPressedEffect.Command="{Binding delete_command}">
        <Label.Effects>
            <local:LongPressedEffect />
        </Label.Effects>
    </Label>
    

    For more details, check the link:
    https://alexdunn.org/2017/12/27/xamarin-tip-xamarin-forms-long-press-effect/

  • NMackayNMackay GBInsider, University mod
Sign In or Register to comment.