2013-01-09 32 views
1

我有一個listview,我個性化,並且我加入了setOnItemLongClickListener(),這很好用。然後我決定實施一個手勢listView.setOnTouchListener(new OnSwipeTouchListener()),這也很好。我從其他帖子複製了OnSwipetouchListener類。Android:如何結合使用Longpress在listView上的項目中刷卡手勢

問題是,當我添加滑動監聽器的longPress不再工作。我想這是因爲滑動監聽器爲自己採取了longpress操作,並且不允許longPress執行任何操作。

我想做什麼:

揮擊聽衆得到下面2秒一切,在那之後一切都將長按。所以我仍然可以通過輕掃手勢更改列表視圖內容,並且我還可以爲每個listitem創建函數。

我的代碼:

public class OnSwipeTouchListener implements OnTouchListener { 

    private final GestureDetector gestureDetector = new GestureDetector(new GestureListener()); 

    public boolean onTouch(final View v, final MotionEvent event) { 
     //super.onTouch(v, event); 
     return gestureDetector.onTouchEvent(event); 
    } 

    private final class GestureListener extends SimpleOnGestureListener { 

     private static final int SWIPE_THRESHOLD = 100; 
     private static final int SWIPE_VELOCITY_THRESHOLD = 100; 

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

     @Override 
     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
      boolean result = false; 
      try { 
       float diffY = e2.getY() - e1.getY(); 
       float diffX = e2.getX() - e1.getX(); 
       if (Math.abs(diffX) > Math.abs(diffY)) { 
        if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) { 
         if (diffX > 0) { 
          onSwipeRight(); 
         } else { 
          onSwipeLeft(); 
         } 
        } 
       } else { 
        if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) { 
         if (diffY > 0) { 
          onSwipeBottom(); 
         } else { 
          onSwipeTop(); 
         } 
        } 
       } 
      } catch (Exception exception) { 
       exception.printStackTrace(); 
      } 
      return result; 
     } 
...methods... 
} 

回答

1

取出onDown方法。現在,這總是返回true,並防止處理longPress。

相關問題