声明:本人文笔不好,叙述不好的地方请多谅解,希望大家重点看代码。
ExpandableListView是组ListView嵌套子ListView的组合模式,在这模式中没有长按点击监听,现在我们就来解决一下这个问题,
1.初始化ExpandableListView组件,即:ExpandableListView expandableListView = (ExpandableListView) view.findViewById(R.id.m_expandlist);
2.自定义点击监听,即:expandableListView.setOnItemLongClickListener(new MyExpandListener());
3.为ExpandableListView列表项注册上下文菜单,注册完点击列表项即可调用菜单,即:this.registerForContextMenu(expandableListView);
4.让MyExpandListener()实现OnItemLongClickListener,即
private class MyExpandListener implements OnItemLongClickListener
{
public boolean onItemLongClick(AdapterView<?> parent, View v,int flagPos, long id)
{
return false;
}
}
5.复写onCreateContextMenu方法(里面的方法根据自己需求设置,这个只是我用到的操作方法),即
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
if (menuInfo instanceof ExpandableListView.ExpandableListContextMenuInfo)
{
ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo) menuInfo;
int type = ExpandableListView
.getPackedPositionType(info.packedPosition);
// PACKED_POSITION_TYPE_CHILD:对子列表点击监听
if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD)
{
int groupPos = ExpandableListView
.getPackedPositionGroup(info.packedPosition);
int childPos = ExpandableListView
.getPackedPositionChild(info.packedPosition);
final Friends dInfo = friendsGroupList.get(groupPos)
.getFriendsList().get(childPos);
final FriendsGroup gInfo = friendsGroupList.get(groupPos);
LayoutInflater layoutInflater = LayoutInflater.from(this);
View delFriendView = layoutInflater.inflate(
R.layout.dialog_del_friend, null);
TextView delname = (TextView) delFriendView
.findViewById(R.id.delname);
delname.setText(dInfo.getJid());
final CheckBox delCheckBox = (CheckBox) delFriendView
.findViewById(R.id.delCheckBox);
Dialog dialog = new AlertDialog.Builder(this)
.setIcon(R.drawable.default_head)
.setTitle("删除好友")
.setView(delFriendView)
.setPositiveButton("确定",
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog,
int which)
{
GroupService.removeUserFromGroup(
dInfo.getJid(),
gInfo.getGroupName(),
connection);
if (delCheckBox.isChecked())
{
GroupService.removeUser(roster,
dInfo.getJid());
}
Intent intent = new Intent();
intent.putExtra("USERID", userName);
intent.putExtra("fromUserJid", CHECK);
intent.setClass(MainQQActivity.this,
MainQQActivity.class);
startActivity(intent);
}
})
.setNegativeButton("取消",
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog,
int which)
{
dialog.cancel();
}
}).create();
dialog.show();
}
}
}