二级列表(ExpandableListView)
- 概念:
A view that shows items in a vertically scrolling two-level list. This differs from the ListView by allowing two levels: groups which can individually be expanded to show its children. The items come from the ExpandableListAdapter associated with this view.
一种用于垂直滚动展示二级列表的视图,组可以单独展开。这些选项的数据通过ExpandableListView关联。
-
使用场景:
QQ.png -
属性:
divider 设置父选项之间的分割线样式
childDivider 设置子选项之间的分割线样式
divider 设置分割线的高度
groupIndicator 父项前的图标
childIndicator 子项前的图标
- 步骤:
- 创建布局并找id
- 获取数据(死数据,网络数据)
- 创建视图(Group,item)
- 创建适配器(继承BaseExpandableListAdapter)
- 给二级列表绑定适配器
5.适配器(BaseExpandableListAdapter)
//父项的个数
@Override
public int getGroupCount() {
return list.size();
}
//某个子项的个数
@Override
public int getChildrenCount(int groupPosition) {
return list.get(groupPosition).getChildren().size();
}
//获取某个父项
@Override
public Object getGroup(int groupPosition) {
return list.get(groupPosition);
}
//获取某个子项
@Override
public Object getChild(int groupPosition, int childPosition) {
return list.get(groupPosition).getChildren().get(childPosition);
}
//父项的id
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
//子项的id
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
//获取父项的view
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
convertView = LayoutInflater.from(context).inflate(R.layout.layout_group, null);
ViewHolder holder = new ViewHolder(convertView);
holder.tv_group.setText(list.get(groupPosition).getName());
return convertView;
}
//获取子项的view
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
convertView = LayoutInflater.from(context).inflate(R.layout.layout_group, null);
ViewHolder holder = new ViewHolder(convertView);
holder.tv_group.setText(list.get(groupPosition).getChildren().get(childPosition).getName());
return convertView;
}
//子项是否可选中
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
- 监听事件 :适配器的isChildSelectable()方法必须返回true
setOnChildClickListener() 单击子选项
setOnGroupClickListener() 单击组选项
setOnGroupCollapseListener() 分组合并(关)
setOnGroupExpandListener() 分组合并(开)
网友评论