Android ExpandableListView相关介绍
一、ExpandableListView介绍一个垂直滚动的显示两个级别(Child,Group)列表项的视图,列表项来自ExpandableListAdapter 。组可以单独展开。
1.重要方法
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
一个接口,将基础数据链接到一个ExpandableListView。此接口的实施将提供访问Child的数据(由组分类),并实例化的Child和Group。
1.重要方法
getChildId(int groupPosition, int childPosition)获取与在给定组给予孩子相关的数据。
getChildrenCount(int groupPosition) 返回在指定Group的Child数目。
2.上代码
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
private List groupData;
private List> childrenData;
private Context context;
public MyExpandableListAdapter(Context context, List groupData, List> childrenData) {
this.context = context;
this.groupData = groupData;
this.childrenData = childrenData;
}
@Override
public int getGroupCount() {
return groupData.size();
}
@Override
public int getChildrenCount(int i) {
return childrenData.get(i).size();
}
@Override
public Object getGroup(int i) {
return groupData.get(i);
}
@Override
public Object getChild(int i, int i1) {
return childrenData.get(i).get(i1);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean b, View convertView, ViewGroup viewGroup) {
GroupHolder groupHolder = null;
if (convertView == null) {
convertView = View.inflate(context, R.layout.item_head, null);
groupHolder = new GroupHolder();
groupHolder.txt = (TextView) convertView.findViewById(R.id.txt);
convertView.setTag(groupHolder);
} else {
groupHolder = (GroupHolder) convertView.getTag();
}
groupHolder.txt.setText(groupData.get(groupPosition));
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
ItemHolder itemHolder = null;
if (convertView == null) {
convertView = View.inflate(context, R.layout.item_child, null);
itemHolder = new ItemHolder();
itemHolder.tv_mes = (TextView) convertView.findViewById(R.id.tv_mes);
convertView.setTag(itemHolder);
} else {
itemHolder = (ItemHolder) convertView.getTag();
}
itemHolder.tv_mes.setText(childrenData.get(groupPosition).get(
childPosition));
return convertView;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return true;
}
class GroupHolder {
public TextView txt;
}
class ItemHolder {
public TextView tv_mes;
}
}
- @Override
- publicbooleanonItemLongClick(AdapterViewarg0,Viewview,intpos,longid){
- //pos不可用说明见下文returnfalse;
- }
如果这个方法是用在ListView长按事件中刚刚好,但在ExpandableListView中,第三个参数pos不能区分开点击的是父项还是子项,以及哪个父项或子项。
在ExpandableListView响应的onItemLongCkick方法中,pos参数值为:从上到下,父项+展现的子项到点击位置的数目(注意:是展现的,隐藏的子项不包括,从0开始)。
例如:
父项1(隐藏3个子项)
父项2
|—子项2-0
|—子项2-1
|—子项2-2
长按子项2-1时,pos值为3。显然根据pos值是无法确定点击的是哪个子项或父项的。
因此依赖pos是很难处理点击位置的。
如果可以直接在onItemLongClick方法中获取groupPos,及childPos该多好呢?
下面我就来所说有几种方法:
1.这种方法是通过下标来找到位置,
@Override public boolean onItemLongClick(AdapterView arg0, View view, int pos, long id) {
- 3.MainActivity的代码
public class MainActivity extends Activity implements ExpandableListView.OnChildClickListener {
private List groupArray;
private List> childArray;
private ExpandableListView expandableListView;
private MyExpandableListAdapter mMyExpandableListAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
}
private void initData() {
//随便一堆测试数据
groupArray = new ArrayList();
groupArray.add("A");
groupArray.add("B");
groupArray.add("C");
List item_list1 = new ArrayList();
item_list1.add("1");
item_list1.add("2");
item_list1.add("3");
List item_list2 = new ArrayList();
item_list2.add("4");
item_list2.add("5");
item_list2.add("6");
List item_list3 = new ArrayList();
item_list3.add("7");
item_list3.add("8");
item_list3.add("9");
childArray = new ArrayList>();
childArray.add(item_list1);
childArray.add(item_list2);
childArray.add(item_list3);
mMyExpandableListAdapter = new MyExpandableListAdapter(MainActivity.this, groupArray, childArray);
expandableListView.setAdapter(mMyExpandableListAdapter);
for (int i = 0; i < mMyExpandableListAdapter.getGroupCount(); i++) {//展开所有父分组
expandableListView.expandGroup(i);
}
}
private void initView() {
expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
expandableListView.setOnChildClickListener(this);
}
@Override
public boolean onChildClick(ExpandableListView expandableListView, View view, int groupPosition, int childPosition, long l) {
Toast.makeText(MainActivity.this, "点击了" + childArray.get(groupPosition).get(childPosition), Toast.LENGTH_SHORT).show();
return true;
}
}
- 效果如图

4.expandableListview它是如何定义长按事件的呢?