2011-08-04 57 views
0

我想玩手勢,但我無法讓他們工作。MonoDroid手勢不適合我

我有這個

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

using Android.App; 
using Android.Content; 
using Android.OS; 
using Android.Runtime; 
using Android.Views; 
using Android.Widget; 

namespace Myapp 
{ 
    [Activity(Label = "My Activity")] 
    public class AboutActivity : Activity 
    { 
     private TextView displayText; 
     private GestureDetector gestureScanner; 
     private GestureListener gestureListener; 

     protected override void OnCreate(Bundle bundle) 
     { 
      RequestWindowFeature(WindowFeatures.NoTitle); 
      base.OnCreate(bundle); 
      SetContentView(Resource.Layout.About); 


      displayText = FindViewById<TextView>(Resource.Id.Privacy); 

      gestureListener = new GestureListener(displayText); 
      gestureScanner = new GestureDetector(this, gestureListener); 
     } 


     public override bool OnTouchEvent(MotionEvent e) 
     { 
      return gestureScanner.OnTouchEvent(e); 


     } 
    } 

    public class GestureListener : GestureDetector.SimpleOnGestureListener 
    { 
     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; 
     } 
    } 

} 

沒什麼每個觸發器。

回答

1

除了創建GestureDetector你可能會錯過分配的手勢事件GestureDetector,例如:

gestureDetector.SetOnDoubleTapListener(本);

請注意,此處的「this」表示OnDoubleTapEvent與上面的行在相同的類中實現。

+0

我想通了它爲什麼不能正常工作,這是因爲我有一個滾動視圖來包裝我的佈局,消耗所有的手勢。所以現在我正試圖解決這個問題。你會在哪裏實現這個SetOnDoubleTapListener它只是在同一個文件中的另一種方法? – chobo2

+0

是的,它可以在同一個類中實現。根據你的代碼,你可以在GestureListener類中實現它。 –