I\'m pretty new to Android dev and still working out a lot of things.
I\'ve got a main menu showing using the following code, but can\'t work out how to disable sele
Or in simple way to un-register and register OnItemClickListener can be a better idea.
To register:
lstDevice.setOnItemClickListener(context);
To de-register:
lstDevice.setOnItemClickListener(null);
You can disable a list item (= make it not respond to touches) by calling both
setClickable(false)
and
setFocusable(false)
in your adapter, for example.
By default, this is not automatically reflected graphically, though.
I am currently using that in a list whose list items are not clickable but most of which contain clickable widgets. Works well.
This way, list items are drawn normally including the separator (see Janusz' reply to the accepted answer above).
In order to disable list items on list creation you have to subclass from ArrayAdapter
. You have to override the following methods: isEnabled(int position)
and areAllItemsEnabled()
. In former you return true
or false
depending is list item at given position enabled or not. In latter you return false
.
If you want to use createFromResource()
you will have to implement that method as well, since the ArrayAdapter.createFromResource()
still instantiates ArrayAdapter
instead of your own adapter.
Finally, the code would look something like the following:
class MenuAdapter extends ArrayAdapter<CharSequence> {
public MenuAdapter(
Context context, int textViewResId, CharSequence[] strings) {
super(context, textViewResId, strings);
}
public static MenuAdapter createFromResource(
Context context, int textArrayResId, int textViewResId) {
Resources resources = context.getResources();
CharSequence[] strings = resources.getTextArray(textArrayResId);
return new MenuAdapter(context, textViewResId, strings);
}
public boolean areAllItemsEnabled() {
return false;
}
public boolean isEnabled(int position) {
// return false if position == position you want to disable
}
}
I believe whether a list item is enabled or not is part of that item's state, so I guess you have to manage that in your ListAdapter
. When subclassing an adapter, you can override isEnabled(position)
. For any position you return true here, the ListView will mark this item as disabled.
So what you want to do is something like this:
class MenuAdapter extends ArrayAdapter<String> {
public boolean isEnabled(int position) {
// return false if position == positionYouWantToDisable
}
}
This probably requires e.g. a Map
managing the enabled state of each item if you want to be able to enable/disable an item using a setter.
Then set the custom adapter on your ListView.