2013-12-09 37 views
1

我正在開發一個Android應用程序,我試圖在我的整個屏幕中捕獲OnTouchevents。我的所有activities都會有一個標題,其中包含兩個buttons,然後是一個正在改變的主體。在屏幕的頂部Android:onTouchListener在根視圖(RelativeLayout)不起作用

- 兩個Buttons

Activity,我現在測試的身體是一個ListView,所以Activity了。

- 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!"); 
     } 
    }); 

就像我說的,我的日誌只有當homeButton被點擊時才顯示TOUCH 。這是爲什麼?我究竟做錯了什麼?

謝謝!

回答

0

我終於設法解決它,並做到這一點,我重新設計了我的佈局。現在不是有一個標題和一個ListView我只是在我的佈局一個ListView:

<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而且,我在整個屏幕上收到了onTouch事件。

我知道這是一個限制性的解決方案,它不能用於複雜的佈局,但我可以在我需要的屏幕上使用它。

+1

有趣。我認爲發生的事情是,因爲有多個視圖捕捉觸摸 - 例如列表視圖,標題,按鈕 - 無法推薦,即使你想要的方式。我敢打賭,如果您製作了自定義視圖,在它們被任何視圖抓住之前抓住了觸摸,然後使用了導演類型模式來確定在更復雜的佈局下會發生什麼情況。在將視圖或網格放入視圖頁面之前,我曾遇到過類似的問題。只是一個想法。 – 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部分android文檔的更多信息。

+0

我剛剛嘗試過,結果相同,onTouchEvent僅在我觸摸我的第一個視圖(homeButton)時調用,如果我單擊任何其他視圖都不會發生任何事情。 –

相關問題