2014-02-22 3 views
7

popupwindow를 확장 한 클래스를 만들었습니다.안드로이드 Click through PopupWindow

private OnTouchListener onTouchListener = new OnTouchListener() { 
    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     float x = event.getX(); 
     float y = event.getY(); 
     Log.d(TAG, "Received"); 

     if (Math.pow(x - coordinates[0], 2) + Math.pow((y - coordinates[1]), 2) < Math.pow(radius, 2)) { 
      Log.d(TAG, "bubbled through"); 
      return false; 
     } 
     return true; 
    } 
}; 

실제로, 나는 시각적으로 표현 SomePopupWindow.showAtLocation(SomeActivity.getWindow().getDecorView().getRootView(), Gravity.CENTER, 0, 0);

를 호출 popupwindow를 표시, 여기에 문제의 제품입니다 : 생성자는이 다음 onTouchListener의 모습을 다음

super(builder.context.get()); 

this.setWindowLayoutMode(ViewGroup.LayoutParams.MATCH_PARENT, 
     ViewGroup.LayoutParams.MATCH_PARENT); 
setFocusable(true); 
setBackgroundDrawable(new BitmapDrawable()); 
setTouchInterceptor(onTouchListener); 
FrameLayout frameLayout = new FrameLayout(builder.context.get()); 
LayoutInflater inflater = (LayoutInflater) builder.context.get().getSystemService(
     Context.LAYOUT_INFLATER_SERVICE); 
View overlayBase = inflater.inflate(R.layout.tutorial_view_items, null, false); 
frameLayout.addView(builder.backgroundView); 
frameLayout.addView(overlayBase); 
setContentView(frameLayout); 

과 같이 보입니다

blah

touchListener가 응답하지만 입력이 기본 액티비티 또는 popupwindow로 전송되지 않습니까? (버튼이 응답하지 않는 경우, ontouchlistener를 제거한 것처럼 버튼이 제대로 작동합니다.) FrameLayout이 터치 리스너 (이 경우 popupwindow의 즉 내용보기) 대신 popupwindow 자체를 설정

갱신 나 팝업에 정의 된 버튼을 클릭 할 수 있습니다. 여전히 Popupwindow에 대한 터치 인터셉터를 등록 기본 활동 /보기

솔루션

에 터치 이벤트를 위임하는 방법을 찾고. 액티비티를 처리하기를 원할 경우 팝업 창에서 터치를 처리하기를 원할 경우 someActivity.dispatchTouchEvent(someMotionEventPassedIntoTheTouchIntercepter);으로 전화를 걸면 팝업 창 setContentView(someView) 메서드에 설정된대로 팝업 창 내용보기 으로 someView.dispatchTouchEvent(theMotionEventPassedToTouchInterceptor)으로 전화하십시오.

내 방법은 특별히 당신을 도울 수 있습니다이 코드를하려고 다음

private OnTouchListener onTouchListener = new OnTouchListener() { 
@Override 
    public boolean onTouch(View v, MotionEvent event) { 
     float x = event.getX(); 
     float y = event.getY(); 

     if (Math.pow(x - coordinates[0], 2) + Math.pow((y - coordinates[1]), 2) < Math.pow(
       radius, 2)) { 
      activity.get().dispatchTouchEvent(event); 
      return false; 
     } 
     frameLayout.dispatchTouchEvent(event); 
     return true; 
    } 
}; 
+0

처럼 보였다? – NameSpace

+0

예, 혼란에 대해 사과드립니다. ontouchlistener가 호출되고 내가 터치 한 부분에 따라 로그 출력이 따라옵니다 (즉 원 영역을 터치하면 "버블이 통과 함") – rperryng

+0

확실하지 않습니다. 해킹 솔루션이 필요한 경우 터치가 xy 좌표에 있는지 확인하고 performClick()을 수행 할 수 있습니다.또는 버튼이 포함 된 Activity/View의 onTouch에 명시 적으로 터치를 전달하십시오. 잘만되면 누군가는 유창한 대답을 줄 수 있습니다. – NameSpace

답변

3

솔루션

는 Popupwindow에 대한 터치 인터셉터를 등록합니다. 액티비티를 처리하기를 원할 경우 팝업 창에서 터치를 처리하기를 원할 경우 someActivity.dispatchTouchEvent(someMotionEventPassedIntoTheTouchIntercepter);으로 전화를 걸면 팝업 창 setContentView(someView) 메서드에 설정된대로 팝업 창 내용보기 으로 someView.dispatchTouchEvent(theMotionEventPassedToTouchInterceptor)으로 전화하십시오.

