Sorry I posted this in General Discussion group:
I'm attempting to write my own event listener and renderer so that I can know when a user is doing a long tap and hold on a row in a ListView
Starting from this guide:
http://arteksoftware.com/gesture-recognizers-with-xamarin-forms/
I created a listener in my android project like so:
`
public interface IGestureListenerHost
{
void OnLongPress();
}
public class MyGestureListener : GestureDetector.SimpleOnGestureListener
{
IGestureListenerHost _host;
public MyGestureListener(IGestureListenerHost host)
{
_host = host;
}
public override void OnLongPress(MotionEvent e)
{
Console.WriteLine("OnLongPress");
_host.OnLongPress();
base.OnLongPress(e);
}
public override bool OnDoubleTap(MotionEvent e)
{
Console.WriteLine("OnDoubleTap");
return base.OnDoubleTap(e);
}
// etc.
`
Next I started to create my renderer like so:
` class MyGridViewCellFrameRenderer : FrameRenderer, IGestureListenerHost
{
MyGestureListener _listener;
GestureDetector _detector;
MyGridViewCellFrame _frame;
public MyGridViewCellFrameRenderer()
{
_listener = new MyGestureListener(this);
_detector = new GestureDetector(_listener);
}
public void OnLongPress()
{
if (_frame != null)
_frame.OnLongPress();
}
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Frame> e)
{
base.OnElementChanged(e);
_frame = this.Element as MyGridViewCellFrame;
if (e.NewElement == null)
{
this.GenericMotion -= HandleGenericMotion;
this.Touch -= HandleTouch;
}
if (e.OldElement == null)
{
this.GenericMotion += HandleGenericMotion;
this.Touch += HandleTouch;
}
}
void HandleTouch(object sender, Android.Views.View.TouchEventArgs e)
{
_detector.OnTouchEvent(e.Event);
}
void HandleGenericMotion(object sender, Android.Views.View.GenericMotionEventArgs e)
{
_detector.OnTouchEvent(e.Event);
}
`
The idea is that my ListView will be hosting custom ViewCell's whose content is a Frame descendant, MyGridViewCellFrame
After running the app I found that my ListView is not getting tap events to select the current row. I found that because I wired up the GenericMotion and Touch event handlers, that it is somehow intercepting the events.
What I tried next was to remove the event handlers and instead override OnTouchEvent like so:
public override bool OnTouchEvent(MotionEvent e)
{
_detector.OnTouchEvent(e);
return base.OnTouchEvent(e);
}
This appeared to work. My ListView is properly getting the tap events to select the current row. However, I then found that the OnLongPress event was being fired in the listener even when i did a quick tap on the ListView. It's as if the renderer is not seeing tap-up events so the detector is not letting the listener know to stop the long-pause event from firing? Any ideas and help is greatly appreciated!