ExpandableListView控件只实现了ExpandableListView.OnChildClickListener接口,完成对子项的点击操作。
通常情况下,我们还会用到长按事件,但 ExpandableListView控件并没有提供长按事件的实现。
这里我们总结两种常见的实现方式:
1、使用上下文菜单实现长按事件
注册上下文菜单 registerForContextMenu(downElv);,并重新下面两个方法:
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
ExpandableListView.ExpandableListContextMenuInfo info =
(ExpandableListView.ExpandableListContextMenuInfo) menuInfo;
//menuinfo该对象提供了选中对象的附加信息
int type = ExpandableListView
.getPackedPositionType(info.packedPosition);
int group = ExpandableListView
.getPackedPositionGroup(info.packedPosition);
int child = ExpandableListView
.getPackedPositionChild(info.packedPosition);
System.out.println("LongClickListener*type-------------------------"
+ type);
System.out.println("LongClickListener*group-------------------------"
+ group);
System.out.println("LongClickListener*child-------------------------"
+ child);
this.setMGroupID(group);
this.setMChildrenID(child);
//处理逻辑
if (type == 0) {// 分组长按事件
showDialog(Globals.DIALOG_GROUPS_LONGCLICK);
} else if (type == 1) {// 长按好友列表项
//showDialog(Globals.DIALOG_FRIENDlIST_LONGCLICK);可以自定义Dialog显示,
也可以使用menu.add(0,0,0,"增加");添加菜单项
}
}
//一般在此函数下面编写响应事件
@Override
public boolean onContextItemSelected(MenuItem item) {
return super.onContextItemSelected(item);
}
2、通过自定义 ExpandableListView适配器,保存分组和子项位置来实现长按事件
下面来说我的解决方法,方法有点投机取巧。首先说明一点,使用我这种方法必须使用自定义的BaseExpandableListAdapter,至于为什么,具体后面讲到。
ExpandableListView本身有继承自AdapterView的setOnItemLongClickListener(AdapterView.OnItemLongClickListener
listener)方法。
实现监听器:
[java] view plain
copy
private class QuickWayListener implements
OnItemLongClickListener{
@Override
public boolean onItemLongClick(AdapterView