Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable listview items with selector and isEnabled()

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>
like image 678
Oleksii Malovanyi Avatar asked Dec 28 '22 17:12

Oleksii Malovanyi


1 Answers

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.

like image 95
Jason Lin Avatar answered Jan 09 '23 02:01

Jason Lin