Using ExpandableListView : ListView « UI « Android






Using ExpandableListView

   
package app.test;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ExpandableListView;

import android.content.Context;
import android.graphics.Typeface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
public class Test extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ExpandableListView list = new ExpandableListView(this);
        list.setGroupIndicator(null);
        list.setChildIndicator(null);
        String[] titles = {"A","B","C"};
        String[] fruits = {"a1","a2"};
        String[] veggies = {"b1","b2","b3"};
        String[] meats = {"c1","c2"};
        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();
    if(titles.length != contents.length) {
      throw new IllegalArgumentException("Titles and Contents must be the same size.");
    }
    
    mContext = context;
    mContents = contents;
    mTitles = titles;
  }
  @Override
  public String getChild(int groupPosition, int childPosition) {
    return mContents[groupPosition][childPosition];
  }
  @Override
  public long getChildId(int groupPosition, int childPosition) {
    return 0;
  }
  @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;
  }
  @Override
  public int getChildrenCount(int groupPosition) {
    return mContents[groupPosition].length;
  }
  @Override
  public String[] getGroup(int groupPosition) {
    return mContents[groupPosition];
  }
  @Override
  public int getGroupCount() {
    return mContents.length;
  }
  @Override
  public long getGroupId(int groupPosition) {
    return 0;
  }
  @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;
  }

}

   
    
    
  








Related examples in the same category

1.Using ListView
2.Using SimpleAdapter to fill data to ListView
3.Custom cell Renderer for ListView
4.Provide xml layout for ListView Item
5.Fill contact information to ListView
6.ListView.CHOICE_MODE_MULTIPLE
7.Use AbsListView OnScrollListener(AbsListView.OnScrollListener), AbsListView#setOnItemScrollListener(AbsListView.OnItemScrollListener)} to display the first letter of the visible range of cheeses.
8.This demo illustrates the use of CHOICE_MODE_MULTIPLE_MODAL, a.k.a. selection mode on ListView.
9.FileList extends ListView
10.set ListView Height Based On Children
11.Adding a List
12.Lunch List
13.Get Item index in item click event
14.On nothing selected event
15.Create List