public class MainActivity extends ExpandableListActivity {
private ExpandableListAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAdapter=new MyExpandableListAdapter();
setListAdapter(mAdapter);
registerForContextMenu(getExpandableListView());
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.setHeaderTitle("Sample View");
menu.add(0, 0, 0, "SampleAction");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
ExpandableListContextMenuInfo menuInfo=(ExpandableListContextMenuInfo) item.getMenuInfo();
String title=((TextView)menuInfo.targetView).getText().toString();
int type=ExpandableListView.getPackedPositionType(menuInfo.packedPosition);
if(type==ExpandableListView.PACKED_POSITION_TYPE_CHILD){
int groupPos=ExpandableListView.getPackedPositionGroup(menuInfo.packedPosition);
int childPos=ExpandableListView.getPackedPositionChild(menuInfo.packedPosition);
Toast.makeText(this, title + ": Child " + childPos + " clicked in group " + groupPos,
Toast.LENGTH_SHORT).show();
return true;
}else if(type==ExpandableListView.PACKED_POSITION_TYPE_GROUP){
int groupPos=ExpandableListView.getPackedPositionGroup(menuInfo.packedPosition);
Toast.makeText(this, title + ": Group " + groupPos + " clicked", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
private class MyExpandableListAdapter extends BaseExpandableListAdapter {
private String[] groups = { "People Names", "Dog Names", "Cat Names",
"Fish Names" };
private String[][] children = {
{ "Arnold", "Barry", "Chuck", "David" },
{ "Ace", "Bandit", "Cha-Cha", "Deuce" },
{ "Fluffy", "Snuggles" }, { "Goldy", "Bubbles" } };
@Override
public int getGroupCount() {
return groups.length;
}
@Override
public int getChildrenCount(int groupPosition) {
return children[groupPosition].length;
}
@Override
public String getGroup(int groupPosition) {
return groups[groupPosition];
}
@Override
public String getChild(int groupPosition, int childPosition) {
return children[groupPosition][childPosition];
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
TextView tv = getGenericView();
tv.setText(groups[groupPosition]);
return tv;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
TextView tv = getGenericView();
tv.setText(children[groupPosition][childPosition]);
return tv;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
private TextView getGenericView() {
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, 64);
TextView textView = new TextView(MainActivity.this);
textView.setLayoutParams(lp);
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
textView.setPaddingRelative(36, 0, 0, 0);
textView.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
return textView;
}
}
}