Examples of ExpandableListView usage in Android
- 2020-06-07 05:18:57
- OfStack
This article illustrates the use of ExpandableListView in Android. ExpandableListView is a control that can be used to pull down list in android. The specific implementation method is as follows:
First: Define an ExpandableListView in the xml file of layout
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
androidrientation="vertical"
>
<ExpandableListView
android:id="@+id/expandableListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
Defines two List to store String in the Group/Child control
private List<String> groupArray;
private List<List<String>> childArray;
Initialize both List and insert some data
groupArray = new ArrayList<String>();
childArray = new ArrayList<List<String>>();
groupArray.add(" The first 1 line ");
groupArray.add(" The first 2 line ");
List<String> tempArray = new ArrayList<String>();
tempArray.add(" The first 1 article ");
tempArray.add(" The first 2 article ");
tempArray.add(" The first 3 article ");
for(int index = 0; index <groupArray.size(); ++index)
{
childArray.add(tempArray);
}
Define Adapter for ExpandableListView
//ExpandableListView the Adapter
public class ExpandableAdapter extends BaseExpandableListAdapter
{
Activity activity;
public ExpandableAdapter(Activity a)
{
activity = a;
}
public Object getChild(int groupPosition, int childPosition)
{
return childArray.get(groupPosition).get(childPosition);
}
public long getChildId(int groupPosition, int childPosition)
{
return childPosition;
}
public int getChildrenCount(int groupPosition)
{
return childArray.get(groupPosition).size();
}
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent)
{
String string = childArray.get(groupPosition).get(childPosition);
return getGenericView(string);
}
// group method stub
public Object getGroup(int groupPosition)
{
return groupArray.get(groupPosition);
}
public int getGroupCount()
{
return groupArray.size();
}
public long getGroupId(int groupPosition)
{
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent)
{
String string = groupArray.get(groupPosition);
return getGenericView(string);
}
// View stub to create Group/Children 's View
public TextView getGenericView(String string)
{
// Layout parameters for the ExpandableListView
AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 64);
TextView text = new TextView(activity);
text.setLayoutParams(layoutParams);
// Center the text vertically
text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
// Set the text starting position
text.setPadding(36, 0, 0, 0);
text.setText(string);
return text;
}
public boolean hasStableIds()
{
return false;
}
public boolean isChildSelectable(int groupPosition, int childPosition)
{
return true;
}
}
Finally, add Adapter to the defined ExpandableListView
ExpandableListView expandableListView = (ExpandableListView)findViewById(R.id.expandableListView);
expandableListView.setAdapter(new ExpandableAdapter(Main.this));
I hope this article has been helpful for your Android programming.