This is a migrated thread and some comments may be shown as answers.
ListView - ItemLongPress
1 Answer 39 Views
This is a migrated thread and some comments may be shown as answers.
Matt
Top achievements
Rank 1
Matt asked on 10 Jan 2018, 03:51 PM

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);
        }

1 Answer, 1 is accepted

Sort by
0
Accepted
Tsvyatko
Telerik team
answered on 15 Jan 2018, 12:04 PM
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
Asked by
Matt
Top achievements
Rank 1
Answers by
Tsvyatko
Telerik team
Share this question
or