title: How to detect swipe on listview?
body: <p>I have used below code to detect swipe on listview:</p>
  
  <p>SwipeGestureListener.java</p>
  
  <pre><code>import android.content.Context;
  import android.view.GestureDetector;
  import android.view.MotionEvent;
  import android.widget.Toast;
  
  public class SwipeGestureListener implements GestureDetector.OnGestureListener{
  
      private static final int SWIPE_MIN_DISTANCE = 150;
      private static final int SWIPE_MAX_OFF_PATH = 100;
  
      private static final int SWIPE_THRESHOLD_VELOCITY = 100;
      protected MotionEvent mLastOnDownEvent = null;
      private static final int SWIPE_THRESHOLD_VELOCITY = 100;
  
      public SwipeGestureListener(Context context) {
      }
  
      @Override   
      public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
  
          if (e1==null)
              e1 = mLastOnDownEvent;
          if (e1==null || e2==null)
              return false;
  
          float dX = e2.getX()-e1.getX();
          float dY = e2.getY()-e1.getY();
  
          if (Math.abs(dY)&lt;SWIPE_MAX_OFF_PATH &amp;&amp; Math.abs(velocityX)&gt;=SWIPE_THRESHOLD_VELOCITY &amp;&amp; Math.abs(dX)&gt;=SWIPE_MIN_DISTANCE ) {
              if (dX&gt;0) {
                  Toast.makeText(context, "Right Swipe", Toast.LENGTH_SHORT).show();
              } else{
                  Toast.makeText(context, "Left Swipe", Toast.LENGTH_SHORT).show();
              }
  
              return true;
          }
  
          return false;
      }
  
      @Override
      public void onLongPress(MotionEvent e) {
          // TODO Auto-generated method stub
  
      }
  
      @Override
      public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { 
          // TODO Auto-generated method stub
          return false;
      }
  
      @Override
      public void onShowPress(MotionEvent e) {
          // TODO Auto-generated method stub
  
      }
  
     @Override
     public boolean onSingleTapUp(MotionEvent e) {
        // TODO Auto-generated method stub
         return false;
     }
  
      @Override
      public boolean onDown(MotionEvent e) {
          // TODO Auto-generated method stub
          return false;
      }
  
  
  }
  </code></pre>
  
  <p>MyActivity.java</p>
  
  <pre><code>ListView listview = (ListView)findViewById(R.id.song_list_listview);
  
  final GestureDetector gdt = new GestureDetector(this, new SwipeGestureListener(this));
  listview.setOnTouchListener(new OnTouchListener(){
  
      @Override
      public boolean onTouch(View v, MotionEvent event) {
          gdt.onTouchEvent(event);
          // TODO Auto-generated method stub
          return false;
      }
  
  });
  </code></pre>
  
  <p>This code works but it seems it only works when I do the swipe gesture on same item of listview. </p>
  
  <p>So for example, I have item 1, 2, and 3. When I start swiping on item 2, the ongoing swiping gesture must be within item 2 layout and end on item 2 layout for the gesture detection to work.</p>
  
  <p>What is the best way to detect swipe on listview? Thanks!</p>