2013-12-09 2 views
1

Android 애플리케이션을 개발 중이며 OnTouch을 전체 화면에 캡쳐하려고합니다.. 모두 내 activities은 두 개의 buttons 헤더와 그 다음 변경 될 본문이 있습니다. Activity에서Android : 루트보기 (RelativeLayout)의 onTouchListener가 작동하지 않습니다.

나는 몸이 ListView입니다 지금 테스트 해요, 그래서 Activity가 있습니다

- 두 Buttons을 화면 상단에.

- ListView 이러한 두 개의 버튼 아래.

전체 화면에서 onTouchEvents을 캡처하고 싶습니다.. OnTouchListener을 내 루트 RelativeLayout으로 설정하고 clickable = false 및 focusable = false를 다른 모든보기에 설정했지만 작동하지 않습니다. onTouch 이벤트는 첫 번째 버튼을 클릭했을 때만 트리거됩니다. 이것은 @ 레이아웃/헤더의 내용입니다

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/root" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:clickable="true" 
    android:focusable="true" 
    android:focusableInTouchMode="true" 
    android:orientation="vertical" 
    tools:context=".MainActivity" > 

    <include 
     layout="@layout/header" 
     android:clickable="false" 
     android:focusable="false" /> 

    <ListView 
     android:id="@+id/lvLocations" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@id/header_layout" 
     android:clickable="false" 
     android:focusable="false" /> 

</RelativeLayout> 

:

내 레이아웃입니다

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/header_layout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:clickable="false" 
    android:focusable="false" 
    android:weightSum="5" > 

    <Button 
     android:id="@+id/homeButton" 
     android:layout_width="0dip" 
     android:layout_height="50dp" 
     android:layout_marginRight="5dp" 
     android:layout_weight="4" 
     android:clickable="false" 
     android:focusable="false" 
     android:onClick="Home" 
     android:text="@string/home" /> 

    <ImageButton 
     android:id="@+id/speechButton" 
     android:layout_width="0dip" 
     android:layout_height="50dp" 
     android:layout_weight="1" 
     android:clickable="false" 
     android:focusable="false" 
     android:onClick="ClickMediaButton" 
     android:src="@drawable/micro" /> 

</LinearLayout> 

는 그리고 이것은 내가 사용하는 코드입니다 :

findViewById(R.id.root).setOnTouchListener(new OnTouchListener() {   
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      Log.d("MACT", "TOUCH!"); 
     } 
    }); 

내가 말했듯이 홈 로그 버튼을 클릭 한 경우에만 내 로그에 TOUCH 이 표시됩니다.. 왜 그런가요? 내가 도대체 ​​뭘 잘못하고있는 겁니까?

감사합니다.

답변

0

마침내 문제를 해결하고 해결하기 위해 레이아웃을 다시 디자인했습니다.

<ListView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/myList" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

가 그럼 난 내 ListView에에 헤더 레이아웃을 추가합니다 :

View header = (View)getLayoutInflater().inflate(R.layout.header,null); 
mList.addHeaderView(header); 

은 이제리스트 뷰에 onTouchListener를 설정 이제 대신 헤더와 ListView에있는의 난 그냥 내 레이아웃의 목록보기가 그리고 voilà, 나는 전체 화면에서 onTouch 이벤트를받습니다.

나는 이것이 제한적인 해결책이며 복잡한 레이아웃에서는 사용할 수 없다는 것을 알고 있지만 필자가 필요로하는 화면에서 사용할 수 있습니다.

+1

흥미 롭습니다. 필자가 생각하는 것은 터치를 캡처하는 여러보기 (예 : 목록보기, 머리글, 단추)가 있기 때문에 원하는 방식으로도이를 알릴 수 없기 때문입니다. 나는 당신이 커스텀 뷰를 만들었고, 그것들 중 하나가 움켜 잡기 전에 터치를 붙잡은 다음, 디렉터 타입 패턴을 사용하여 어떤 일이 발생했는지를 결정하는 것은보다 복잡한 레이아웃에서 일어날 수 있다고 확신한다. 이전에 viewpagers에지도 또는 그리드를 배치하는 것과 비슷한 문제가있었습니다. 그냥 생각. – Rarw

+0

정말 재미있는 의견, 나는 결코 많은 것을 사용자 지정보기로 가져 가지 않아서, 이것에 대해 생각하지 않았습니다. 좋은 접근 방식처럼 보인다 :) –

0

RelativeLayout의 터치 이벤트를 포착하는 사용자 정의보기를 만든 다음 원하는대로 할 수 있습니다.

public class CustomRelativeLayout extends RelativeLayout{ 

    CustomRelativeLayout(Context context, AttributeSet attrs){ 
     super(context, attrs); 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent ev){ 
     //handle touch event 
     return true; 
    } 
} 

그러면 XML에서이 네임 스페이스의 클래스를 찾아서이 상대 레이아웃을 사용하게됩니다. 물론 원하는 수정자를 추가 할 수 있지만 여기에 보이는 것과 같은 예가 있습니다.

<com.example.whatever.your.project.CustomRelativeLayout 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent"/> 

일단 터치하면 원하는대로 할 수 있습니다. 다른 액티비티로 전달하거나 다른 메서드를 호출 할 때 사용할 수 있습니다. 사용자 정의 뷰를 사용하는 방법에 대한 자세한 내용은 see this 안드로이드 문서의 일부입니다.

+0

방금 ​​시도한 결과가 같고 onTouchEvent는 첫 번째보기 (홈 버튼)를 터치 할 때만 호출됩니다. 다른보기를 클릭해도 아무 일도 일어나지 않습니다. –

관련 문제