Android - Can't get the OnTouchEvent to fire in Activity - using MonoDroid (Need the MotionEvent from OnTouch)
Would anyone by chance have a working example of using GestureListner with Monodroid? I can't seem to successfully translate what's out on the Net using Java.
I think I am close... And I think if I can get this "OnTouchEvent" to fire, I could in turn get my GestureDetector Class' OnTouchEvent to fi开发者_StackOverflow社区re, and then I would be able to get the swipe motion (or OnFling).
What do I need to do to get this event to fire in my Activity class? No
public override bool OnTouchEvent(MotionEvent e)
{
m_gestureDetector.OnTouchEvent(e);
return base.OnTouchEvent(e);
}
And I think I definitely need the OnTouch event instead of OnClick because I need the MotionEvent.
According to the Android docs for OnTouchEvent:
Called when a touch screen event was not handled by any of the views under it. This is most useful to process touch events that happen outside of your window bounds, where there is no view to receive it.
Are you sure you aren't handling it in a view? My guess is you probably should handle it in your view.
Basically you need to call the override the OnTouchEvent on the Activity. I think that's where you got it wrong. Here's an example on how I did it. Hope it helps.
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.textView );
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;
}
}
精彩评论