[mono-android] Gesture listener subclass no longer works with V4

Mike Child mike at mikechild.net
Fri Dec 30 07:43:22 EST 2011


Like Tomasz said, Inherit from Java.Lang.Object. You should never
implement the Handle property yourself.

Mike Child



On Fri, Dec 30, 2011 at 7:16 AM, Tomasz Cielecki <tomasz at ostebaronen.dk> wrote:
> Have your class GestureListener inherit Java.Lang.Object, this way you
> don't have to Implement the Handle property. I did this with an
> ISensorEventListener and it works just fine.
>
> On Fri, Dec 30, 2011 at 12:18 PM, John Murray <john at murray.gb.com> wrote:
>> 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;
>>
>>         }
>>
>>     }
>>
>>
>>
>> }
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> _______________________________________________
>> Monodroid mailing list
>> Monodroid at lists.ximian.com
>>
>> UNSUBSCRIBE INFORMATION:
>> http://lists.ximian.com/mailman/listinfo/monodroid
>>
>
>
>
> --
> Med Venlig Hilsen / With Best Regards
> Tomasz Cielecki
> http://ostebaronen.dk
> _______________________________________________
> Monodroid mailing list
> Monodroid at lists.ximian.com
>
> UNSUBSCRIBE INFORMATION:
> http://lists.ximian.com/mailman/listinfo/monodroid


More information about the Monodroid mailing list