首先需要在layout下的某个xml文件中申明ExpandableView节点
<ExpandableListView android:id="@+id/ExpandableListView_Section" android:layout_width="fill_parent" android:layout_height="wrap_content" android:clickable="false" android:divider="#FFCECFCE" android:dividerHeight="1.0dp" android:fadingEdge="vertical" android:background="#FFFFFFFF" android:childDivider="#FFCECFCE" android:cacheColorHint="#00000000"> </ExpandableListView>
然后在代码中取到该view
expLV = (ExpandableListView) mLayout.findViewById(R.id.ExpandableListView_Section);
然后设置他的adapter
expLV.setAdapter(adapter);
这样就可以了。
adapter是一个继承BaseExpandableListAdapter的类。需要实现如下方法
/**扩展baseExpandableAdapter用于展示二级列表使用*/ public class MyExpandableListAdapter extends BaseExpandableListAdapter{ private LayoutInflater mInflater; private List<KnowledgeCatalogInfo> KnowledgeCatalogInfos; public MyExpandableListAdapter(Context context , List<KnowledgeCatalogInfo> KnowledgeCatalogInfos){ this.mInflater = LayoutInflater.from(context); this.KnowledgeCatalogInfos = KnowledgeCatalogInfos; } public MyExpandableListAdapter(Context context){ this.mInflater = LayoutInflater.from(context); } @Override public int getGroupCount() { } @Override public int getChildrenCount(int groupPosition) { } @Override public Object getGroup(int groupPosition) { return this.KnowledgeCatalogInfos.get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { return null; } @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) { return view; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { return view; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } /** * 获得展示的VIEW * @return */ public View getGenericView(){ View view = this.mInflater.inflate(R.layout.practise_section_group_item_layout, null); return view; } }