Take the 2-minute tour ×

I'm trying to subclass an ExpandableListView so that I can add distinct groups.

By distinct groups, I mean that each group contains a different child view type (where all the childeren are the same throughout a group).

That is:

Group 1
   TextView 1
   TextView 2
   TextView 3
Group 2
   EditText 1
   EditText 2
   EditText 3

I tried stacking two ELV ontop of each other in a LinearLayout, however the main behavior I'm trying to obtain (which is the only reason I'm using a ELV in the first place) is lossed: each list becomes independently scrollable, where if a single ELV is used that contains multiple groups, they will all scroll together).

I've already seen the post

How to add image in expandable List in parent in android?

However this isn't quite what I'm looking for. This solves the problem for multiple, distinct children within a group:

Group 1
    TextView 1
    Image 1
    TextView 2
    Image 2

I'm sure with enough time I might be able to figure it out from this, but I wanted to see if anyone has already done it before.

I already implemented my ELV adapter and got everything compiling and running ok, and am now ready to adapt the code to solve this particular problem.

Thank you.

share|improve this question
Here is what I came up with: stackoverflow.com/questions/14274392/… –  Samus Arin Jan 29 '13 at 21:45

1 Answer 1

up vote 2 down vote accepted

there's no support for that from android. one common approach is to make a wrapper object for you list elements. for example, suppose you want to show Cars and Persons in the list, then you'd do something like

class ListElement {
  Car car = null;
  Person person = null;

at runtime, either car or person is null. in your implementation of getView(), you do something like,

ListElement e = getItem(position);
if (e.car != null) {
  // create view for Car
} else if (e.person != null) {
  // create view for Person
} else {
  // error!

if you use your convert view (which you should), you have to be careful. for example, if convert view was inflated for a Car, and you are supposed to be getting a view for a Person. a way to handle this is to create an uber view that contains both Car and Person views, and then you just hide or show the respective part in getView().

<FrameLayout ...>
  <LinearLayout android:id="@+id/car_layout" ... />
  <LinearLayout android:id="@+id/person_layout" ... />

another way is to have two separate expandable ListViews.

share|improve this answer
Heh, I was worried the most about convertView b/c I noticed getView checks if its null, and if its not then it just uses whatever convertView is (which could result in a type mismatch as your pointing out). I've also come accrossed getChildType(int groupPos, int childPos), could this be overriden as well to deal with typing ? –  Samus Arin Jan 28 '13 at 22:13
Yes, getChildType() is actually the preferred way. But the solution above works as well. –  Andrew Jan 28 '13 at 22:16
Thanks man. I'm going to use your approach first to get it working, then if I'm feeling saucy I'll go for the getChildType glory (heh). –  Samus Arin Jan 28 '13 at 22:18
agree w/ @Andrew. –  Jeffrey Blattman Jan 29 '13 at 0:13
There is a getGroupType as well. I'm either going to have to do some experimenting, or find an example. I don't think it'll be too difficult to get working. –  Samus Arin Jan 29 '13 at 14:04

Your Answer


