2012-04-04 3 views
4

私のアプリでスワイプジェスチャーを実装しようとしています。ほぼすべてのコードを作成しましたが、うまくいきません。ここでAndroid - スワイプジェスチャーのトラブル

は私が私の活動を持っているコードです:私は、画面をタッチすると

// Swipe detector 
gestureDetector = new GestureDetector(new SwipeGesture(this)); 
gestureListener = new OnTouchListener() { 
    public boolean onTouch(View v, MotionEvent event) 
    { 
     Log.e("", "It works"); 
     return gestureDetector.onTouchEvent(event); 
    } 
}; 

LinearLayout root = (LinearLayout) findViewById(R.id.rules_root); 
root.setOnTouchListener(gestureListener); 

は、logcatはit worksを表示します。

は、ここで彼クラスSwipeGestureの私のコード:

public class SwipeGesture extends SimpleOnGestureListener 
{ 
    private static final int SWIPE_MIN_DISTANCE = 120; 
    private static final int SWIPE_MAX_OFF_PATH = 250; 
    private static final int SWIPE_THRESHOLD_VELOCITY = 200; 

    private Activity activity; 

    public SwipeGesture(Activity activity) 
    { 
     super(); 
     this.activity = activity; 
    } 

    @Override 
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) 
    { 
     Log.e("", "Here I am"); 
     try 
     { 
      if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) return false; 
      if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) 
      { 
       if (((TabActivity) activity.getParent()).getTabHost() != null) 
       { 
        TabHost th = ((TabActivity) activity.getParent()).getTabHost(); 
        th.setCurrentTab(th.getCurrentTab() - 1); 
       } 
       else 
       { 
        activity.finish(); 
       } 
       Log.e("", "Swipe left"); 
      } 
      else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) 
      { 
       if (((TabActivity) activity.getParent()).getTabHost() != null) 
       { 
        TabHost th = ((TabActivity) activity.getParent()).getTabHost(); 
        th.setCurrentTab(th.getCurrentTab() + 1); 
       } 
       Log.e("", "Swipe right"); 
      } 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 
     return false; 
    } 
} 

ラインLog.e("", "Here I am");は表示されません。ですから、私はonFlingメソッドが呼び出されないと推測します。

これはなぜ機能しないのでしょうか?

ありがとうございました。

よろしくお願いいたします。

V.

答えて

6

あなたのSimpleOnGestureListenerで、登録するジェスチャーのonDownをオーバーライドします。それだけtrueを返すことができますが、それは次のように定義する必要があります。..

@Override 
    public boolean onDown(MotionEvent e) { 
      return true; 
    } 

.... See this link.. and the comment below the answer..

+0

この作品のおかげで、! – Manitoba

+0

オハイオ州の男、私はこの同じ問題で約2時間、客観性 –

5

いくつか変更する必要があります。例を示します。あなたはTabs間でスワイプしているように見えます

class SwipeGesture extends SimpleOnGestureListener { 
    @Override 
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, 
      float velocityY) { 

     try { 
      if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) 
       return false; 
      if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE 
        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
//Do something 

       return true; 
      } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE 
        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
//Do something 
       return true; 

      } 

     } catch (Exception e) { 
      Log.e("Fling", "There was an error processing the Fling event:" 
        + e.getMessage()); 
     } 
     return true; 
    } 

    // Necessary for the onFling event to register 
    @Override 
    public boolean onDown(MotionEvent e) { 
     return true; 
    } 
} 

root.setOnTouchListener(new View.OnTouchListener() { 
      public boolean onTouch(View v, MotionEvent event) { 
       if (gestureDetector.onTouchEvent(event)) { 
        return false; 
       } 
       return false; 
      } 
     }); 

SwipeGestureクラス

はあなたのOnTouchListenerを設定します。 FragmentsViewPagerを使用すると、ユーザーにとってはるかに簡単でスムーズです。

関連する問題