2. 设计BaseExpandableListAdapter的子类
我设计的子类是一种通用的Adapter子类,类的实现中并不包含具体的Layout实现,所有的Layout都由GroupList的getResource和buildView来负责,因此,可以非常灵活地修改Layout的具体实现,而不用修改Adapter的代码。
public class GroupListAdapter extends BaseExpandableListAdapter {
private Context mContext;
private List<GroupList> mGroups;
public GroupListAdapter(Context context, List<GroupList> groups) {
this.mContext = context;
this.mGroups = groups;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
List<GroupList> chList = mGroups.get(groupPosition).getChild();
if( chList == null) {
return null;
}
return chList.get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view,ViewGroup parent) {
GroupList child = (GroupList)getChild(groupPosition, childPosition);
if( child == null) {
return null;
}
if (view == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = (RelativeLayout)inflater.inflate(child.getResource(), null);
}
child.buildView(view);
return view;
}
@Override
public int getChildrenCount(int groupPosition) {
List<GroupList> chList = mGroups.get(groupPosition).getChild();
if( chList == null) {
return 0;
}
return chList.size();
}
@Override
public Object getGroup(int groupPosition) {
return mGroups.get(groupPosition);
}
@Override
public int getGroupCount() {
return mGroups.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isLastChild, View view,ViewGroup parent) {
GroupList group = (GroupList)getGroup(groupPosition);
if (view == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = (RelativeLayout)inflater.inflate(group.getResource(), null);
}
group.buildView(view);
return view;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int arg0, int arg1) {
return true;
}
}
3. 应用代码
为了简化,我就直接在MainActivity中使用上述封装的类来完成多级列表的功能演示,示例如下:
public class MainActivity extends Activity implements OnGroupClickListener,OnChildClickListener {
private ExpandableListView mlistView;
private GroupListAdapter mAdapter;
private static final String[] mParentMenu = {
"Book", "Video", "Audio"
};
private static final String[][] mChildMenu = {
{ "book1", "book2", "book3", "book4" },
{ "video1", "video2" },
{ "audio1", "audio2", "audio3","audio4"}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mlistView = new ExpandableListView(this);
mlistView.setOnGroupClickListener(this);
mlistView.setOnChildClickListener(this);
List<GroupList> groups = new ArrayList<GroupList>();
for( int i=0; i<mParentMenu.length; i++) {
List<GroupList> childs = new ArrayList<GroupList>();
for( int j=0; j<mChildMenu[i].length; j++ ) {
childs.add(new GroupListChild(mChildMenu[i][j]));
}
groups.add(new GroupListParent(mParentMenu[i],childs));
}
mAdapter = new GroupListAdapter(this,groups);
mlistView.setAdapter(mAdapter);
setContentView(mlistView);
}
@Override
public boolean onChildClick(ExpandableListView parent, View v,int groupPosition, int childPosition, long id) {
return false;
}
@Override
public boolean onGroupClick(ExpandableListView parent, View v,int groupPosition, long id) {
return false;
}
}