ListView - ItemLongPress

2 posts, 1 answers
  1. Matt
    Matt avatar
    5 posts
    Member since:
    Aug 2017

    Posted 10 Jan Link to this post

    I'm looking for a way to get an ItemLongPress similar to the ItemTapped that is provided for android only (but accessible in through Forms).

    I tried making a custom renderer and it is being used (I put a breakpoint in an override of the OnElementChanged in the customer renderer and it's being called), but I'm not getting any touch events.  I tried overriding the OnTouchEvent (breakpoint never hits) and I tried Control.SetOnLongClickListener inside the OnElementChanged event (after setting LongClickable = true), but the breakpoint in my new Long Click Listener never hits. 

    Code snippets from the custom renderer (not entire code, but this is where I've set breakpoints):

            protected override void OnElementChanged(Xamarin.Forms.Platform.Android.ElementChangedEventArgs<Telerik.XamarinForms.DataControls.RadListView> e)
            {
                base.OnElementChanged(e);
                if (e.NewElement != null) {
                    if (Control != null) {
                        Control.Clickable = true;
                        Control.SetOnTouchListener(RadTouchListener.Instance.Value);
                        Control.LongClickable = true;
                        Control.SetOnLongClickListener(RadLongClickListener.Instance.Value);
                    }
                }
            }

     

            private class RadLongClickListener : Java.Lang.Object, Android.Views.View.IOnLongClickListener
            {
                public static readonly Lazy<RadLongClickListener> Instance = new Lazy<RadLongClickListener>(() => new RadLongClickListener());

                public bool OnLongClick(Android.Views.View v)
                {
                    var renderer = v.Tag as ExtendedRadListRenderer;
                    ((ILongPressController)renderer?.Element)?.SendLongClicked();
                    return true;
                }
            }

     

      public override bool OnTouchEvent(MotionEvent e)
            {
                return base.OnTouchEvent(e);
            }

  2. Answer
    Tsvyatko
    Admin
    Tsvyatko avatar
    863 posts

    Posted 15 Jan Link to this post

    Hello Matt,

    Thank you for contacting us! In order to enable the listview long-press you would need the following code in the custom renderer:
    Control.ListView.AddItemClickListener(new RadLongClickListener(this));


    I have prepared sample project demonstrating how this can be achieved. Check the attachment and let us know if this helps.

    Regards,
    Tsvyatko
    Progress Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top