Home > Android, Article > Android practice – Custom ExpandableListView Sample

Android practice – Custom ExpandableListView Sample

References: ExpandableListView

A view that shows items in a vertically scrolling two-level list. This differs from the ListView by allowing two levels: groups which can individually be expanded to show its children. The items come from the ExpandableListAdapter associated with this view.

The following shows an example of  how to create a custom ExpandableListView in an activity
1, Create a main.xml  to provide a ListView control .
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 style="@style/background"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical" >

<include layout="@layout/top" />

<ExpandableListView
 android:id="@+id/expandableListView"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:groupIndicator="@drawable/play_expand_background"
 android:listSelector="@drawable/play_expand_seletor"
 android:scrollbars="none" >
 </ExpandableListView>

</LinearLayout>

2, Custom adapter

public class ExpandableListAdapter extends BaseExpandableListAdapter {

private Context mContext;
 private ExpandableListView mExpandableListView;
 private List<GroupEntity> mGroupCollection;
 private int[] groupStatus;

public ExpandableListAdapter(Context pContext,
 ExpandableListView pExpandableListView,
 List<GroupEntity> pGroupCollection) {
 mContext = pContext;
 mGroupCollection = pGroupCollection;
 mExpandableListView = pExpandableListView;
 groupStatus = new int[mGroupCollection.size()];

setListEvent();
 }

private void setListEvent() {

mExpandableListView
 .setOnGroupExpandListener(new OnGroupExpandListener() {

@Override
 public void onGroupExpand(int arg0) {
 // TODO Auto-generated method stub
 groupStatus[arg0] = 1;
 }
 });

mExpandableListView
 .setOnGroupCollapseListener(new OnGroupCollapseListener() {

@Override
 public void onGroupCollapse(int arg0) {
 // TODO Auto-generated method stub
 groupStatus[arg0] = 0;
 }
 });
 }

@Override
 public String getChild(int arg0, int arg1) {
 // TODO Auto-generated method stub
 return mGroupCollection.get(arg0).GroupItemCollection.get(arg1).Name;
 }

@Override
 public long getChildId(int arg0, int arg1) {
 // TODO Auto-generated method stub
 return 0;
 }

@Override
 public View getChildView(int arg0, int arg1, boolean arg2, View arg3,
 ViewGroup arg4) {
 // TODO Auto-generated method stub

ChildHolder childHolder;
 if (arg3 == null) {
 arg3 = LayoutInflater.from(mContext).inflate(
 R.layout.list_group_item, null);

childHolder = new ChildHolder();

childHolder.title = (TextView) arg3.findViewById(R.id.item_title);
 arg3.setTag(childHolder);
 }else {
 childHolder = (ChildHolder) arg3.getTag();
 }

childHolder.title.setText(mGroupCollection.get(arg0).GroupItemCollection.get(arg1).Name);
 return arg3;
 }

@Override
 public int getChildrenCount(int arg0) {
 // TODO Auto-generated method stub
 return mGroupCollection.get(arg0).GroupItemCollection.size();
 }

@Override
 public Object getGroup(int arg0) {
 // TODO Auto-generated method stub
 return mGroupCollection.get(arg0);
 }

@Override
 public int getGroupCount() {
 // TODO Auto-generated method stub
 return mGroupCollection.size();
 }

@Override
 public long getGroupId(int arg0) {
 // TODO Auto-generated method stub
 return arg0;
 }

@Override
 public View getGroupView(int arg0, boolean arg1, View arg2, ViewGroup arg3) {
 // TODO Auto-generated method stub
 GroupHolder groupHolder;
 if (arg2 == null) {
 arg2 = LayoutInflater.from(mContext).inflate(R.layout.list_group,
 null);
 groupHolder = new GroupHolder();
 groupHolder.img = (ImageView) arg2.findViewById(R.id.tag_img);
 groupHolder.title = (TextView) arg2.findViewById(R.id.group_title);
 arg2.setTag(groupHolder);
 } else {
 groupHolder = (GroupHolder) arg2.getTag();
 }
 if (groupStatus[arg0] == 0) {
 groupHolder.img.setImageResource(R.drawable.group_down);
 } else {
 groupHolder.img.setImageResource(R.drawable.group_up);
 }
 groupHolder.title.setText(mGroupCollection.get(arg0).Name);

return arg2;
 }

class GroupHolder {
 ImageView img;
 TextView title;
 }

class ChildHolder {
 TextView title;
 }

@Override
 public boolean hasStableIds() {
 // TODO Auto-generated method stub
 return true;
 }

@Override
 public boolean isChildSelectable(int arg0, int arg1) {
 // TODO Auto-generated method stub
 return true;
 }

}
3,  The final effect
4, Source Code
You can download here
About these ads
Categories: Android, Article
  1. nitin
    May 31, 2012 at 6:28 am | #1

    great work ! awsome!

  2. July 8, 2012 at 2:26 am | #2

    Great work ..i have also tried to explain it in my word with source i provided at end of article
    see this http://androidtrainningcenter.blogspot.in/2012/07/android-expandable-listview-simple.html. I will be happy if some one get help from it

  3. Amber
    July 17, 2012 at 1:10 am | #3

    Thanks for sharing. Very mindful work indeed!

  4. ssgoel
    August 1, 2012 at 2:28 pm | #4

    Very good work!!

  5. PRINCE
    August 28, 2012 at 5:39 pm | #5

    best

  6. Shailendra
    October 1, 2012 at 4:05 pm | #6

    nice one! help me a lot. thank u

  7. android.pfo
    November 21, 2012 at 11:13 pm | #7

    This is the best post according to “adapter based” ExpancableListView i have ever seen! Thanks!

  8. January 3, 2013 at 9:03 pm | #8

    thank you very much. this helped a lot and is btw a good example on how to use and implement expandable listviews! (y)

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: