Xamrin Forms: swipe to delete (gesture) in ListView

I want to implement wipes to remove functionality in Xamrin Forms, for which I tried the following.

  • I wrote a custom renderer to represent the list, and in "OnElementChanged" the renderer was able to access the associated command in "CustomListView", and I can add this command to the Swipe gesture, as shown below.

    swipeGestureRecognizer = new UISwipeGestureRecognizer (() => { if (command == null) { Console.WriteLine ("No command set"); return;} command.Execute (null); }); 

However, I am having problems accessing a specific line (scroll bar), so I can make the button visible / hidden in the scroll bar in the list view. Can you recommend a way to implement the same?

+5
source share
2 answers

You can do something like this:

  protected override void OnElementChanged (ElementChangedEventArgs<ListView> e) { base.OnElementChanged (e); var swipeDelegate = new SwipeRecogniserDelegate (); swipeGestureRecognizer = new UISwipeGestureRecognizer { Direction = UISwipeGestureRecognizerDirection.Left, Delegate = swipeDelegate }; swipeGestureRecognizer.AddTarget (o => { var startPoint = swipeDelegate.GetStartPoint (); Console.WriteLine (startPoint); var indexPath = this.Control.IndexPathForRowAtPoint(startPoint); if(listView.SwipeCommand != null) { listView.SwipeCommand.Execute(indexPath.Row); } }); this.Control.AddGestureRecognizer (swipeGestureRecognizer); this.listView = (SwipableListView)this.Element; } 

SwipeRecogniserDelegate key. it is implemented like this:

 public class SwipeRecogniserDelegate : UIGestureRecognizerDelegate { PointF startPoint; public override bool ShouldReceiveTouch (UIGestureRecognizer recognizer, UITouch touch) { return true; } public override bool ShouldBegin (UIGestureRecognizer recognizer) { var swipeGesture = ((UISwipeGestureRecognizer)recognizer); this.startPoint = swipeGesture.LocationOfTouch (0, swipeGesture.View); return true; } public PointF GetStartPoint () { return startPoint; } } 
+4
source

Scrolling for deletion is now built into the Xamarin Froms ListView using ContextAction. Here is a basic tutorial on how to do this. It is very easy to implement.

http://developer.xamarin.com/guides/cross-platform/xamarin-forms/working-with/listview/

+5
source

Source: https://habr.com/ru/post/1202748/


All Articles