[mono-android] Gesture listener subclass no longer works with V4
John Murray
john at murray.gb.com
Fri Dec 30 06:18:25 EST 2011
The following code adapted from some stuff on Stackoverflow has served me
well up to now
I have made major adaptations to it but this code is the basic original -
I've stripped everything out and put it in the simple one button one
textview MonoDroid sample project but it behaves the same under v4 as my
main project viz if falls over with an unhandled exception on lines
public IntPtr Handle
{
get { throw new NotImplementedException(); }
}
In the OnCreate when the line
gestureScanner = new GestureDetector(this, gestureListener);
then as this instance of gesture detector is called it goes straight to the
IntPtr code and hangs
Presumably v4 is now enforcing some correct way of doing things and this
code is not acceptable - I cannot trace what's going on - as soon as the
onCreate is called it seems to go straight to this line and fall over - as I
said never happened until v4
I know it is probably my code but I couldn't get it much simpler - I have
probably mistranslated from the Java code I found on StackOverFlow and as I
am boilerplating I don't really understand deeply what's going on with the
Java Handle
Any help gratefully received
John Murray
///////////////////////////////////////////////////////
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Gestures;
namespace MonodroidC
{
[Activity(Label = "MonodroidC", MainLauncher = true, Icon =
"@drawable/icon")]
public class Activity1 : Activity
{
private TextView displayText;
private GestureDetector gestureScanner;
private GestureListener gestureListener;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
displayText = FindViewById<TextView>(Resource.Id.t11);
gestureListener = new GestureListener(displayText);
gestureScanner = new GestureDetector(this, gestureListener);
}
public override bool OnTouchEvent(MotionEvent e)
{
return gestureScanner.OnTouchEvent(e);
}
}
public class GestureListener : GestureDetector.IOnGestureListener
{
private readonly TextView view;
private static int SWIPE_MAX_OFF_PATH = 250;
private static int SWIPE_MIN_DISTANCE = 120;
private static int SWIPE_THRESHOLD_VELOCITY = 200;
public GestureListener(TextView view)
{
this.view = view;
}
public IntPtr Handle
{
get { throw new NotImplementedException(); }
}
public bool OnDown(MotionEvent e)
{
view.Text = "- DOWN -";
return true;
}
public bool OnFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY)
{
try
{
if (Math.Abs(e1.GetY() - e2.GetY()) > SWIPE_MAX_OFF_PATH)
return false;
// right to left swipe
if (e1.GetX() - e2.GetX() > SWIPE_MIN_DISTANCE &&
Math.Abs(velocityX) > SWIPE_THRESHOLD_VELOCITY)
Toast.MakeText(view.Context, "Left Swipe",
ToastLength.Short).Show();
else if (e2.GetX() - e1.GetX() > SWIPE_MIN_DISTANCE &&
Math.Abs(velocityX) > SWIPE_THRESHOLD_VELOCITY)
Toast.MakeText(view.Context, "Right Swipe",
ToastLength.Short).Show();
}
catch (Exception e)
{
// nothing
}
return false;
}
public void OnLongPress(MotionEvent e)
{
view.Text = "- LONG PRESS -";
}
public bool OnScroll(MotionEvent e1, MotionEvent e2, float
distanceX, float distanceY)
{
view.Text = "- FLING -";
return true;
}
public void OnShowPress(MotionEvent e)
{
view.Text = "- SHOW PRESS -";
}
public bool OnSingleTapUp(MotionEvent e)
{
view.Text = "- SINGLE TAP UP -";
return true;
}
}
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/monodroid/attachments/20111230/a9da4c24/attachment-0001.html
More information about the Monodroid
mailing list