2013-07-27 3 views
6

があるAndroidの本当の私はスワイプ操作を検出するためにクラスを使用アンドロイドでジェスチャー活動に取り組んでいる

public class ActivitySwipeDetector implements View.OnTouchListener { 

    static final String logTag = "ActivitySwipeDetector"; 
    private Activity activity; 
    static final int MIN_DISTANCE = 100; 
    private float downX, downY, upX, upY; 

    public ActivitySwipeDetector(Activity activity){ 
     this.activity = activity; 
    } 

    public void onRightToLeftSwipe(){ 
     Log.i(logTag, "RightToLeftSwipe!"); 
     Toast.makeText(activity, "RightToLeftSwipe", Toast.LENGTH_SHORT).show(); 
     //activity.doSomething(); 
    } 

    public void onLeftToRightSwipe(){ 
     Log.i(logTag, "LeftToRightSwipe!"); 
     Toast.makeText(activity, "LeftToRightSwipe", Toast.LENGTH_SHORT).show(); 
     //activity.doSomething(); 
    } 

    public void onTopToBottomSwipe(){ 
     Log.i(logTag, "onTopToBottomSwipe!"); 
     //Toast.makeText(activity, "onTopToBottomSwipe", Toast.LENGTH_SHORT).show(); 
     //activity.doSomething(); 
    } 

    public void onBottomToTopSwipe(){ 
     Log.i(logTag, "onBottomToTopSwipe!"); 
     //Toast.makeText(activity, "onBottomToTopSwipe", Toast.LENGTH_SHORT).show(); 
     //activity.doSomething(); 
    } 

    public boolean onTouch(View v, MotionEvent event) { 
     switch(event.getAction()){ 
     case MotionEvent.ACTION_DOWN: { 
      downX = event.getX(); 
      downY = event.getY(); 
      return true; 
     } 
     case MotionEvent.ACTION_UP: { 
      upX = event.getX(); 
      upY = event.getY(); 

      float deltaX = downX - upX; 
      float deltaY = downY - upY; 

      // swipe horizontal? 
        if(Math.abs(deltaX) > MIN_DISTANCE){ 
         // left or right 
         if(deltaX < 0) { this.onLeftToRightSwipe(); return true; } 
         if(deltaX > 0) { this.onRightToLeftSwipe(); return true; } 
        } 
        else { 
         Log.i(logTag, "Swipe was only " + Math.abs(deltaX) + " long, need at least " + MIN_DISTANCE); 
         return false; // We don't consume the event 
        } 

        // swipe vertical? 
        if(Math.abs(deltaY) > MIN_DISTANCE){ 
         // top or down 
         if(deltaY < 0) { this.onTopToBottomSwipe(); return true; } 
         if(deltaY > 0) { this.onBottomToTopSwipe(); return true; } 
        } 
        else { 
         Log.i(logTag, "Swipe was only " + Math.abs(deltaX) + " long, need at least " + MIN_DISTANCE); 
         return false; // We don't consume the event 
        } 

        return true; 
     } 
     } 
     return false; 
    } 

} 

私のxmlファイルは

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/action_settings" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/hello_world" /> 

</LinearLayout> 

ですそして私の活動で私はこのように呼んでいます。私はこのように呼んでいます ctivity。しかし、スワイプディテクタをLinearLayoutにのみ適用するにはどうすればよいのですか?私を助けてください。私と最善のために必要な

+0

特定のビューのタッチリスナーを接続しています。スワイプを検出するビューを決定し、適切なビューにアタッチします。ここで行ったように、アクティビティのトップレベルビューにアタッチすると、アクティビティ全体で機能します。 –

+0

しかし、私は子供のビューを添付して、それが動作しない場合... –

答えて

18

はあなたのコードだったので、私は少しを固定し、より多くの修正が必要、コメント

public class RelativeLayoutTouchListener implements OnTouchListener { 

