2011-08-24 44 views
0

我正試圖在列表活動上實現滑動手勢。這是可行的,但問題是觸摸事件也激活列表上的項目!刷卡式滑動手勢和android問題ListActivity:扔手勢正在激活底層列表項

我找過其他答案,但不幸找不到工作解決方案。下面是我使用的代碼:

在列出的活動:

// Setup the swipe detector 
mSwipeGestureListener = new SwipeGestureListener(this); 
mGestureDetector = new GestureDetector(mSwipeGestureListener); 
mGestureDetector.setIsLongpressEnabled(false); 

@Override 
public boolean dispatchTouchEvent(final MotionEvent ev) 
{ 
    boolean handled = false; 

    if (mGestureDetector != null) 
    { 
     handled = mGestureDetector.onTouchEvent(ev); 
    } 

    handled |= super.dispatchTouchEvent(ev); 

    return handled; 
} 

,這裏是我的手勢監聽器代碼:

public class SwipeGestureListener extends GestureDetector.SimpleOnGestureListener 
    { 
    protected final Activity mActivity; 
    protected final int mTouchSlop; 
    protected final int mMinimumSwipeVelocity; 

    public SwipeGestureListener(final Activity activity) 
    { 
     mActivity = activity; 
     final ViewConfiguration viewConfiguration = ViewConfiguration.get(activity); 

     mTouchSlop = viewConfiguration.getScaledTouchSlop(); 
     mMinimumSwipeVelocity = viewConfiguration.getScaledMinimumFlingVelocity(); 
    } 

    @Override 
    public boolean onFling(final MotionEvent e1, final MotionEvent e2, final float velocityX, final float velocityY) 
    { 
     // Don't handle swipe if Y offset is greater than touch slop 
     if (Math.abs(e1.getY() - e2.getY()) > mTouchSlop) 
     { 
      return false; 
     } 
     else 
     { 
      if (e1.getX() - e2.getX() > mTouchSlop 
      && -velocityX > mMinimumSwipeVelocity) 
      { 
       // User swiped finger left. 
       onSwipeToLeft(); 
      } 
      else if (e2.getX() - e1.getX() > mTouchSlop 
      && velocityX > mMinimumSwipeVelocity) 
      { 
       // User swiped finger left. 
       onSwipeToRight(); 
      } 

      // We're interested in these series of events 
      return true; 
     }   
    } 

    // It is necessary to return true from onDown for the onFling event to register 
    @Override 
    public boolean onDown(final MotionEvent e) 
    { 
     return true; 
    } 

    protected void onSwipeToLeft() {} 

    protected void onSwipeToRight() {}; 
    } 

我徹底難倒。我怎樣才能在沒有手勢的情況下處理Android中的手勢,同時激活列表中的基礎項目!

P.S.在根佈局上使用view.setOnTouchListener導致從不接收觸摸事件;這就是我使用dispatchTouchEvent的原因。

任何幫助將不勝感激。

謝謝!

回答

0

這不是一個完整的解決方案,但我能夠解決此問題與下面的代碼更改:

而不是使用dispatchEvent的,我這樣做:

getListView().setOnTouchListener(new View.OnTouchListener() 
    { 
     @Override 
     public boolean onTouch(final View v, final MotionEvent me) 
     { 
      boolean handled = false; 

      if (mGestureDetector != null) 
      { 
       handled = mGestureDetector.onTouchEvent(me); 
      } 

      return handled; 
     } 
    }); 

我提高了掃視姿勢如下:

@Override 
    public boolean onFling(final MotionEvent e1, final MotionEvent e2, final float velocityX, final float velocityY) 
    {   
     if (e1 != null && e2 != null) 
     { 
      final float length = Math.abs(e1.getX() - e2.getX()); 
      final float height = Math.abs(e1.getY() - e2.getY()); 

      // Accept criteria: X movement more than or equal to touch slop, total 
      // slope <= 50% 
      if (length < mTouchSlop || height/length > 0.50) 
      { 
       return false; 
      } 
      else 
      { 
       if (-velocityX > mMinimumSwipeVelocity) 
       { 
        // User swiped finger left. 
        onSwipeToLeft(); 
       } 
       else if (velocityX > mMinimumSwipeVelocity) 
       { 
        // User swiped finger right. 
        onSwipeToRight(); 
       } 

       // We're interested in these series of events 
       return true; 
      } 
     } 
     else 
     { 
      return false; 
     } 
    } 

我刪除,因爲這是搞砸了滾動的ListView控件這是返回true的onDown()重寫。

希望這可以幫助別人。