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.
Steps to develop Expandable List :
-> Initialize the Expandable List View
-> Construct the Expandable List Adapter
-> Set the Adapter to Expandable List View
Initialize the Expandable List View
ExpandableListView explvList;
explvList = (ExpandableListView)findViewById(R.id.explvList);
Construct the Expandable List Adapter
public class ExpLvAdapter extends BaseExpandableListAdapter
{
@Override
public Object getChild(int groupPosition, int childPosition)
{
return childPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition)
{
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
{
TextView tv = null;
tv = new TextView(Home.this);
tv.setText("ChildView "+groupPosition);
tv.setPadding(30, 0, 0, 0);
return tv;
}
@Override
public int getChildrenCount(int groupPosition)
{
return children[groupPosition].length;
}
@Override
public Object getGroup(int groupPosition)
{
return groupPosition;
}
@Override
public int getGroupCount()
{
return 2;
}
@Override
public long getGroupId(int groupPosition)
{
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent)
{
TextView tv = null;
tv = new TextView(Home.this);
tv.setText("GroupView "+groupPosition);
tv.setPadding(30, 0, 0, 0);
return tv;
}
@Override
public boolean hasStableIds()
{
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition)
{
return true;
}
}
Set the Adapter to Expandable List View
explvList.setAdapter(new ExpLvAdapter());
Output:
Steps to develop Expandable List :
-> Initialize the Expandable List View
-> Construct the Expandable List Adapter
-> Set the Adapter to Expandable List View
Initialize the Expandable List View
ExpandableListView explvList;
explvList = (ExpandableListView)findViewById(R.id.explvList);
Construct the Expandable List Adapter
public class ExpLvAdapter extends BaseExpandableListAdapter
{
@Override
public Object getChild(int groupPosition, int childPosition)
{
return childPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition)
{
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
{
TextView tv = null;
tv = new TextView(Home.this);
tv.setText("ChildView "+groupPosition);
tv.setPadding(30, 0, 0, 0);
return tv;
}
@Override
public int getChildrenCount(int groupPosition)
{
return children[groupPosition].length;
}
@Override
public Object getGroup(int groupPosition)
{
return groupPosition;
}
@Override
public int getGroupCount()
{
return 2;
}
@Override
public long getGroupId(int groupPosition)
{
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent)
{
TextView tv = null;
tv = new TextView(Home.this);
tv.setText("GroupView "+groupPosition);
tv.setPadding(30, 0, 0, 0);
return tv;
}
@Override
public boolean hasStableIds()
{
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition)
{
return true;
}
}
Set the Adapter to Expandable List View
explvList.setAdapter(new ExpLvAdapter());
Output: