2013-05-27 69 views

回答

5
private GestureDetector _gestureDetector; 
private GestureListener _gestureListener; 

protected override void OnCreate(Bundle bundle) 
{ 
     _gestureListener = new GestureListener(); 
     _gestureListener.LeftEvent += GestureLeft; 
     _gestureListener.RightEvent += GestureRight; 
     _gestureDetector = new GestureDetector(this, _gestureListener); 
} 

private void GestureLeft() 
{ 
    if (TabHost.CurrentTab - 1 < 0) 
    { 
     TabHost.CurrentTab = TabHost.TabWidget.TabCount - 1; 
    } 
    else 
    { 
     TabHost.CurrentTab--; 
    } 
} 

private void GestureRight() 
{ 
    if (TabHost.CurrentTab + 1 >= TabHost.TabWidget.TabCount) 
    { 
     TabHost.CurrentTab = 0; 
    } 
    else 
    { 
     TabHost.CurrentTab++; 
    } 
} 

public override bool DispatchTouchEvent(MotionEvent ev) 
{ 
    _gestureDetector.OnTouchEvent(ev); 
    return base.DispatchTouchEvent(ev); 
} 

GestureListener.cs(我在網上查到了這個解決辦法,但我忘了黯然源,感謝原作者反正):

using System; 
using Android.Views; 

namespace Utils 
{ 
public class GestureListener : Java.Lang.Object, GestureDetector.IOnGestureListener 
{ 
    public event Action LeftEvent; 
    public event Action RightEvent; 
    private static int SWIPE_MAX_OFF_PATH = 250; 
    private static int SWIPE_MIN_DISTANCE = 120; 
    private static int SWIPE_THRESHOLD_VELOCITY = 200; 

    public GestureListener() 
    { 
    } 

    public bool OnDown(MotionEvent e) 
    { 
     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 && LeftEvent != null) 
       LeftEvent();//Toast.MakeText(view.Context, "Left Swipe", ToastLength.Short).Show(); 
      else if (e2.GetX() - e1.GetX() > SWIPE_MIN_DISTANCE && Math.Abs(velocityX) > SWIPE_THRESHOLD_VELOCITY && RightEvent != null) 
       RightEvent();// Toast.MakeText(view.Context, "Right Swipe", ToastLength.Short).Show(); 
     } 
     catch (Exception e) 
     { 
      // nothing 
     } 
     return false; 
    } 

    public void OnLongPress(MotionEvent e) 
    { 
    } 

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

    public void OnShowPress(MotionEvent e) 
    { 
    } 

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

}

1

謝謝你,作品像預期一樣。下面是一個ActionBarSherlock衍生物:

private void GestureLeft() 
    { 
     Xamarin.ActionbarSherlockBinding.App.ActionBar ab = base.SupportActionBar; 

     if (ab.SelectedTab.Position - 1 < 0) 
     { 
      //ab.SelectTab(ab.GetTabAt(ab.SelectedTab.Position - 1)); 
      ab.SelectTab(ab.GetTabAt(ab.TabCount - 1)); 
     } 
     else 
     { 
      //TabHost.CurrentTab--; 
      ab.SelectTab(ab.GetTabAt(ab.SelectedTab.Position - 1)); 
     } 
    } 

    private void GestureRight() 
    { 
     Xamarin.ActionbarSherlockBinding.App.ActionBar ab = base.SupportActionBar; 


     if (ab.SelectedTab.Position+1 >= ab.TabCount) //TabHost.CurrentTab + 1 >= TabHost.TabWidget.TabCount) 
     { 
      ab.SelectTab(ab.GetTabAt(0)); 
      //TabHost.CurrentTab = 0; 
     } 
     else 
     { 
      ab.SelectTab(ab.GetTabAt(ab.SelectedTab.Position + 1)); 
      //TabHost.CurrentTab++; 
     } 
    }