2012-10-11 28 views
2

我想禁用某些區域的調度觸摸,以便您理解這裏是我的屏幕。如何在屏幕的某些部分禁用調度觸摸事件

enter image description here

你可以看到頁眉,頁腳和MapView的圖像,但是當我點擊位置按鈕(右側頭),我的地圖也越來越通知(就像它得到了感動),我不想。我希望只有按鈕的onClick事件應該被點擊而不是調度事件。

這裏是我的XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/LinearLayout1" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<!-- include title bar for all screen --> 

<include 
    android:id="@+id/include1" 
    layout="@layout/titlebar_layout" 
    android:layout_gravity="top" /> 



<FrameLayout 
    android:layout_gravity="center" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="0.88" 
    > 

    <com.google.android.maps.MapView 
     android:id="@+id/map" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1.63" 
     android:apiKey="0VkXbAOFvAq7b6uaGHSmnS2a2VosPxoS6ceHY_g" 
     android:clickable="true" > 
    </com.google.android.maps.MapView> 


</FrameLayout> 

<include 
    android:id="@+id/include2" 
    android:layout_gravity="bottom" 
    android:layout_marginBottom="38dp" 
    layout="@layout/bottom_layout" /> 

</LinearLayout> 

任何幫助將不勝感激。

回答

7

您可以覆蓋dispatchTouchEvent,並檢查其中用戶觸摸屏幕..

添加點擊事件
@Override 
public boolean dispatchTouchEvent(MotionEvent ev) {  
    //first check if the location button should handle the touch event 
    if(locBtn != null) { 
     int[] pos = new int[2]; 
     locBtn.getLocationOnScreen(pos); 
     if(ev.getY() <= (pos[1] + locBtn.getHeight()) && ev.getX() > pos[0]) //location button event 
      return locBtn.onTouchEvent(ev); 
    } 
     return super.dispatchTouchEvent(ev); 
} 
+0

哎爲answer..i'll感謝很快就回來接受你的答案,如果這個工程的答案 –

+0

遺憾沒work..but感謝 –

+0

+1的答案 –

0

使用帶有地圖視圖相對佈局在底部和按鈕,在點擊頂部..和按鈕

2

爲什麼MapView的正在接收dispatchTouchEvent()事件的原因是,Android的啓動dispacthing最後添加的視圖的事件,如果它不處理存在,那麼它分派到一個之前添加的最後一個視圖等上...

在您的xml佈局中,首先添加Header,然後添加MapView和最後一個頁腳。所以在你的情況下,事件首先分派給Footer,然後分派到MapView,最後分派給Header。

你必須選擇來解決這個:

  • 最簡單的就是重新排列布局的項目,從MapView類,然後將所有其他人。您可能需要使用相對佈局才能正確定位它們。使用此MapView將不會看到按鈕處理的事件。
  • 保持您的佈局並讓MapView接收事件。 MapView將需要測試事件是由他處理還是被忽略。爲此,您可以使用由@Nunu提供的代碼,該代碼應該可以工作。也許你需要添加到按鈕Y座標按鈕的高度。

祝你好運。

+0

+1沒有人可以阻止我按Upvote這樣一個很好的解釋..感謝很多 –

+0

我會很快回來欣賞你的答案..:) :) –

+0

@路易斯..再次感謝您的第二個選項(也建議由@Nunu),但沒有工作.. :(你是什麼意思加入y座標到按鈕高度??在此先感謝 –

0

使用view.bringToFront()獲取其他視圖上方的視圖位置。