The following is the first few sections of a chapter from The Busy Coder's Guide to Android Development, plus headings for the remaining major sections, to give you an idea about the content of the chapter.
Android does not have a “tree” widget, allowing users to navigate an arbitrary hierarchy of stuff. In large part, that is because such trees are difficult to navigate on small touchscreens with comparatively large fingers.
Android does have
ExpandableListView, a subclass of
ListView that supports
a two-layer hierarchy: groups and children. Groups can be expanded to show their
children or collapsed to hide them, and you can get control on various events for
the groups or the children.
Android offers an
ExpandableListActivity as a counterpart to its
However, it does not offer an
ExpandableListFragment. This is not a major issue, as
you can work with an
ExpandableListView inside a regular
Fragment yourself, just
as you would for most other widgets not named
Rather than use a
ExpandableListView, you will use an
ExpandableListAdapter, where you can control separate details for groups and children.
SimpleExpandableListAdapter, roughly analogous to
ArrayAdapter, where your data resides in a
Mapobjects for groups, and a
Mapobjects for the children
SimpleCursorTreeAdapter, roughly analogous to
SimpleCursorAdapter, for mapping data in a
Cursorto rows and columns
In many cases, though, the complexity of managing groups and children will steer
you down the path of extending
BaseExpandableListAdapter and handling all of the
view construction yourself. There are many methods that you will need to implement:
getGroupCount(), to return the number of groups
getGroupId(), to return an
intID for a group given its position
getGroupView(), to return the
Viewthat should be used to render the group, perhaps using the built-in
android.R.layout.simple_expandable_list_item_1that is set up for such groups and handles rendering the expanded and collapsed states
getChildrenCount(), to return the number of children for a given group
getChildId(), to return an
intID for a child given its position (and its group’s position)
getChildView(), to return the
Viewthat should be used to render the child, given its position and its group’s position
isChildSelectable(), to indicate if the user can select a given child, given its position and its group’s position
hasStableIds(), to indicate if the ID values you returned from
getChildId()will remain constant for the life of this adapter
There are four major events that you will be able to respond to with respect to the
user’s interaction with an
setOnGroupExpandListener()) or collapse (
If you use
setOnGroupClickListener() to be notified about clicks on a group, be sure
false from your implementation of the
onGroupClick() method required by the
OnGroupClickListener interface. If you return
true, you consume the click event, which
ExpandableListView from using that event to expand or collapse the group.
The preview of this section is unavailable right now, but if you leave your name and number at the sound of the tone, it might get back to you (BEEEEEEEEEEEEP!).
The preview of this section was stepped on by Godzilla.