4

Scope: - using overriden ArrayAdapter; - using selector; - using isEnabled for disabling items.

Aim: - disable some list items and load disabled state view via selector.

Problem: - everything works (custom view, selector for unfocused, focuesd and pressed states) but disabled items don't use selector for disabled state.

Investigating: when I use isEnabled for disabling some items in listview Hierarchy Viewer shows that disabled items are unfocusable, unclickable but(!) enabled.

Is it a bug or something is missing?

P.S. actually, docs say that isEnabled doesn't do setEnabled(false) for list item, it makes it a divider(?) object. P.P.S I also tried to use if-statement to set my View (in getView) as isEnabled(false). But it works only for focused items?

My Selector looks like:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Disabled -->
    <item 
        android:state_enabled="false"
        android:textColor="@color/greyDark"
        android:drawable="@drawable/list_item_disabled" />
    <!-- Pressed -->
    <item 
        android:state_enabled="true"
        android:state_pressed="true"
        android:textColor="@android:color/white"
        android:drawable="@drawable/list_item_pressed" />
    <!-- Focused -->
    <item
        android:state_enabled="true"
        android:state_focused="true"
        android:textColor="@android:color/white"
        android:drawable="@drawable/list_item_focused" />
    <!-- Default -->
    <item 
        android:state_enabled="true"
        android:drawable="@drawable/list_item_unfocused" />
</selector>

2 Answers 2

9

The function isEnabled() in the adapter only makes item unfocusable and unclickable. You need to call view.setEnabled() at the end of adapter.getView() to make your selector functional.

Also, for a parent view to pass enable state to its descendants, you need to specify the attribute android:duplicateParentState="true" to the child views in your xml file.

1
  • Note to future readers: don't do what I did and assume I only needed the first part because I didn't have nested layouts in my XML layout file!
    – cqcallaw
    Jul 22, 2014 at 2:44
1

Hack: use getView to check disabled item logic and inflate view with another layout. isEnabled is still usefull.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View row;

    if (mListItem[position].isEnabled() == false) row = inflater.inflate(
            R.layout.list_row_disabled, null);
    else {
        row = inflater.inflate(R.layout.list_row, null);
        // set right extensible icon
        if (mListItem[position].getType()) {
            ImageView ic_arrow = (ImageView) row.findViewById(R.id.list_row_arrow);
            ic_arrow.setImageResource(R.drawable.ic_arrow_right);
        }
    }
    // set left icon
    ImageView ic_item = (ImageView) row.findViewById(R.id.list_row_icon);
    ic_item.setImageResource(mListItem[position].getIcon());
    // blend icon if item is disabled
    if (mListItem[position].isEnabled() == false) 
        ic_item.setColorFilter(0x99D0D0D0,Mode.SRC_ATOP); // make icons look grayer 

    // set title text
    TextView txvTitle = (TextView) row.findViewById(R.id.list_row_title);
    txvTitle.setText(mListItem[position].getTitle());

    return row;
}

@Override
public boolean isEnabled(int position) {
    return mListItem[position].isEnabled();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.