2012-04-28 61 views
1

我想在ViewGroup項目上實施滑動手勢(向左或向右滑動)以將其刪除。我還想讓手指沿着手指(水平拖動)將其刪除。我沒有找到任何例子。庫爾,請你給我建議我可以在哪裏看?要刪除的Android幻燈片元素

感謝

回答

4
public class SelectFilterActivity extends Activity implements OnClickListener 
{ 
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 GestureDetector gestureDetector; 
View.OnTouchListener gestureListener; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    /* ... */ 

    // Gesture detection 
    gestureDetector = new GestureDetector(new MyGestureDetector()); 
    gestureListener = new View.OnTouchListener() { 
     public boolean onTouch(View v, MotionEvent event) { 
      return gestureDetector.onTouchEvent(event); 
     } 
    }; 

} 

class MyGestureDetector 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; 
      // right to left swipe 
      if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
       Toast.makeText(SelectFilterActivity.this, "Left Swipe", Toast.LENGTH_SHORT).show(); 
      } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
       Toast.makeText(SelectFilterActivity.this, "Right Swipe", Toast.LENGTH_SHORT).show(); 
      } 
     } catch (Exception e) { 
      // nothing 
     } 
     return false; 
    } 

} 
+0

謝謝...但我從哪裏瞭解哪些因素有關? – Matroska 2012-04-28 10:42:06

+0

參考http://stackoverflow.com/questions/4373485/android-swipe-on-list – 2012-04-28 10:44:44

+0

好吧,這似乎是可行的,但我不明白如何移動視圖(拖動它)來顯示移動。你能幫我解決嗎? – Matroska 2012-04-28 12:58:23