在网上看到一个例子,讲android中的expandablelistview,是一种可以扩展的listview,就是那种点击一下可以扩展出子项,再点一下收缩回去的显示list。因为需要查看一堆文件的目录结构,就使用了expandablelist以便于直观地看到结构形式。
顶层是group,第二层是child。实现ExpandableListView至少需要下面两个类。
一、ExpandableListView
一个垂直滚动的显示两个级别(Child,Group)列表项的视图,列表项来自ExpandableListAdapter 。组可以单独展开。
其所用到的重要方法如下:
expandGroup(int groupPos) :在分组列表视图中展开一组, setSelectedGroup(int groupPosition) :设置选择指定的组。 setSelectedChild(int groupPosition, int childPosition, boolean shouldExpandGroup) :设置选择指定的子项。 getPackedPositionGroup(long packedPosition) :返回所选择的组 getPackedPositionForChild(int groupPosition, int childPosition) :返回所选择的子项 getPackedPositionType(long packedPosition) :返回所选择项的类型(Child,Group) isGroupExpanded(int groupPosition) :判断此组是否展开
二、ExpandableListAdapter
getChildId(int groupPosition, int childPosition) 获取与在给定组给予孩子相关的数据。 getChildrenCount(int groupPosition) 返回在指定Group的Child数目。 getChildView() 获取子视图(就是二级视图) getChildView()获取父视图
当我们想做到自己的打开或者关闭的标记时,可以先设置一个selector.xml,然后再用ExpandableListView的实例去调用setGroupIndicator(this.getResources().getDrawable(R.drawable.expand_list_selector)); 这样就可以了。
下面就看下例子:
这个代码好像也是在eoe上下载下来的,具体也记不太清楚了,先看下效果图:
先给下代码,人家写的,咱们学习下:
public class ListViewActivity extends Activity { ExpandableListView expandableListView; ListViewAdapter treeViewAdapter; public String[] groups = { "列表1", "列表2", "列表3" }; public String[][] child = { { "" }, { "" }, { "", "" } }; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); treeViewAdapter = new ListViewAdapter(this, ListViewAdapter.PaddingLeft >> 1); expandableListView = (ExpandableListView) this .findViewById(R.id.expandableListView); List<ListViewAdapter.TreeNode> treeNode = treeViewAdapter.GetTreeNode(); for (int i = 0; i < groups.length; i++) { ListViewAdapter.TreeNode node = new ListViewAdapter.TreeNode(); node.parent = groups[i]; for (int ii = 0; ii < child[i].length; ii++) { node.childs.add(child[i][ii]); } treeNode.add(node); } treeViewAdapter.UpdateTreeNode(treeNode); expandableListView.setAdapter(treeViewAdapter); } }
class MyGridView extends GridView { public MyGridView(android.content.Context context, android.util.AttributeSet attrs) { super(context, attrs); } /** * 设置不滚动 */ public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } }
adapter的代码:
public class ListViewAdapter extends BaseExpandableListAdapter implements OnItemClickListener { public static final int ItemHeight = 48;// 每项的高度 public static final int PaddingLeft = 36;// 每项的高度 private int myPaddingLeft = 0; private MyGridView toolbarGrid; private String menu_toolbar_name_array[] = { "存储卡", "我的下载", "图书导入", "系统备份", "系统恢复", "清除全部", "在线升级", "快速入门", "关于开卷", "退出系统", "在线升级", "快速入门", "关于开卷", "退出系统", "关于开卷", "退出系统", "关于开卷", "退出系统", "关于开卷", "退出系统" }; private int menu_toolbar_image_array[] = { R.drawable.icon_sdcard, R.drawable.icon_sdcard, R.drawable.icon_sdcard, R.drawable.icon_sdcard, R.drawable.icon_sdcard, R.drawable.icon_sdcard, R.drawable.icon_sdcard, R.drawable.icon_sdcard, R.drawable.icon_sdcard, R.drawable.icon_sdcard, R.drawable.icon_sdcard, R.drawable.icon_sdcard, R.drawable.icon_sdcard, R.drawable.icon_sdcard, R.drawable.icon_sdcard, R.drawable.icon_sdcard, R.drawable.icon_sdcard, R.drawable.icon_sdcard, R.drawable.icon_sdcard, R.drawable.icon_sdcard }; private List<TreeNode> treeNodes = new ArrayList<TreeNode>(); private Context parentContext; private LayoutInflater layoutInflater; static public class TreeNode { Object parent; List<Object> childs = new ArrayList<Object>(); } public ListViewAdapter(Context view, int myPaddingLeft) { parentContext = view; this.myPaddingLeft = myPaddingLeft; } public List<TreeNode> GetTreeNode() { return treeNodes; } public void UpdateTreeNode(List<TreeNode> nodes) { treeNodes = nodes; } public void RemoveAll() { treeNodes.clear(); } public Object getChild(int groupPosition, int childPosition) { return treeNodes.get(groupPosition).childs.get(childPosition); } public int getChildrenCount(int groupPosition) { return treeNodes.get(groupPosition).childs.size(); } static public TextView getTextView(Context context) { AbsListView.LayoutParams lp = new AbsListView.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, ItemHeight); TextView textView = new TextView(context); textView.setLayoutParams(lp); textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); return textView; } /** * 可自定义ExpandableListView */ public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { if (convertView == null) { layoutInflater = (LayoutInflater) parentContext .getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = layoutInflater.inflate(R.layout.view, null); toolbarGrid = (MyGridView) convertView .findViewById(R.id.GridView_toolbar); toolbarGrid.setNumColumns(4);// 设置每行列数 toolbarGrid.setGravity(Gravity.CENTER);// 位置居中 toolbarGrid.setHorizontalSpacing(10);// 水平间隔 toolbarGrid.setAdapter(getMenuAdapter(menu_toolbar_name_array, menu_toolbar_image_array));// 设置菜单Adapter toolbarGrid.setOnItemClickListener(this); } return convertView; } /** * 可自定义list */ public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { TextView textView = getTextView(this.parentContext); textView.setText(getGroup(groupPosition).toString()); textView.setPadding(myPaddingLeft + PaddingLeft, 0, 0, 0); return textView; } public long getChildId(int groupPosition, int childPosition) { return childPosition; } public Object getGroup(int groupPosition) { return treeNodes.get(groupPosition).parent; } public int getGroupCount() { return treeNodes.size(); } public long getGroupId(int groupPosition) { return groupPosition; } public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } public boolean hasStableIds() { return true; } /** * 构造菜单Adapter * * @param menuNameArray * 名称 * @param imageResourceArray * 图片 * @return SimpleAdapter */ private SimpleAdapter getMenuAdapter(String[] menuNameArray, int[] imageResourceArray) { ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>(); for (int i = 0; i < menuNameArray.length; i++) { HashMap<String, Object> map = new HashMap<String, Object>(); map.put("itemImage", imageResourceArray[i]); map.put("itemText", menuNameArray[i]); data.add(map); } SimpleAdapter simperAdapter = new SimpleAdapter(parentContext, data, R.layout.item_menu, new String[] { "itemImage", "itemText" }, new int[] { R.id.item_image, R.id.item_text }); return simperAdapter; } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(parentContext, "当前选中的是:" + position, Toast.LENGTH_SHORT) .show(); } }
private String menu_toolbar_name_array[] = { "存储卡", "我的下载", "图书导入", "系统备份", "系统恢复", "清除全部", "在线升级", "快速入门", "关于开卷", "退出系统", "在线升级", "快速入门", "关于开卷", "退出系统", "关于开卷", "退出系统", "关于开卷", "退出系统", "关于开卷", "退出系统" }; private int menu_toolbar_image_array[] = { R.drawable.icon_sdcard, R.drawable.icon_sdcard, R.drawable.icon_sdcard, R.drawable.icon_sdcard, R.drawable.icon_sdcard, R.drawable.icon_sdcard, R.drawable.icon_sdcard, R.drawable.icon_sdcard, R.drawable.icon_sdcard, R.drawable.icon_sdcard, R.drawable.icon_sdcard, R.drawable.icon_sdcard, R.drawable.icon_sdcard, R.drawable.icon_sdcard, R.drawable.icon_sdcard, R.drawable.icon_sdcard, R.drawable.icon_sdcard, R.drawable.icon_sdcard, R.drawable.icon_sdcard, R.drawable.icon_sdcard };
public List<TreeNode> GetTreeNode() { return treeNodes; } public void UpdateTreeNode(List<TreeNode> nodes) { treeNodes = nodes; } public void RemoveAll() { treeNodes.clear(); } public Object getChild(int groupPosition, int childPosition) { return treeNodes.get(groupPosition).childs.get(childPosition); } public int getChildrenCount(int groupPosition) { return treeNodes.get(groupPosition).childs.size(); } public Object getGroup(int groupPosition) { return treeNodes.get(groupPosition).parent; } public int getGroupCount() { return treeNodes.size(); }
private SimpleAdapter getMenuAdapter(String[] menuNameArray, int[] imageResourceArray) { ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>(); for (int i = 0; i < menuNameArray.length; i++) { HashMap<String, Object> map = new HashMap<String, Object>(); map.put("itemImage", imageResourceArray[i]); map.put("itemText", menuNameArray[i]); data.add(map); } SimpleAdapter simperAdapter = new SimpleAdapter(parentContext, data, R.layout.item_menu, new String[] { "itemImage", "itemText" }, new int[] { R.id.item_image, R.id.item_text }); return simperAdapter; }
代码下载地址如下:http://download.csdn.net/detail/aomandeshangxiao/3986353
网上还有其他例子,
可以一并学习下:Android自定义ExpandableListView用户界面
ViewAndroid版手风琴(ExpandableListView)
ExpandableListView使用及数据更新
用ExpandableListView实现类似QQ好友列表