Xamarin Forms: SwipeGestureRecognizer is not working on android platform

Sreejith Sreenivasan 1,001 Reputation points
2023-06-16T11:46:29.19+00:00

The SwipeGestureRecognizer is not working on the android platform and I found a lot of threads related to that.

I found an alternative solution here and this is the corresponding thread. All the swipes are implemented using a SwipeListener.cs and ISwipeCallBack.cs. But it is only work with a label, when I tried it with a listview the swipe events are not firing.

Here is my sample project to reproduce the issue.

This solution is an old on, so something on the implementation is deprecated, I can't understand what it is. Is there any way to make the swipe work for listviews?

SwipeListener.cs

using System;
using Xamarin.Forms;

namespace SwipeLib
{
	public class SwipeListener : PanGestureRecognizer
	{
		private ISwipeCallBack mISwipeCallback;
		private double translatedX = 0, translatedY = 0;

		public SwipeListener(View view, ISwipeCallBack iSwipeCallBack)
		{
			mISwipeCallback = iSwipeCallBack;
			var panGesture = new PanGestureRecognizer();
			panGesture.PanUpdated += OnPanUpdated;
			view.GestureRecognizers.Add(panGesture);
		}

		void OnPanUpdated(object sender, PanUpdatedEventArgs e)
		{

			View Content = (View)sender;

			switch (e.StatusType) {

				case GestureStatus.Running:

					try {
						translatedX = e.TotalX;
						translatedY = e.TotalY;
					} catch (Exception err) {
						System.Diagnostics.Debug.WriteLine("" + err.Message);
					}
					break;

				case GestureStatus.Completed:

					System.Diagnostics.Debug.WriteLine("translatedX : " + translatedX);
					System.Diagnostics.Debug.WriteLine("translatedY : " + translatedY);

					if (translatedX < 0 && Math.Abs(translatedX) > Math.Abs(translatedY)) {
						mISwipeCallback.onLeftSwipe(Content);
					} else if (translatedX > 0 && translatedX > Math.Abs(translatedY)) {
						mISwipeCallback.onRightSwipe(Content);
					} else if (translatedY < 0 && Math.Abs(translatedY) > Math.Abs(translatedX)) {
						mISwipeCallback.onTopSwipe(Content);
					} else if (translatedY > 0 && translatedY > Math.Abs(translatedX)) {
						mISwipeCallback.onBottomSwipe(Content);
					} else {
						mISwipeCallback.onNothingSwiped(Content);
					}

					break;

			}
		}
	}
}

ISwipeCallBack.cs

using System;
using Xamarin.Forms;
namespace SwipeLib
{  
	public interface ISwipeCallBack
	{

		void onLeftSwipe(View view);
		void onRightSwipe(View view);
		void onTopSwipe(View view);
		void onBottomSwipe(View view);
		void onNothingSwiped(View view);
	}
}
Developer technologies | .NET | Xamarin
{count} votes

1 answer

Sort by: Most helpful
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 36,451 Reputation points Microsoft External Staff
    2023-06-20T09:31:48.6233333+00:00

    Hello,

    This is a known issue reported at GitHub-  [Android] Swipe gestures don't work on page with ListView · Issue #6863 · xamarin/Xamarin.Forms · GitHub. You could follow the progress at GitHub.

    And you could refer to the solution which is based on ScrollView and ScrollViewRenderer, then try to create a custom Listview, and implement the method in ListViewRenderer. Please refer to Customizing a ListView - Xamarin | Microsoft Learn.

    Best Regards,

    Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    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.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.