2012-07-05 75 views
4

我有一個ListView並且第一次顯示它我想禁用它的一些項目。當android:state_enabled =「false」時無法設置顏色

爲此,我提出這個textorange_selected.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_enabled="false" 
      android:color="@color/medium_gray" /> 

    <item android:state_selected="true" 
     android:color="@android:color/white" /> 

    <item android:state_focused="true" 
     android:color="@android:color/white" /> 

    <item android:state_pressed="true" 
     android:color="@android:color/white" /> 

    <item android:color="@color/lighter_orange" /> 
</selector> 

我用它在一排的佈局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="20dp" 
    android:gravity="left|center" 
    android:layout_width="wrap_content" 
    android:paddingLeft="5dp" 
    android:paddingRight="5dp" 
    android:background="@color/list_bg"> 

    <ImageView 
     android:id="@+id/avatar" 
     android:layout_width="48dp" 
     android:layout_height="48dp" 
     android:layout_marginTop="10dp" 
     android:layout_alignParentLeft="true" /> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="wrap_content" 
     android:layout_height="48dp" 
     android:layout_toRightOf="@+id/avatar" 
     android:layout_marginLeft="10dp" 
     android:layout_marginTop="5dp"> 


      <TextView android:id="@+id/listitem1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="2dp" 
       android:textColor="@drawable/textblack_selected" 
       android:textSize="18dp" 
       android:textStyle="bold" /> 

      <TextView android:id="@+id/listitem2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="40dp" 
       android:textColor="@drawable/textorange_selected" /> 

    </LinearLayout> 

    <ImageView 
     android:id="@+id/arrowImage" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="25dp" 
     android:layout_alignParentRight="true" 
     android:src="@drawable/arrow_selector" /> 

</RelativeLayout> 

的問題是,它從未進入android:state_enabled="false"(那應該是該項目被禁用的時刻,並且我將文本的顏色設置爲灰色)。

對於禁止我使用此代碼的項目中我的適配器:

@Override 
public boolean areAllItemsEnabled() {      
    return false; 
} 

@Override 
public boolean isEnabled(int position) { 
    return position < 1; 
} 

哪裏是我的錯?

+0

您正在禁用Java中的TextView? – 2012-07-05 09:06:37

+0

看到我的編輯。我把代碼如何禁用ListView的項目。我在android – Gabrielle 2012-07-05 09:09:30

+0

工作,relativelayout被禁用,不確定textview是。你可以嘗試用duplicateParentState將禁用狀態傳遞給textview – njzk2 2013-03-14 08:37:06

回答

2

選擇器在其分配的視圖上使用值isEnabled(),在這種情況下爲RelativeLayout。它不會在Adapter上撥打isEnabled()

您需要的代碼,如:

public View getView(int position, View convertView, ViewGroup parent) { 
    RelativeLayout v = (RelativeLayout)LayoutInflater.from(parent.getContext()) 
      .inflate(R.layout.listitem_relativelayout, null); 
    if(postion < 1) v.setEnabled(false); 
1

的問題是,由isEnabled()返回啓用狀態自動轉發到一個列表項的所有子視圖。 您需要做的是在適配器的getView()方法的末尾調用view.setEnabled()以使選擇器正常工作。

此外,確保您通過在TextView本身以及您的LinearLayout包裝上指定android:duplicateParentState="true"來將狀態傳遞給子代。