2013-07-27 4 views
6

Je travaille sur l'activité Gesture dans Android je classe pour détecter l'action swipe estAndroid comment ajouter swipe Gesture sur LinearLayout sans onDown vrai

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; 
    } 

} 

mon fichier xml est

<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> 

et dans mon activité, j'appelle comme ça

ActivitySwipeDetector activitySwipeDetector = new ActivitySwipeDetector(this); 
     LinearLayout lowestLayout = (LinearLayout)this.findViewById(R.id.action_settings); 
     lowestLayout.setOnTouchListener(activitySwipeDetector); 

J'ai appelé ActivitySwipeDetector(this); de sorte que son travail très bien avec un plein activité. Mais comment puis-je appliquer un détecteur de balayage uniquement à LinearLayout et non à l'ensemble de l'activité. Sil te plait aide moi.

+0

Vous associez l'écouteur tactile à une vue particulière. Décidez quelle vue vous voulez détecter, et attachez-la à cette vue correcte. Si vous l'attachez à la vue de niveau supérieur dans l'activité comme vous l'avez fait ici, cela fonctionnera pour toute l'activité. –

+0

Mais si j'attache la vue de l'enfant c'est pas des travaux ... –

Répondre

18

Needed pour moi et le meilleur était votre code, donc je fixe un peu, besoin de plus fixe, voir le commentaire

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; 
    } 

} 

Utilisation:

@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

Je n'ai pas eu vous, où vous avez exactement frappé ??? –

+0

Je viens d'éditer votre code, vérifiez celui-là .. Je pense que fait avec votre problème .. –

+0

+1 cela m'a aidé merci :) –

0

J'ai eu de bons résultats avec l'original code. Mais l'écouteur doit être ajouté à une vue, pas une mise en page. Nous étendons View.OnTouchListener, après tout.

+1

LinearLayout étend ViewGroup qui étend View. Je ne vois pas pourquoi un écouteur n'a pas pu être ajouté à une mise en page. – mhenry