2011-08-24 3 views
0

목록 활동에 스 와이프 제스처를 구현하려고합니다. 이것은 작동하지만 문제는 터치 이벤트가 목록의 항목을 활성화한다는 것입니다.스 와이프 제스처 및 안드로이드 문제 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. 루트 레이아웃에서 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; 
     } 
    } 

을 그리고 그리스트 뷰에 스크롤 속이고 있었기 때문에 진정한 반환 된 onDown() 재정의를 제거 : 날뛰는 제스처는 다음과 같다.

누군가가 도움을 줄 수 있기를 바랍니다.

관련 문제