布局文件中
<ExpandableListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/transparent" android:cacheColorHint="#00000000" android:descendantFocusability="afterDescendants" android:divider="@color/white" --》group的divider android:dividerHeight="1px" --> divider的高度,对child也生效 android:childDivider="@color/white" --》 child的divider android:fastScrollEnabled="false" android:groupIndicator="@null" --》 去掉默认的箭头,方便自己定义展开的图片。 android:listSelector="@android:color/transparent" android:scrollbars="none" > </ExpandableListView>
代码中使用:
private ExpandAdapter mAdapter;mAdapter = new ExpandAdapter(mContext,objectList); mListView.setAdapter(mAdapter);
Adapter定义 继承BaseExpandableListAdapter
public class ExpandAdapter extends BaseExpandableListAdapter { private List<Object> groupList; // 这里为对象模型列表,每个列表中需包含childview对象的列表 private Context mContext; private LayoutInflater mLayoutInflater; public ExpandAdapter (Context context, List<Object> objects) { mContext = context; groupList = objects; mLayoutInflater = LayoutInflater.from(context); } @Override public int getGroupCount() { return groupList.size(); } @Override public int getChildrenCount(int groupPosition) { if (groupList.get(groupPosition) != null && groupList.get(groupPosition).getChildList()!= null) { return groupList.get(groupPosition).getChildList().size(); } else { return 0; } } @Override public Object getGroup(int groupPosition) { return groupList.get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { return groupList.get(groupPosition).getChildList().get(childPosition); } @Override public long getGroupId(int groupPosition) { return 0; } @Override public long getChildId(int groupPosition, int childPosition) { return 0; } @Override public boolean hasStableIds() { return false; }
// group的布局显示,每个组的布局
@Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { GroupViewHolder viewHolder = null; if (convertView == null) { viewHolder = new GroupViewHolder(); convertView = mLayoutInflater.inflate(R.layout.item_groupview, null); convertView.setTag(viewHolder); } else { viewHolder = (GroupViewHolder) convertView.getTag(); } // TODO if (isExpanded) { //展开了,如果需要显示自定义的箭头 } else { //未展开,如果需要显示自定义的箭头 } return convertView; } // 组的字列表布局 @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { ChildViewHolder viewHolder = null; if (convertView == null) { viewHolder = new ChildViewHolder(); convertView = mLayoutInflater.inflate(R.layout.item_childview, null); convertView.setTag(viewHolder); } else { viewHolder = (ChildViewHolder) convertView.getTag(); } // TODO return convertView; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } class GroupViewHolder { // TODO } class ChildViewHolder { // TODO }}
实现了上面的方法就可以显示ExpandablListView了
展开listview某项
exListView.expandGroup(groupPosition);