    static final String logTag = "ActivitySwipeDetector"; 
    private Activity activity; 
    static final int MIN_DISTANCE = 100;// TODO change this runtime based on screen resolution. for 1920x1080 is to small the 100 distance 
    private float downX, downY, upX, upY; 

    // private MainActivity mMainActivity; 

    public RelativeLayoutTouchListener(MainActivity mainActivity) { 
     activity = mainActivity; 
    } 

    public void onRightToLeftSwipe() { 
     Log.i(logTag, "RightToLeftSwipe!"); 
     Toast.makeText(activity, "RightToLeftSwipe", Toast.LENGTH_SHORT).show(); 
     // activity.doSomething(); 
    } 

    public void onLeftToRightSwipe() { 
     Log.i(logTag, "LeftToRightSwipe!"); 
     Toast.makeText(activity, "LeftToRightSwipe", Toast.LENGTH_SHORT).show(); 
     // activity.doSomething(); 
    } 

    public void onTopToBottomSwipe() { 
     Log.i(logTag, "onTopToBottomSwipe!"); 
     Toast.makeText(activity, "onTopToBottomSwipe", Toast.LENGTH_SHORT).show(); 
     // activity.doSomething(); 
    } 

    public void onBottomToTopSwipe() { 
     Log.i(logTag, "onBottomToTopSwipe!"); 
     Toast.makeText(activity, "onBottomToTopSwipe", Toast.LENGTH_SHORT).show(); 
     // activity.doSomething(); 
    } 

    public boolean onTouch(View v, MotionEvent event) { 
     switch (event.getAction()) { 
     case MotionEvent.ACTION_DOWN: { 
      downX = event.getX(); 
      downY = event.getY(); 
      return true; 
     } 
     case MotionEvent.ACTION_UP: { 
      upX = event.getX(); 
      upY = event.getY(); 

      float deltaX = downX - upX; 
      float deltaY = downY - upY; 

      // swipe horizontal? 
      if (Math.abs(deltaX) > MIN_DISTANCE) { 
       // left or right 
       if (deltaX < 0) { 
        this.onLeftToRightSwipe(); 
        return true; 
       } 
       if (deltaX > 0) { 
        this.onRightToLeftSwipe(); 
        return true; 
       } 
      } else { 
       Log.i(logTag, "Swipe was only " + Math.abs(deltaX) + " long horizontally, need at least " + MIN_DISTANCE); 
       // return false; // We don't consume the event 
      } 

      // swipe vertical? 
      if (Math.abs(deltaY) > MIN_DISTANCE) { 
       // top or down 
       if (deltaY < 0) { 
        this.onTopToBottomSwipe(); 
        return true; 
       } 
       if (deltaY > 0) { 
        this.onBottomToTopSwipe(); 
        return true; 
       } 
      } else { 
       Log.i(logTag, "Swipe was only " + Math.abs(deltaX) + " long vertically, need at least " + MIN_DISTANCE); 
       // return false; // We don't consume the event 
      } 

      return false; // no swipe horizontally and no swipe vertically 
     }// case MotionEvent.ACTION_UP: 
     } 
     return false; 
    } 

} 

使用法を参照してください。私はオリジナルとの良好な結果を得てい

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    rlTop = (RelativeLayout) findViewById(R.id.rlTop);  
    rlTop.setOnTouchListener(new RelativeLayoutTouchListener(this)); 
} 
+0

私はdid't、あなたが正確に打たれたか? –

+0

私はちょうどあなたのコードを編集しました。私はあなたの問題を完了したと思っています。 –

+0

+1それは私に感謝を助けました:) –

0

をコード。しかし、リスナーはレイアウトではなくビューに追加する必要があります。結局、View.OnTouchListenerを拡張しています。

+1

LinearLayoutはViewを拡張するViewGroupを拡張します。なぜリスナーをレイアウトに追加できないのか分かりません。 – mhenry