You need to use ExpandableListView, example here.
- Define two XML layouts for Group view and Child view, add
TextView
to each layout for showing item text and description.
- In code, set Item text to group's
TextView
and Description to child TextView
, all children views will be hidden initially.
- Use
ExpandableListView
's collapseGroup()
and expandGroup()
methods to collapse or expand the groups.
For example, to collapse expanded groups before showing Description, first define getGroupCount()
method in your ListAdapter
class:
@Override
public int getGroupCount() {
return yourList.size();
}
then call this method to collapse previously expanded groups:
private void collapseAll() {
int cnt = listAdapter.getGroupCount();
for (int i = 0; i < cnt; i++){
yourList.collapseGroup(i);
}
}
EDIT:
You can use TagHandler class with examples here and here to show dynamic child list on child's TextView
by using:
Html.fromHtml("<ul><li>Item 1</li>...</ul>", null, new MyTagHandler()))
For that you need to transform child list's item text to HTML text by iterating over your dynamic List/ArrayList
and making/appending HTML tags with string
data inside them.