2016-08-02 4 views

ответ

2

Если вы ищете Xamarin/C# например

Реализовать GestureDetector.IOnGestureListener на вашем Activity (или просмотреть) и высчитайте направление наложения в OnFling метода:

[Activity(Label = "Swiper", MainLauncher = true, Icon = "@mipmap/icon")] 
public class MainActivity : Activity, GestureDetector.IOnGestureListener 
{ 
    GestureDetector gestureDetector; 
    const int SWIPE_DISTANCE_THRESHOLD = 100; 
    const int SWIPE_VELOCITY_THRESHOLD = 100; 

    public bool OnDown(MotionEvent e) 
    { 
     return true; 
    } 

    public bool OnFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) 
    { 
     float distanceX = e2.GetX() - e1.GetX(); 
     float distanceY = e2.GetY() - e1.GetY(); 
     if (Math.Abs(distanceX) > Math.Abs(distanceY) && Math.Abs(distanceX) > SWIPE_DISTANCE_THRESHOLD && Math.Abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) 
     { 
      if (distanceX > 0) 
       OnSwipeRight(); 
      else 
       OnSwipeLeft(); 
      return true; 
     } 
     return false; 
    } 

    void OnSwipeLeft() 
    { 
     Button button = FindViewById<Button>(Resource.Id.myButton); 
     button.Text = "Swiped Left"; 
    } 

    void OnSwipeRight() 
    { 
     Button button = FindViewById<Button>(Resource.Id.myButton); 
     button.Text = "Swiped Right"; 
    } 

    public void OnLongPress(MotionEvent e) 
    { 
    } 

    public bool OnScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) 
    { 
     return false; 
    } 

    public void OnShowPress(MotionEvent e) 
    { 
    } 

    public bool OnSingleTapUp(MotionEvent e) 
    { 
     return false; 
    } 

    public bool OnTouch(View v, MotionEvent e) 
    { 
     return gestureDetector.OnTouchEvent(e); 
    } 

    public override bool OnTouchEvent(MotionEvent e) 
    { 
     gestureDetector.OnTouchEvent(e); 
     return false; 
    } 
    protected override void OnCreate(Bundle savedInstanceState) 
    { 
     base.OnCreate(savedInstanceState); 
     SetContentView(Resource.Layout.Main); 

     gestureDetector = new GestureDetector(this); 
    } 
} 
2

Согласно с https://developer.android.com/training/gestures/detector.html

Вы можете реализовать GestureDetector.OnGestureListener в классе деятельности и переопределить onFling или OnScroll

@Override 
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, 
     float distanceY) { 
    Log.d(DEBUG_TAG, "onScroll: " + e1.toString()+e2.toString()); 
    return true; 
} 


@Override 
public boolean onFling(MotionEvent event1, MotionEvent event2, 
     float velocityX, float velocityY) { 
    Log.d(DEBUG_TAG, "onFling: " + event1.toString()+event2.toString()); 
    return true; 
}