2013-08-23 2 views
1

EditText에서 작동하도록 스 와이프 제스처를 시도하고 있습니다. 그러나 내가 시도하는 모든 것은 버그가있는 경향이 있습니다.edittext에서 Android 핸들링 스 와이프 제스처

false를 반환하면 제스처가 작동하지만 스 와이프 제스처를 만들 때 커서가 움직입니다. 그러나 GestureListener (onFling)에서 결과를 반환하면 커서 체재가 유지되지만 Android 2.3.3에서는 제스처가 끝난 후 텍스트 편집의 상황에 맞는 메뉴가 팝업됩니다 (4.1.2에서는 컨텍스트 메뉴가 없음) 그러나 제스처를 만드는 것은 전체 단어를 선택할 것입니다.

package com.example.testmarkup; 

import android.content.Context; 
import android.view.GestureDetector; 
import android.view.GestureDetector.SimpleOnGestureListener; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnTouchListener; 

public class OnSwipeTouchListener implements OnTouchListener { 

    public interface OnSwipeListener { 
     public void OnSwipe(); 
    } 

    private GestureDetector mGestureDetector = null; 

    private OnSwipeListener mOnSwipeLeftListener; 

    private OnSwipeListener mOnSwipeRightListener; 
    private GestureListener mGestureListener = null; 

    public OnSwipeTouchListener(final Context context) { 
     mGestureListener = new GestureListener(); 
     mGestureDetector = new GestureDetector(context, mGestureListener);  
    } 

    public boolean onTouch(final View view, final MotionEvent motionEvent) { 
     final boolean result = mGestureDetector.onTouchEvent(motionEvent); 
     //When I return false here the gesture works but the cursor moves when making a swipe gesture. 
     //However when I return the result from the GestureListener (onFling) the cursor stay's in place, 
     //but on Android 2.3.3 after the gesture is finished the context menu from the text edit pops up, 
     //on 4.1.2 there is no context menu, but making the gesture will select a whole word. 
//  return false; 
     return result; 

    } 

    private final class GestureListener extends SimpleOnGestureListener { 

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

     @Override 
     public boolean onFling(final MotionEvent e1, final MotionEvent e2, final float velocityX, final float velocityY) { 
      boolean result = false; 
      try { 
       final float diffY = e2.getY() - e1.getY(); 
       final 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(); 
          result = true; 
         } else { 
          onSwipeLeft(); 
          result = true; 
         } 
        } 
       } else { 
        if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) { 
         if (diffY > 0) { 
          onSwipeBottom(); 
          result = true; 
         } else { 
          onSwipeTop(); 
          result = true; 
         } 
        } 
       } 
      } catch (final Exception exception) { 
       exception.printStackTrace(); 
      } 
      return result; 
     } 

    } 

    public void onSwipeRight() { 
     if (mOnSwipeRightListener != null) 
      mOnSwipeRightListener.OnSwipe(); 
    } 

    public void onSwipeLeft() { 
     if (mOnSwipeLeftListener != null) 
      mOnSwipeLeftListener.OnSwipe(); 
    } 

    public void onSwipeTop() { 
    } 

    public void onSwipeBottom() { 
    } 

    public OnSwipeTouchListener setOnSwipeLeft(final OnSwipeListener aListener) { 
     mOnSwipeLeftListener = aListener; 
     return this; 
    } 

    public OnSwipeTouchListener setOnSwipeRight(final OnSwipeListener aListener) { 
     mOnSwipeRightListener = aListener; 
     return this; 
    }  

} 

답변

0

당신은 슬쩍 이벤트를 처리 할 를 사용할 수 있습니다

여기에 내 현재 코드입니다. 링크를 따라 가면이 작업을 수행하는 방법에 대해 다른 많은 질문이 있습니다. 기본적으로, 당신은 같은 것을 할 수 있습니다

//global variables 
private boolean isSwiping = false; 
private SwipeDetector.Direction swipeDirection = null; 
private View v;//set to the parent layout of the fragments. 

//swipe-handling code 
$.with(v).swipe(new Function() { 
    @Override 
    public void invoke($ droidQuery, Object... params) { 
     if (params[0] == SwipeDetector.Direction.START) 
      isSwiping = true; 
     else if (params[0] == SwipeDetector.Direction.STOP) { 
      if (isSwiping) { 
       isSwiping = false; 
       if (swipeDirection != null) { 
        switch(swipeDirection) { 
         case DOWN : 
          //TODO: Down swipe complete, so do something 
          break; 
         case UP : 
          //TODO: Up swipe complete, so do something 
          break; 
         case LEFT : 
          //TODO: Left swipe complete, so do something 
          break; 
         case RIGHT : 
          //TODO: Right swipe complete, so do something (such as): 

          break; 
         default : 
          break; 
        } 
       } 
      } 
     } 
     else { 
      swipeDirection = (SwipeDetector.Direction) params[0]; 
     } 
    } 
}); 

당신은 http://bit.ly/droidquery에서 droidQuery를 얻을 수 있습니다.

관련 문제