2013-04-20 62 views
8

我面臨一個奇怪的問題,當我刪除按鈕行時,我在ListView中添加了一個自定義行是可選的,但是當我添加按鈕時,我無法點擊該行請參閱下面的xml。帶按鈕的Android ListView不可選(可點擊)

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="77dp" 
    android:layout_height="77dp" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:layout_marginLeft="18dp" 
    android:src="@drawable/company_logo" /> 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:text="Idds sdsad " 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

<TextView 
    android:id="@+id/textView2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_toRightOf="@+id/imageView1" 

    android:layout_below="@+id/textView1" 
    android:textColor="#8b8989" 
    android:layout_marginLeft="5dp" 
    android:text="Tap to see detail" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/textView2" 
    android:layout_centerHorizontal="true" 
    android:text="Button" /> 

請幫助爲什麼發生這種情況。

回答

18

嘗試在XML設置

android:focusable="false" 
android:focusableInTouchMode="false" 

ButtonButton重點關注該行,這就是爲什麼你不能選擇你的行。

26

正如您使用您的自定義行。

在您的getView中設置onclickListenerbutton之後,請設置其focusability false

button.setFocusable(false);

並且還爲您的行的佈局容器設置android:descendantFocusability="blocksDescendants"。 您可以直接設置android:focusable="false"但這會讓您的按鈕無法點擊。

+10

在該行的佈局容器上設置android:descendantFocusability =「blocksDescendants」足以爲我解決這個問題。我不必對按鈕的可調焦屬性做任何事情。 – harmanjd 2014-09-20 00:59:26

+0

更好的解決方案,因爲它更具說明性和高級別。非常好。 – Marchy 2017-05-08 18:50:26