question

Hamittirpan-3199 avatar image
0 Votes"
Hamittirpan-3199 asked Hamittirpan-3199 commented

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

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


dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JessieZhang-2116 avatar image
1 Vote"
JessieZhang-2116 answered Hamittirpan-3199 commented

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @JessieZhang-2116

Thank you very much for your quick reply. Based on the documents you shared, I decided it was the best way to write Custom Builders for StackLayout in my ListView. And I wrote a custom builder like this one.

These codes can be useful for developers who want to use long press action in ListView. (There was a problem while uploading to the site in txt format. That's why I could only upload it in pdf format :) )


82355-listview-long-press-renderer.pdf


0 Votes 0 ·