Skip to content

Instantly share code, notes, and snippets.

@cirocosta
Last active December 22, 2015 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cirocosta/6468320 to your computer and use it in GitHub Desktop.
Save cirocosta/6468320 to your computer and use it in GitHub Desktop.
Implementation of an Adapter to an ExpandableListView. Specific for 1-line group/child title.
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
public class NaiveExpandableListAdapter extends BaseExpandableListAdapter {
/*
* Implementation of an Adapter to an ExpandableListView. Specific for
* 1-line group/child title.
*
*/
private Context context;
private ArrayList<ArrayList<String>> children;
private ArrayList<String> groups;
private ChildHolder cHolder;
private GroupHolder gHolder;
public NaiveExpandableListAdapter(Context context,
ArrayList<String> groups, ArrayList<ArrayList<String>> children) {
this.context = context;
this.children = children;
this.groups = groups;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return children.get(groupPosition).get(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) {
View row = convertView;
cHolder = null;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(CHILD_RESOURCE, null);
cHolder = new ChildHolder();
cHolder.title = (TextView) row.findViewById(CHILD_TEXTVIEW);
row.setTag(cHolder);
} else {
cHolder = (ChildHolder) row.getTag();
}
cHolder.title.setText((String) getChild(groupPosition, childPosition));
return row;
}
@Override
public int getChildrenCount(int groupPosition) {
return children.get(groupPosition).size();
}
@Override
public Object getGroup(int groupPosition) {
return groups.get(groupPosition).toString();
}
@Override
public int getGroupCount() {
return groups.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
View row = convertView;
gHolder = null;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(PARENT_RESOURCE, null);
gHolder = new GroupHolder();
gHolder.title = (TextView) row.findViewById(PARENT_TEXTVIEW);
row.setTag(gHolder);
} else {
gHolder = (GroupHolder) row.getTag();
}
gHolder.title.setText((String) getGroup(groupPosition));
return row;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int arg0, int arg1) {
return true;
}
}
class ChildHolder {
TextView title;
}
class GroupHolder {
TextView title;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment