views:

406

answers:

1

I have just started my career as an android programmer, and am currently relying heavily on the sample code and api examples. I have been working with this api example, to produce an expandable list of items (note this example does not use the ExpadableListView).

In playing with the example, I tried to add another widget that would become visible and be gone at the same time as the text (mDialogue in the sample code). This works well with another TextView, but as soon as I tried to add a button widget, it stopped working. The list would expand on first click, showing my hidden TextView and Button, but it will not disappear on further clicks. The button is however, clickable, and I was able to set up an onClick listener to change the button text back and forth.

I'm starting to wonder, is it just not possible to have a clickable item inside a clickable list item? Or is there some kind of work around? Would it solve my problem if I used ExpandableListView?

+2  A: 

You have two options of how to handle focus within a ListView controlled by ListView#setItemsCanFocus(boolean). If you want individual Views within a list item to focus so that the user can interact with them individually instead of with the list item as a whole, call it passing true. false is the default behavior.

The default behavior where ListView manages item focus and clicks is basically a shortcut/optimization for the common case where the whole item acts as a single unit from an interaction standpoint, but its layout may be complex. When you tell ListView that its items can focus it disables this special behavior and you should use the more traditional mechanisms of handling events on the Views within the list items. (Listeners, overridden on* methods, etc.)

But why do your list items stop taking clicks when your ListView isn't set up for focusable items? ListView will only generate item click events if the list item view returns false from View#hasFocusable(). This means no children of the list item can be focusable if you want to receive item click events for it. As soon as your button becomes visible, the list item has a focusable child and will no longer receive list item click events.

adamp
Ahh... I see! Once I changed it to button.setFocuasable(false) then everything behaved as expected. Thanks for the explanation! This seems less like black magic now.
Aurora