注意:如果您因爲遇到類似問題而來到這裏,請確保您閱讀並嘗試提供的所有答案。看來在某些情況下,人們可能比另一個人工作得更好。所以做你自己的測試!行內容獨立可點擊區域
前言
我ListView的行佈局像這樣(這是剝奪了我的真實佈局的版本,所以不要的東西像包裹在的LinearLayout等單要素評價 - 這只是爲了說明問題 - 最終版本得到了更多的元素存在):
XML文件如下以供參考,但我簡短描述我想達到的目標:
- 藍框(圖標)和「此處通常爲長文本」TextView爲行「真實」內容(
item_main_container
)。它可以是任何東西 - 它應該是無關緊要的。此圖層內容不可點擊。事實上(但我剛剛意識到),我應該顯示「藍色方塊」被紅色半覆蓋,而下一層被綠色方框覆蓋一半。 - 紅色和綠色框是視圖我綁定行的OnCliclListeners(包裹在
item_click_area_container
) - 我只是希望用戶能夠點擊行,不管item_main_container
的內容真的是(所以這些紅色/綠色是透明的應用程序)。 注意:它們在圖像上設置爲透明,因此您可以通過它看到item_main_content
,但它僅用於演示目的。另請注意,紅色和綠色代表單獨的動作,我需要能夠在item_click_area_container
圖層上具有更多元素,以便能夠根據用戶點擊的位置處理更多操作。 - 黃色區域(
button_bar_container
)保存用戶可能想要點按的按鈕。
所以基本上item_click_area_container
覆蓋item_main_container
,但不button_bar_container
,也就是上面item_click_area_container
否則用戶將無法通過點擊任意按鈕,它的。在角度來看,這將是:
佈局輪廓如下:
問題
我無法正確地作出item_click_area_container
伸展覆蓋儘管我付出了努力,但它應該儘可能地保持整個item_main_container
。由於"usually long text here"
文字可以是任意長度,我不知道item_main_container
的高度是多少。在下面的XML中有android:layout_height="100dp
- 這個工作方式我看到所有的行元素,但是對於更長的文本item_click_area_container
高度太小。但如果我用android:layout_height="fill_parent"
替換它,那麼整個item_click_area_container
是不可見的,這打破了目的。
質詢
- 爲什麼這是行不通的預期和/或如何解決呢?
- 有具有比使用像我
item_click_area_container
(但不包括基於OnTouchListener解決方案)的方法依賴於內容的點擊區域沒有更好的辦法?
佈局XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/item_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/item_main_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/item_avatar"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#0000ff"
android:src="@drawable/icon_help"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:id="@+id/item_body"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Usually long text here..."
android:textColor="#ffffff"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/item_click_area_container"
android:layout_width="fill_parent"
android:layout_height="100dp">
<LinearLayout
android:id="@+id/green_click"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#7700ff00">
</LinearLayout>
<LinearLayout
android:id="@+id/red_click"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2"
android:background="#77ff0000">
</LinearLayout>
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:id="@+id/button_bar_container"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_marginTop="3dp"
android:background="#ffff00">
</LinearLayout>
</LinearLayout>
編輯#1
我想我可以使用OnTouchListener
,並在onTouch()
檢查event.getAction()
,如果它是MotionEvent.ACTION_DOWN
[R先閱讀事件的X/Y並檢查視圖的邊界。這可能會訣竅,但是,如果可能的話,我寧願單獨堅持使用OnClickListener。且不說它會有助於理解當前的罪魁禍首是什麼,尤其是佈局ADT
我相信加入'OnClickListener'到'item_main_container'還將努力 – Thomas
我第一次拒絕了這個提示,但休息後,我的文檔再次潛伏,我認爲它確實可以。你可以使用onTouchListener,檢查'event.getAction()',如果它是'MotionEvent.ACTION_DOWN',則讀取事件的X/Y。這應該足夠了,但是我需要保留tap中的tap區域代碼,這是不是最完美的。但仍然可以是一個進步。需要調查。謝謝:) –
正確的,應該是「button_bar_container」在覆蓋看到。糾正。是的,我也認爲它應該可以工作 - 但它不會,我不知道爲什麼。 –