I'm trying to program an Android interface which uses an expandable list on the side and a fragment on the other side so I can load different resources by clicking on the children of the expandable list. But unfortunately I cannot find any good tutorials about this list anywhere. Yes I've look in the API demos and I've made a normal list with a BaseExpandableListAdapter but still, understanding those list well it's kinda hard without a good tutorial, do you have any good one around or info I could check?
Asked
Active
Viewed 9.5k times
51
-
3I can't believe this was marked as "not constructive"! It was very constructive, and the answers very helpful! Thanks for posting this question, DeX03. – Doug Barbieri Sep 15 '17 at 05:27
-
I believe this flag was because you don't ask questions on StackOverflow that you can easily research yourself. – barnacle.m Oct 22 '19 at 11:58
3 Answers
66
Create item list
List<ParentItem> itemList = new ArrayList<ParentItem>();
ParentItem parent1 = new ParentItem();
parent1.getChildItemList().add(new ChildItem());
parent1.getChildItemList().add(new ChildItem());
parent1.getChildItemList().add(new ChildItem());
ParentItem parent2 = new ParentItem();
parent2.getChildItemList().add(new ChildItem());
parent2.getChildItemList().add(new ChildItem());
parent2.getChildItemList().add(new ChildItem());
itemList.add(parent1);
itemList.add(parent2);
ExpandableListViewAdapter adapter = new ExpandableListViewAdapter(context, itemList);
Data Objects
public class ParentItem {
private List<ChildItem> childItemList;
public ParentItem() {
childItemList = new ArrayList<ChildItem>();
}
public List<ChildItem> getChildItemList() {
return childItemList;
}
}
public class ChildItem {
// filll with your data
}
Adapter
public class ExpandableListViewAdapter extends BaseExpandableListAdapter {
private static final class ViewHolder {
TextView textLabel;
}
private final List<ParentItem> itemList;
private final LayoutInflater inflater;
public ExpandableListViewAdapter(Context context, List<ParentItem> itemList) {
this.inflater = LayoutInflater.from(context);
this.itemList = itemList;
}
@Override
public ChildItem getChild(int groupPosition, int childPosition) {
return itemList.get(groupPosition).getChildItemList().get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public int getChildrenCount(int groupPosition) {
return itemList.get(groupPosition).getChildItemList().size();
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
final ViewGroup parent) {
View resultView = convertView;
ViewHolder holder;
if (resultView == null) {
resultView = inflater.inflate(android.R.layout.test_list_item, null); //TODO change layout id
holder = new ViewHolder();
holder.textLabel = (TextView) resultView.findViewById(android.R.id.title); //TODO change view id
resultView.setTag(holder);
} else {
holder = (ViewHolder) resultView.getTag();
}
final ChildItem item = getChild(groupPosition, childPosition);
holder.textLabel.setText(item.toString());
return resultView;
}
@Override
public ParentItem getGroup(int groupPosition) {
return itemList.get(groupPosition);
}
@Override
public int getGroupCount() {
return itemList.size();
}
@Override
public long getGroupId(final int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View theConvertView, ViewGroup parent) {
View resultView = theConvertView;
ViewHolder holder;
if (resultView == null) {
resultView = inflater.inflate(android.R.layout.test_list_item, null); //TODO change layout id
holder = new ViewHolder();
holder.textLabel = (TextView) resultView.findViewById(android.R.id.title); //TODO change view id
resultView.setTag(holder);
} else {
holder = (ViewHolder) resultView.getTag();
}
final ParentItem item = getGroup(groupPosition);
holder.textLabel.setText(item.toString());
return resultView;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
Gives you
==============================
+Parent 1
==============================
-child 1.1
==============================
-child 1.2
==============================
-child 1.3
==============================
+Parent 2
==============================
-child 2.1
==============================
-child 2.2
==============================
-child 2.3
==============================

Dmytro Danylyk
- 19,684
- 11
- 62
- 68
-
1but, how can i handle the click event on the children of the lists? cause that's the main issue with all the tutorials i've found – DeX03 Mar 22 '12 at 20:55
-
-
2I'm getting null `holder.textLabel.setText(item.toString());`, How fixed it? – Pratik Butani Mar 01 '14 at 08:21
-
It means "holder.textLabel" is null, are you are inflating it correct? – Dmytro Danylyk Mar 01 '14 at 09:19
-
Yes, your code is just pasted. I got compile time error at `private static final class ViewHolder` that is `The member type ViewHolder cannot be declared static; static types can only be declared in static or top level types`, after i have removed `static`, it gives me run time error. – Pratik Butani Mar 01 '14 at 09:38
-
-
Thank you, this is the same data structure I am using, but I couldn't find any examples that validated it would work. One dumb question though. When the data changes, how do you tell the adapter to update the ExpandableListView? Would you add an updateData(List<> data) method? If so, what triggers the list update? Thanks. – John Ward Feb 05 '17 at 04:18
-
@JohnWard call https://developer.android.com/reference/android/widget/BaseAdapter.html#notifyDataSetChanged() – Dmytro Danylyk Feb 05 '17 at 08:59
-
Thanks a lot for this. Perfect, simple and well-explained implementation. Although, if you've a complex layout for the child (not just a textview), how will you create a separate viewholder for it? – Asim Apr 04 '18 at 15:33
-
How do I fill data For ParentItem as Heading and fill data For ChildItem ? – Noor Hossain Sep 22 '20 at 09:05
51
you can find working example of the expandable list view by following links:
for click on child, you can handle this way.
getExpandableListView().setOnChildClickListener(new OnChildClickListener() {
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// your code...
}
});
Hope this will help you. Thanks..

Terry
- 14,529
- 13
- 63
- 88

Never Quit
- 2,072
- 1
- 21
- 44
-
but that doesn't work the same way for activities which do not extend ExpandableListActivity because that's what i have, how do i fixed to work in that one? – DeX03 Mar 23 '12 at 13:57
-
1
This way you can handle events:
getExpandableListView().setOnChildClickListener(new OnChildClickListener()
{
public boolean onChildClick(ExpandableListView parent,
View v, int groupPosition, int childPosition, long id)
{
// your code...
}
}

davidcesarino
- 16,160
- 16
- 68
- 109

Kaushal
- 37
- 1
- 4