2013-09-04 4 views

답변

1

guy_m의 답에 영감을 얻어 필자는 제안서를 채택하고 DrawerLayout의 다음 확장을 제안합니다. 다시,이 해결책은 onInterceptTouchEvent()를 오버라이드 (override)하는 것입니다.

최우선 방법의 논리

은 매우 간단하다 : 터치 이벤트가 서랍보기합니다 (DrawerLayout의 슬라이딩 부분) 떨어져 를 발생할 때마다

  • , 우리는 false를 돌려줍니다. 즉, DrawerLayout은이 동작과이 동작에 대한 추가 터치 이벤트를 가로 채거나 처리하지 않으며 DrawerLayout의 자식 뷰에서 처리합니다.
  • 한편, 서랍보기 안에 이벤트 이 발생하면 super.onInterceptTouchEvent()를 호출하여이 방법으로 이벤트를 가로채는 방법 및 처리 방법을 결정하도록합니다. 이렇게하면 슬라이드/터치 동작이 on 서랍보기에서 발생하는 한 원래 DrawerLayout과 같이 서랍을 안팎으로 밀어 넣을 수 있습니다.

오버 라이딩 된 onInterceptTouchEvent() 메소드의 다음 예제는 드로어보기가 화면 오른쪽에있는 DrawerLayout (android : gravity = "right")에 대한 것입니다. 그러나 표준 왼쪽 배치 서랍보기에서도 작동하도록 코드를 적용하는 방법이 분명해야합니다.

public class CustomDrawerLayout extends DrawerLayout 
{ 

    @Override 
    public boolean onInterceptTouchEvent(MotionEvent event) 
    { 
     final View drawerView = getChildAt(1); 
     final ViewConfiguration config = ViewConfiguration.get(getContext()); 

     // Calculate the area on the right border of the screen on which 
     // the DrawerLayout should always intercept touch events. 
     // In case the drawer is closed, we still want the DrawerLayout 
     // to respond to touch/drag gestures there and reopen the drawer! 
     final int rightBoundary = getWidth() - 2 * config.getScaledTouchSlop(); 

     // If the drawer is opened and the event happened 
     // on its surface, or if the event happened on the 
     // right border of the layout, then we let DrawerLayout 
     // decide if it wants to intercept (and properly handle) 
     // the event. 
     // Otherwise we don't let DrawerLayout to intercept, 
     // letting its child views handle the event. 
     return (isDrawerOpen(drawerView) && drawerView.getLeft() <= event.getX() 
       || rightBoundary <= event.getX()) 
       && super.onInterceptTouchEvent(event); 
    } 
... 
} 
+0

이것이 문제를 해결하는 이유에 대한 설명을 해 줄 수 있습니까? – Rob

+0

죄송합니다. 이전에 설명을 입력했지만 어떻게 든 게시에 분실했습니다. 나는 대답을 편집 할 것이다. –

+0

이것은 아마 내 해킹보다 나은 해결책입니다. – paul

5

DrawerLayout이 수정되었습니다.

onInterceptTouchEvent(MotionEvent ev)의 경우 interceptForTap이 true로 설정되지 않도록해야합니다. 한 가지 방법은 다음 조건을 제거하는 것입니다.

if (mScrimOpacity > 0 && 
    isContentView(mLeftDragger.findTopChildUnder((int) x, (int) y))) { 
    interceptForTap = true; 
} 

이렇게하면 넘어 질 수 있습니다.

LOCK_MODE_LOCKED_OPEN에 서랍 잠금 모드를 설정할 수 있습니다 닫지 서랍을합니다.

+0

어떻게 DrawerLayout을 수정 했습니까? – SpyZip

+0

[소스] (https://github.com/android/platform_frameworks_support/blob/master/v4/java/android/support/v4/widget/DrawerLayout.java)를 다운로드하여 수정 한 다음 프로젝트에 추가하십시오. – paul