ListView只能显示一级列表,如果我们需要像QQ好友列表的那样的效果,就需要用到ExpandableListView,入门新手可能对该控件不是很熟悉,下面就详解一下基本用法,其实跟ListView差不多,下面来说一下具体的使用方法把!
效果图:
首先,布局中加入
<ExpandableListView
android:id ="@+id/expandableListView"
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:groupIndicator="@null"
/>
然后,在activity中设置adapter,这里需要注意的是adapter的使用,我们这里的adapter继承的是BaseExpandableListAdapter
先初始一下数据,
public String[] groups = { "魏", "蜀", "吴" };
public String[][] children = {
{ "曹操", "荀彧", "郭嘉", "夏侯惇", "许褚"},
{ "刘备", "诸葛亮", "关羽", "赵云", "庞统", "魏延", "马超" },
{ "孙权", "周瑜", "鲁肃", "黄盖", "吕蒙"},
};
然后设置adapter和点击监听
ExpandableListView expandableListView = (ExpandableListView)findViewById(R.id.expandableListView);
expandableListView.setAdapter(new ExpandableAdapter(groups,children));
//设置子条目的点击监听
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
Toast.makeText(MainActivity.this, "当前点击的是::"+groups[groupPosition]+"国的"+children[groupPosition][childPosition], Toast.LENGTH_SHORT).show();
//这里return true的话子列表不会展开 return false才展开
return false;
}
});
写一个自定义adapter继承BaseExpandableListAdapter
public class ExpandableAdapter extends BaseExpandableListAdapter{
public String[] groups;
public String[][] children;
public ExpandableAdapter(String[] groups, String[][] children) {
this.groups = groups;
this.children = children;
}
//获取与给定的组相关的数据,得到数组groups中元素的数据
public Object getGroup(int groupPosition) {
return groups[groupPosition];
}
//获取与孩子在给定的组相关的数据,得到数组children中元素的数据
public Object getChild(int groupPosition, int childPosition) {
return children[groupPosition][childPosition];
}
//获取的群体数量,得到groups里元素的个数
public int getGroupCount() {
return groups.length;
}
//取得指定组中的children个数,就是groups中每一个条目中的个数
public int getChildrenCount(int groupPosition) {
return children[groupPosition].length;
}
//获取组在给定的位置编号,即groups中元素的ID
public long getGroupId(int groupPosition) {
return groupPosition;
}
//获取在给定的组的children的ID,也就是children中元素的ID
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
//获取一个视图显示给定组,存放groups
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
TextView textView = getGenericView(24);
textView.setText(getGroup(groupPosition).toString());
return textView;
}
//获取一个视图显示在给定的组 的儿童的数据,就是存放children
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
TextView textView = getGenericView(18);
textView.setText(getChild(groupPosition, childPosition).toString());
return textView;
}
//孩子在指定的位置是可选的,即:children中的元素是可点击的
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
//表示孩子是否和组ID是跨基础数据的更改稳定
public boolean hasStableIds() {
return true;
}
//自定义的创建TextView
public TextView getGenericView(int mTextSize) {
// Layout parameters for the ExpandableListView
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
TextView textView = new TextView(MainActivity.this);
textView.setLayoutParams(lp);
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
textView.setPadding(42, 12, 12, 12);
textView.setTextSize(mTextSize);
textView.setTextColor(Color.BLACK);
return textView;
}
}
OK!完成,运行一下看效果吧!想要美观一些需要在adapter的getChildView和getGroupView中加载自定义的布局文件,类似于BaseAdapter中的getView里的一样,如果条目过多的话注意别忘了在getChildView和getGroupView中使用ViewHolder!
好了,本次教程就这些,有什么错误的地方欢迎指点
本文相关下载:点击免费下载源码及Apk文件