Tôi tìm thấy một câu trả lời về Steve Oliver blog ở đây: http://steveoliverc.wordpress.com/2009/10/16/context-menus-for-expandable-lists/
Bạn nên sử dụng onCreateContextMenu()
thay vì tìm kiếm setOnChildLongClickListener()
. Đây là thông tin của Steve:
Danh sách có thể mở rộng hỗ trợ menu ngữ cảnh theo cách tương tự như danh sách chuẩn: thêm người nghe cho menu ngữ cảnh (khi người dùng nhấn lâu vào mục danh sách). Tuy nhiên, không giống như chế độ xem danh sách chuẩn, bạn có thể muốn biết liệu người dùng đã chọn một nhóm (mục có thể mở rộng) hay một mục danh sách con (mục phụ).
Ngoài ra, bạn có thể không muốn làm bất cứ điều gì nếu người dùng cố gắng hiển thị menu ngữ cảnh trên một mục nhóm. Có thể có trường hợp bạn muốn làm điều gì đó cho tất cả trẻ em dưới một nhóm, nhưng trong ứng dụng Librarium của tôi, tôi muốn bỏ qua các mục nhóm và trình bày menu ngữ cảnh chỉ dành cho trẻ em.
Trước tiên, bạn cần phải biết khi nào trình đơn ngữ cảnh sẽ được tạo để bạn có thể xác định liệu người dùng đã nhấn vào một nhóm hay một đứa trẻ. Nếu họ nhấn vào một nhóm, sau đó hủy menu ngữ cảnh. Điều này cũng cho chúng ta cơ hội nhận được văn bản của mục con, để chúng ta có thể đặt nó vào phần đầu của menu ngữ cảnh.
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
ExpandableListView.ExpandableListContextMenuInfo info =
(ExpandableListView.ExpandableListContextMenuInfo) menuInfo;
int type =
ExpandableListView.getPackedPositionType(info.packedPosition);
int group =
ExpandableListView.getPackedPositionGroup(info.packedPosition);
int child =
ExpandableListView.getPackedPositionChild(info.packedPosition);
// Only create a context menu for child items
if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD)
{
// Array created earlier when we built the expandable list
String page =mListStringArray[group][child];
menu.setHeaderTitle(page);
menu.add(0, MENU_READ, 0, “Read page”);
menu.add(0, MENU_EDIT, 0, “Edit page”);
menu.add(0, MENU_FAVORITE, 0, “Add page to favorites”);
menu.add(0, MENU_EXPORT, 0, “Export page to file”);
menu.add(0, MENU_DELETE, 1, “Delete page”);
}
}
Thứ hai, tạo ra các menu ngữ cảnh:
public boolean onContextItemSelected(MenuItem menuItem)
{
ExpandableListContextMenuInfo info =
(ExpandableListContextMenuInfo) menuItem.getMenuInfo();
int groupPos = 0, childPos = 0;
int type = ExpandableListView.getPackedPositionType(info.packedPosition);
if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD)
{
groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);
childPos = ExpandableListView.getPackedPositionChild(info.packedPosition);
}
// Pull values from the array we built when we created the list
String author = mListStringArray[groupPos][0];
String page = mListStringArray[groupPos][childPos * 3 + 1];
rowId = Integer.parseInt(mListStringArray[groupPos][childPos * 3 + 3]);
switch (menuItem.getItemId())
{
case MENU_READ:
readNote(rowId);
return true;
case MENU_EDIT:
editNote(rowId);
return true;
// etc..
default:
return super.onContextItemSelected(menuItem);
}
}
Vậy là xong. Giờ đây, người dùng có thể nhấn và giữ một mục trong danh sách có thể mở rộng và nhận menu ngữ cảnh nếu đó là mục con.
Xem câu trả lời chi tiết ở đây ... http://stackoverflow.com/question/23646098/android-can-i-use-setonlongclicklistener-and-setonclicklistener-cho-same-butto/23646241 # 23646241 – Nepster