2013-08-22 67 views
0

我使用ListView的適配器,我的代碼,列表視圖適配器的按鈕不能點擊

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    > 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="10dp" 
     android:padding="10dp" 
     android:background="@android:color/darker_gray" 
     android:orientation="vertical" > 
     <TextView 
      android:id="@+id/tv_position" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textColor="@android:color/white" 
      android:text="position1" 
      android:textSize="20sp" 
      /> 
     <Button 
      android:id="@+id/btn_like" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Button" 
      /> 
    </LinearLayout> 
    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_margin="10dp" 
     android:background="@drawable/selector_translate_blue" 
     android:clickable="true" //Add this is to allow this to get focus, but the button can not be clicked. 
     > 
    </RelativeLayout> 
</FrameLayout> 

當我刪除的android:點擊=「true」,則可以單擊該按鈕,但RelativeLayout的無法聚焦。 如何實現與Google Plus相似的效果?

UPDATE 現在只有RelativeLayout可以被點擊。 我的意思是如何使Button也可以被點擊? 不要同時點擊它們兩個。

回答

0

嘗試設置android:focusableandroid:focusableInTouchModefalse到您的按鈕:

<Button 
      android:id="@+id/btn_like" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Button" 
      android:focusable="false" 
      android:focusableInTouchMode="false" 
      /> 

UPDATE 你需要設置你的Buttonandroid:focusablefalse,也是RelativeLayout。另外,你需要爲你的兩個Button一個selectorRelativeLayout 這裏:

<Button 
     android:id="@+id/btn_like" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:focusable="false" 
     android:background="@drawable/selector"/> 

這裏:

<RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_margin="10dp" 
     android:background="@drawable/selector_translate_blue" 
     android:background="@drawable/selector_ll" 
     android:focusable="false" > 

,這裏是最重要的事情,在那些selector S,在android:state_pressed,你需要增加更多的android:state_focused,這樣,當物品點擊時,ButtonRelativeLayout不能得到focusable,所以它不會觸發這個狀態:

selector.xml:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@android:color/darker_gray" android:state_pressed="true" android:state_focused="true"></item> 
</selector> 

selector_ll.xml:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" android:state_focused="true" 
      android:drawable="@android:color/darker_gray"/> <!-- pressed --> 
    <item android:state_focused="true" 
      android:drawable="@android:color/darker_gray"/> <!-- focused --> 
    <item android:drawable="@android:color/white"/> <!-- default --> 
</selector> 

希望這有助於。

+0

Thanks.No use ah ...,我希望Button和RelativeLayout都可以被點擊。 – fatsoon

+0

@tenpage看起來您的'LinearLayout'與您的'RelativeLayout'重疊,因此您不能觸及'RelativeLayout',將'LinearLayout'的'onClick'事件傳遞到'RelativeLayout'。查看我的更新。乾杯! – user2652394

+0

對不起,我的表情錯誤。 – fatsoon

0

您是否試過在您的相關佈局中添加android:focusable =「true」或android:focusableInTouchMode =「true」?

+0

是的,我嘗試過,但RelativeLayout無法集中。 – fatsoon