2013-03-05 3 views
2

나는 드래그를 만들고 드롭이 코드를 사용하고 있습니다 :Android. 끌기를 방해하는 방법?

private final class MyTouchListener implements OnTouchListener { 
    public boolean onTouch(View view, MotionEvent motionEvent) { 
     if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) { 
     ClipData data = ClipData.newPlainText("", ""); 

     GlobalTouchX = (int) motionEvent.getX(); 
     GlobalTouchY = (int) motionEvent.getY(); 

     shadowBuilder = new MyDragShadowBuilder(view);  

     view.startDrag(data, shadowBuilder, null, 0); 

     CurrentDragImage = (ImageView)view; 
     view.setVisibility(View.GONE); 

     return true; 
     } else { 
     return false; 
     } 
    } 
    } 

가 어떻게 드래그 방법을 방해 할 수 드롭을 기다리지 않고, 또는 나는 드롭 이벤트는 프로그래밍 방식으로 호출 할 수 있습니다? 나는 여러 가지 방법을 시도했지만 행운은 없었다. onInterceptTouchEvent (MotionEvent 나)이 뷰 그룹 클래스의 메소드를 오버라이드 (override)하는 것이다 움직임을 차단의

MainRelative.setOnDragListener(new OnDragListener() {     
    public boolean onDrag(View v, DragEvent event) { 

    int action = event.getAction(); 

    switch (event.getAction()) { 

    case DragEvent.ACTION_DRAG_LOCATION:  

     //!!!!!!!!!!!!!!! 
     //HERE I WANT TO INTERRUPT DRAG EVENT ON SOME CONDITION 
     //!!!!!!!!!!!!!!! 

     break; 

    case DragEvent.ACTION_DROP: 

     MyOnDrop(v, event, true);     

     break; 
    } 

return true;} 
}); 

답변

0

방법 중 하나를 드래그를하는 경우,이 메소드가 불려 : 여기를 중단 할 수 있습니다 예를 들어, 그것은 좋은 것입니다 보기에, 그래서 당신이 가로 채기 해야하는 경우이 방법에 논리를 배치해야합니다.

예를 들어, FrameLayout을 사용하여 하위 뷰를 유지하려는 경우. 그런 다음이 FrameLayout 클래스를 확장하는 새 클래스를 만들고 onInterceptTouchEvent (MotionEvent me) 메서드를 재정의해야합니다.

class NewFrameLayout extends FrameLayout{ 


    @Override 
    public boolean onInterceptTouchEvent(MotionEvent ev) { 

    ///Place your logic here to intercept the dragging event 


     /* 
     * This method JUST determines whether we want to intercept the motion. 
     * If we return true, onMotionEvent will be called and we do the actual 
     * work here. 
     */ 

     /* 
     * Shortcut the most recurring case: the user is in the dragging 
     * state and he is moving his finger. We want to intercept this 
     * motion. 
     */ 
    } 
} 
+0

답장을 보내 주셔서 감사합니다. 큰 감사를 드린다! 나는 모든 디자인을 xml 파일에 가지고 있는데, 부모 요소는 RelativeLayout이다. 그래서 당신이 제공하는 방식을 사용한다면이 부모 RelativeLayout을 프로그래밍 방식으로 만들고 onInterceptTouchEvent를 사용해야합니다. 그래서 모든 뷰를 프로그래밍 방식으로 생성해야합니다. 맞습니까? 디자인을 위해 XML 파일을 사용할 방법이 없습니까? 기존 RelativeLayout에 대해 onInterceptTouchEvent를 재정의 할 수있는 방법이 있습니까? –

+0

당신이 할 수있는 일은 커스텀 RelativeLayout을 생성하고 그것을 XML에서 사용하는 것입니다. 예를 들어 RelativeLayoutNew가 새 클래스 인 경우 XML 파일의 RelativeLayout을 대체합니다. 나는 그것이 의미가 있기를 바랍니다. –

관련 문제