ExpandableListView的长按点击弹出上下文菜单方法

声明:本人文笔不好,叙述不好的地方请多谅解,希望大家重点看代码。

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();
			}
		}
	}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值