Question

For my project, I have a Spinner tied to the ActionBar. I have implemented a custom Adapter for the Spinner, which extends BaseAdapter and implements SpinnerAdapter. I did this as a custom Adapter because my Spinner has to display expanding/collapsing nodes with carats, and special checkmarks to indicate the user's choice of node within a tree to select. All of this is working fine, except when the user presses a node to select, I don't know how to dismiss the Spinner. It just hangs around until the user collapses it by pressing the top view (the button used to expand the Spinner in the first place), or by pressing the Back button. Obviously I have overlooked something in my implementation of my SpinnerAdapter, but I'm having a fit trying to discover what it is. I have custom ViewGroups being inflated within the Adapter, and some of their children have their own click handlers. Is this messing up my Spinner?

Was it helpful?

Solution

OK, I was clued into the answer by the extremely knowledgeable Ian Lake.

My understanding is that the Action Bar's Spinner will respond to click events when the top-level items on the list (your individual list items) are allowed to have focus and do not otherwise respond to click events. However, if you are using ViewGroups as your list items, and you have child views which can take click events, unless you explicitly prevent it, they will steal focus, and your Spinner will never process the click events. Sometimes, this is exactly what you want: you might have some switches in your layout that you want to allow the user to toggle without collapsing the Spinner. However, if it's NOT what you want, then you have a couple of options:

  1. You can put the attribute android:descendantFocusability="blocksDescendants" into the root element of your layout, to make sure that focus doesn't go to the child views of the layout; or

  2. You can put the attribute android:focusable="false" to any of your child views which can be focusable.

If your child views at this point still have click handlers of their own, they will still respond to click events, and this will prevent the Spinner from collapsing. If this is not what you want, then you must remove the click handlers and make these views non-clickable.

Any errors in the foregoing discussion are mine alone and not Ian's.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top