내 방법은 특별히 당신이 실제로 당신이 팝업을 통해 떨어질 것으로 예상되는 클릭에 기록되고있는 "버블"여부를 명확히 할 수 다음

private OnTouchListener onTouchListener = new OnTouchListener() { 
@Override 
    public boolean onTouch(View v, MotionEvent event) { 
     float x = event.getX(); 
     float y = event.getY(); 

     if (Math.pow(x - coordinates[0], 2) + Math.pow((y - coordinates[1]), 2) < Math.pow(
       radius, 2)) { 
      activity.get().dispatchTouchEvent(event); 
      return false; 
     } 
     frameLayout.dispatchTouchEvent(event); 
     return true; 
    } 
}; 
+0

여보세요? 'theMotionEventPassedToTouchInterceptor' 당신은 이것을하는 방법을 더 자세하게 말할 수 있습니까? –

+0

'onTouchListener'에 전달 된 매개 변수 중 하나가'MotionEvent' 유형 인 것을 보시겠습니까? 기본 액티비티에 대한 참조가있는 경우 yourActivity.dispatchTouchEvent (motionEvent)를 호출하여 해당 터치 이벤트를 전달하십시오 (그리고 rrturn false!) – rperryng

+0

이 바로 여기에서 3 일간의 작업 후에 해결 방법을 제공합니다. 너는 남자 야! –

0
public class TestPopupActivity extends Activity { 

//The "x" and "y" position of the "Show Button" on screen. 
Point p; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    Button btn_show = (Button) findViewById(R.id.show_popup); 
    btn_show.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View arg0) { 

     //Open popup window 
     if (p != null) 
     showPopup(TestPopupActivity.this, p); 
    } 
    }); 
} 

// Get the x and y position after the button is draw on screen 
// (It's important to note that we can't get the position in the onCreate(), 
// because at that stage most probably the view isn't drawn yet, so it will return (0, 0)) 
@Override 
public void onWindowFocusChanged(boolean hasFocus) { 

    int[] location = new int[2]; 
    Button button = (Button) findViewById(R.id.show_popup); 

    // Get the x, y location and store it in the location[] array 
    // location[0] = x, location[1] = y. 
    button.getLocationOnScreen(location); 

    //Initialize the Point with x, and y positions 
    p = new Point(); 
    p.x = location[0]; 
    p.y = location[1]; 
} 

// The method that displays the popup. 
private void showPopup(final Activity context, Point p) { 
    int popupWidth = 200; 
    int popupHeight = 150; 

    // Inflate the popup_layout.xml 
    LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup); 
    LayoutInflater layoutInflater = (LayoutInflater) context 
    .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View layout = layoutInflater.inflate(R.layout.popup_layout, viewGroup); 

    // Creating the PopupWindow 
    final PopupWindow popup = new PopupWindow(context); 
    popup.setContentView(layout); 
    popup.setWidth(popupWidth); 
    popup.setHeight(popupHeight); 
    popup.setFocusable(true); 

    // Some offset to align the popup a bit to the right, and a bit down, relative to button's position. 
    int OFFSET_X = 30; 
    int OFFSET_Y = 30; 

    // Clear the default translucent background 
    popup.setBackgroundDrawable(new BitmapDrawable()); 

    // Displaying the popup at the specified location, + offsets. 
    popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y); 

    // Getting a reference to Close button, and close the popup when clicked. 
    Button close = (Button) layout.findViewById(R.id.close); 
    close.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     popup.dismiss(); 
    } 
    }); 
} 
} 

처럼 보였다. ... . 또는 링크 더 유 도움이 될 수 있습니다 .. http://mrbool.com/how-to-implement-popup-window-in-android/28285

+0

감사합니다. 나는 팝업창을 부 풀릴 때 아무런 문제도 없었다. 문제는 popupwindow가 고정되어있는 뷰에 터치 이벤트를 전달할 수 없다는 것입니다. 지금까지 알 수 있듯이이 구현은 제공된 코드 스 니펫에 포함되어 있지 않습니다 – rperryng

+0

Rperryng ... 무례해야합니까? "고마워?" Hitesh가 귀하의 질문을 해석하는 동안 도움을 주려고한다는 점을 감안할 때 부적절합니다. –

+0

네 말이 맞아. 부적절했습니다. Hitesh! – rperryng