package com.example.expandablelist; import android.os.Bundle; import android.app.Activity; import android.view.Gravity; import android.view.Menu; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.BaseExpandableListAdapter; import android.widget.ExpandableListAdapter; import android.widget.ExpandableListView; import android.widget.TextView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ExpandableListAdapter adapter = new BaseExpandableListAdapter() { private String[] group = new String[] {"G1","G2","G3"}; private String[][] child = new String[][] {{"g1c1","g1c2","g1c3"}, {"g2c1","g2c2","g2c3"}, {"g3c1","g3c2","g3c3"} }; @Override public boolean isChildSelectable(int groupPosition, int childPosition) { // TODO Auto-generated method stub return true; } @Override public boolean hasStableIds() { // TODO Auto-generated method stub return false; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { // TODO Auto-generated method stub TextView textview =getTextView(); textview.setText(getGroup(groupPosition).toString()); return textview; } @Override public long getGroupId(int groupPosition) { // TODO Auto-generated method stub return groupPosition; } @Override public int getGroupCount() { // TODO Auto-generated method stub return group.length; } @Override public Object getGroup(int groupPosition) { // TODO Auto-generated method stub return group[groupPosition]; } @Override public int getChildrenCount(int groupPosition) { // TODO Auto-generated method stub return child[groupPosition].length; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { // TODO Auto-generated method stub TextView textview = getTextView(); textview.setText(getChild(groupPosition, childPosition).toString()); return textview; } private TextView getTextView() { // TODO Auto-generated method stub AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,64); TextView textview = new TextView(MainActivity.this); textview.setLayoutParams(lp); textview.setGravity(Gravity.CENTER_VERTICAL|Gravity.LEFT); textview.setPadding(36,0,0,0); textview.setTextSize(20); return textview; } @Override public long getChildId(int groupPosition, int childPosition) { // TODO Auto-generated method stub return childPosition; } @Override public Object getChild(int groupPosition, int childPosition) { // TODO Auto-generated method stub return child[groupPosition][childPosition]; } }; ExpandableListView expandListView=(ExpandableListView)findViewById(R.id.list); expandListView.setAdapter(adapter); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }