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



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章