Use ExpandableListView

Description

The following code shows how to Use ExpandableListView.

Example

Main layout xml file


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
</LinearLayout>

Main Activity Java code


import android.content.Context;
import android.graphics.Typeface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
//from  w ww.j  av  a2  s  .c o  m
import android.app.Activity;
import android.os.Bundle;
import android.widget.ExpandableListView;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ExpandableListView list = new ExpandableListView(this);
        list.setGroupIndicator(null);
        list.setChildIndicator(null);
        String[] titles = {"Fruits","Vegetables","Meats"};
        String[] fruits = {"Apples","Oranges"};
        String[] veggies = {"Carrots","Peas","Broccoli"};
        String[] meats = {"Pork","Chicken"};
        String[][] contents = {fruits,veggies,meats};
        SimplerExpandableListAdapter adapter = new SimplerExpandableListAdapter(this,
            titles, contents);

        list.setAdapter(adapter);
        setContentView(list);

    }
}

class SimplerExpandableListAdapter extends BaseExpandableListAdapter {
  private Context mContext;
  private String[][] mContents;
  private String[] mTitles;
  
  public SimplerExpandableListAdapter(Context context, String[] titles, String[][] contents) {
    super();
    //Check arguments
    if(titles.length != contents.length) {
      throw new IllegalArgumentException("Titles and Contents must be the same size.");
    }
    
    mContext = context;
    mContents = contents;
    mTitles = titles;
  }
  
  //Return a child item
  @Override
  public String getChild(int groupPosition, int childPosition) {
    return mContents[groupPosition][childPosition];
  }

  //Return a item's id
  @Override
  public long getChildId(int groupPosition, int childPosition) {
    return 0;
  }

  //Return view for each item row
  @Override
  public View getChildView(int groupPosition, int childPosition,
      boolean isLastChild, View convertView, ViewGroup parent) {
    TextView row = (TextView)convertView;
    if(row == null) {
      row = new TextView(mContext);
    }
    row.setText(mContents[groupPosition][childPosition]);
    return row;
  }

  //Return number of items in each section
  @Override
  public int getChildrenCount(int groupPosition) {
    return mContents[groupPosition].length;
  }

  //Return sections
  @Override
  public String[] getGroup(int groupPosition) {
    return mContents[groupPosition];
  }

  //Return the number of sections
  @Override
  public int getGroupCount() {
    return mContents.length;
  }

  //Return a section's id
  @Override
  public long getGroupId(int groupPosition) {
    return 0;
  }

  //Return a view for each section header
  @Override
  public View getGroupView(int groupPosition, boolean isExpanded,
      View convertView, ViewGroup parent) {
    TextView row = (TextView)convertView;
    if(row == null) {
      row = new TextView(mContext);
    }
    row.setTypeface(Typeface.DEFAULT_BOLD);
    row.setText(mTitles[groupPosition]);
    return row;
  }

  @Override
  public boolean hasStableIds() {
    return false;
  }

  @Override
  public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
  }

}
Use ExpandableListView




















Home »
  Android »
    Android UI »




UI Basics
Action Bar
Animation
Button
Canvas
CheckBox
Clock Date Picker
Dialog
EditText
Event
Fragment
Gesture
GridView
ImageView
Layout
ListView
Map
Menu
Model
OpenGL
ProgressBar
RadioButton
Spinner
Tab
TextView
Thread
Toast
Video
View
WebView