2011-12-10 2 views
0

전체 화면에 대해 OnTouchListener를 갖고 싶습니다. 모든보기를 onTouchListener에 첨부하려고했지만 잘못된 touchEvents가 생성됩니다. 이 방법은 재정의 메서드에 의해 실현 될 수 있습니다, 내가 찾고있는 것은 리스너 솔루션입니다. 감사!전체 화면의 OnTouchListener

제스처 리스너를 사용하여이 작업을 수행 할 수 있습니까?

+0

아래 링크는 이미지를 사용하여이를 수행하는 방법을 설명합니다. http://www.zdnet.com/blog/burnette/how-to-use-multi-touch-in-android-2-part-2-building -the-touch-example/1763 누구가 비슷한 질문을했습니다 : http://stackoverflow.com/questions/5648985/ontouchlistener-for-entire-screen 제스처 리스너 구현에 관해서는 전혀 몰라요. – ihtkwot

+0

모든 뷰에서 투명한 오버레이 뷰를 만들고 오버레이 뷰를 수신 할 수 있습니까? 이 중첩 된보기를 클릭하면 버튼이 아래보기로 이동합니다. –

+0

마이크는 잘 모르겠습니다. 나는 나 자신을 시도하지 않았다. 질문에 대한 답변을 검색하는 동안 찾은 몇 가지 접근 방법을 함께 모으려고했습니다. 미안하지만 더 도움이되지 못했습니다. – ihtkwot

답변

1

스 와이프 동작을 수신하는 메서드를 사용하여 onSwipeListener 클래스를 삽입 할 수 있습니다. 그런 다음 activity (LinearLayout/RelativeLayout)의 레이아웃에 대해 view.OnTouchListener를 설정 한 다음 onSwipeListener의 다양한 메서드를 재정의하고 다양한 작업을 삽입 할 수 있습니다.

아래는 만들 수있는 onSwipeListener 클래스입니다. 다음과 같이

public class OnSwipeTouchListener implements OnTouchListener { 

    private final GestureDetector gestureDetector; 

    public OnSwipeTouchListener (Context ctx){ 
     gestureDetector = new GestureDetector(ctx, new GestureListener()); 
    } 

    @Override 
    public boolean onTouch(View v, MotionEvent 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(); 
         } 
        } 
        result = true; 
       } 
       else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) { 
        if (diffY > 0) { 
         onSwipeBottom(); 
        } else { 
         onSwipeTop(); 
        } 
       } 
       result = true; 

      } catch (Exception exception) { 
       exception.printStackTrace(); 
      } 
      return result; 
     } 
    } 

    public void onSwipeRight() { 
    } 

    public void onSwipeLeft() { 
    } 

    public void onSwipeTop() { 
    } 

    public void onSwipeBottom() { 
    } 
} 

이 클래스를 생성 한 후, 당신은 그것을 호출 할 수

relativeLayout.setOnTouchListener(new OnSwipeTouchListener(context) { 
public void onSwipeRight() { 
    //do something 
} 

public void onSwipeLeft() { 
    //do something 
} 

} 

희망이 도움이!

관련 문제