XCT Long press doesn't work in the Listview or SwipeView control.

Hamittirpan 166 Reputation points
2021-03-29T06:58:11.733+00:00

Hi everyone !

I am using a detailed SwipeView control in Listview. I am trying to integrate the long press control on each card in my list. For this, I want to use xamarin community toolkit long press. Xct long press works in areas outside of listview on my screen. For example, label, image etc.

But xct long press does not work on any controls inside my listview. How can I solve this problem?

82227-xct-long-press-example.png
Thanks in advance

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,294 questions
0 comments No comments
{count} votes

Accepted answer
  1. JessieZhang-MSFT 7,706 Reputation points Microsoft Vendor
    2021-03-29T09:29:11.537+00:00

    Hello,

    Welcome to our Microsoft Q&A platform!

    You can use Custom Renderers to achieve this.

    In android:

     [assembly: ExportRenderer(typeof(CustomListView), typeof(CustomListViewDroid))]  
        namespace FormsListViewSample.Droid  
        {  
            public class CustomListViewDroid: ListViewRenderer  
            {  
          
                public CustomListViewDroid(Android.Content.Context context):base(context) {   
                  
                  
                }  
                protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.ListView> e)  
                {  
                    base.OnElementChanged(e);  
                    if (e.OldElement == null)  
                    {  
                        var view = (CustomListView)Element;  
          
                        this.Control.ItemLongClick += (s, args) =>  
                        {  
                            this.Control.SetItemChecked(args.Position, true);  
                            Java.Lang.Object item = this.Control.GetItemAtPosition(args.Position);  
                            view.OnLongClicked(item.GetType().GetProperty("Instance").GetValue(item));  
                        };  
                    }  
          
                }  
            }  
        }  
    

    In forms:

     public class CustomListView:ListView  
    {  
        public event EventHandler LongClicked;  
    
        public void OnLongClicked()  
        {  
            if (LongClicked != null)  
                LongClicked(this, new ItemTappedEventArgs(this, this.SelectedItem));  
        }  
    
        public void OnLongClicked(object v)  
        {  
            Log.Warning("test", "OnLongClicked is triggered");  
        }  
    }  
    

    Refer: https://forums.xamarin.com/discussion/1013/catching-long-presses-on-a-list-view

    And there is a similar thead about this, you can check it:
    https://stackoverflow.com/questions/62895490/long-press-event-on-listview-items-in-xamarin-forms

    https://www.c-sharpcorner.com/article/longpress-event-for-image/

    Best Regards,

    Jessie Zhang

    ---
    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful