Thursday, August 11, 2011

Don't use Adapter#isEnabled(int) to disable individual list items (Android)

When we have items in a ListView, we may want to disable some of the items.

Disabling some of the items can be done by overriding isEnabled(int position) on the adapter, but unfortunately the list item dividers are not drawn anymore! Overriding isEnabled(int position) was meant to be used to create some kind of "separator" or "list header".

The correct way to disable an item in a ListView is to call setEnabled(false) on the view of the item itself. In other words, call setEnabled(false) on the view that is returned from getView(int position, View convertView, ViewGroup parent).

Reference: Romain Guy's post on android-developers.

2 comments: