当android:state_enabled="false"时无法设置颜色

4

我有一个ListView,第一次显示时我想禁用其中的一些项。

为此,我制作了这个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;
}

我的错误在哪里?



你在Java中禁用TextView吗? - Mohsin Naeem
请看我的编辑。我放了代码,展示如何禁用ListView的项目。我在Android上工作。 - Gabrielle
RelativeLayout已被禁用,不确定TextView是否也被禁用。您可以尝试使用duplicateParentState将禁用状态传递给TextView。 - njzk2
2个回答

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"来将状态传递给后代。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接